java:S4165 Should not judge about method implementation

The Java rule S4165 complains about a variable assignment to be redundant, which it technically is. But it’s the wrong place to tell. See the following code.

public static String foo( String s )
    // We may want to do something really smart with this String before returning.
    return s;
String bar = "bar";
bar = bar ); // FP

At the marked line Sonar says, that the assignment is redundant. Yes, it is. But who are we to forsee the implementation of the foo() method? It may change. Do I have to add a NOSONAR tag to any location, where a library decides to change a method’s implementation? Ot should I even change my implementation and remember to change it back when the lib’s changelog tells me, that this method actually does something to the return value again? Not a good idea.

I do agree, that this may be woth to be notified for other cases. But for this case it is definitely FP.

Used products: Java, SonarCube and Lint.
Qube 8.3, soon 9.4.
Lint 7.4.0.

Thank you for reporting @marvin.

I believe the status quo is correct for the reason that future changes in method foo will lead the rule to no longer trigger. The reason is that the analyzer will see that what is returned has changed (and thus the allocation is no longer redundant).

As for your mention of the impact on libraries, the rule considers only code living in the same file. Thus library calls will never raise an issue.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.