Sonar "useless assignment to local variable" workaround?

Viewed 26429

I am working towards improving my code, and I came across this issue from Sonar:

Remove this useless assignment to local variable "uiRequest"

Fact is, it is not useless, as I am using it just after in the code:

        // I am supposed to remove this
        UiRequest uiRequest = null;

        if("Party".equals(vauban.getName()))   {
            uiRequest = contextBuilder.buildContext(vauban);
        } else {
            // Maybe I could work my way around here ?
            throw new NamingException(
                    String.format(
                            "Hey %s, change your name to %s, thanks",
                            vauban.getName(), "Vauban"));
        }

        // Set the generated Id in the result of context builder
        MyOwnService response = callService(uiRequest, vauban);

        return response;

Sonar still tells me that "uiRequest" is useless, why ? It is not, as I don't want it to reach the code if it is null. I tried initializing it (uiRequest = new UiRequest()) but it keeps telling me that it is useless.

Anyone got an idea about why Sonar behaves like this / how to correct this ?

4 Answers
Related