You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
this rule could be expanded to all "unnecessary" variables, not just return statements. We could add a property to allow such variables if declared as final. The Java code will produce an extra store / load instruction in bytecode, but being final, the JIT will remove them and effectively be the same for long running processes.
Also deprecate the rule UnnecessaryLocalBeforeReturn
We should think about the following problem: if the whole java-design ruleset is active, and we have both rules UnnecessaryLocalBeforeReturn and the new UnnecessaryLocal, they might report the same issues...
No. UnusedLocalVariable is usages == 0, ie: int x = 9; and no further references to x found.
UnnecessaryLocalBeforeReturn is int x = 9; return x;.
However, if we use the symbol table info and check for usages < 2 as discussed we would match both, UnusedLocalVariable (usages == 0), and UnnecessaryLocalBeforeReturn (usages == 1).
We can probably keep UnusedLocalVariable and make the new rule only check for usages == 1.
The rule will check for each context if the number of uses of the variableis exactly 1. If it is, it will check if it's final and the corresponding property to ignore finals is true.
By definition, all errors currently reported by UnnecessaryLocalBeforeReturn will be covered by this rule too. Not the other way around.
Shall UnnecessaryLocalBeforeReturn be deprecated? Disabled automatically upon finding both rules configured? Add a flag to the new rule to only check returned locals and keep the old behavior?
This rule should also consider scenarios were a temp variable is needed because it's initialized with fields / variables that are later modified. See #282
I'm having second thoughts about this rule. I've ceased to see scenarios were this would report on any case where:
it's not already detected by UnnecessaryLocalBeforeReturn
it's not a variable introduced for readability
If someone does see a use-case for this, please let me know, otherwise, I'd just drop it; and add a property on UnnecessaryLocalBeforeReturn to assume statement order matters (go back to the old behavior of having statements immediately follow one another) to solve issues such as #310
Activity
jsotuyod commentedon Feb 6, 2017
There is already a
UnusedLocalVariablerule in the unused ruleset, which could also have some collisions with this.adangel commentedon Feb 6, 2017
So we should have already an overlapping between
UnnecessaryLocalBeforeReturnandUnusedLocalVariable?jsotuyod commentedon Feb 6, 2017
No.
UnusedLocalVariableis usages == 0, ie:int x = 9;and no further references toxfound.UnnecessaryLocalBeforeReturnisint x = 9; return x;.However, if we use the symbol table info and check for usages < 2 as discussed we would match both,
UnusedLocalVariable(usages == 0), andUnnecessaryLocalBeforeReturn(usages == 1).We can probably keep
UnusedLocalVariableand make the new rule only check for usages == 1.adangel commentedon Feb 6, 2017
You're right - seems like we can close the issue then....
jsotuyod commentedon Feb 6, 2017
So, summing up.
Rule Set: probably java-unnecessary
The rule will check for each context if the number of uses of the variable is exactly 1. If it is, it will check if it's final and the corresponding property to ignore finals is true.
By definition, all errors currently reported by
UnnecessaryLocalBeforeReturnwill be covered by this rule too. Not the other way around.Shall
UnnecessaryLocalBeforeReturnbe deprecated? Disabled automatically upon finding both rules configured? Add a flag to the new rule to only check returned locals and keep the old behavior?jsotuyod commentedon Feb 17, 2017
The new rule should also consider annotated variables as valid, even if used only once. See #262
jsotuyod commentedon Mar 18, 2017
This rule should also consider scenarios were a temp variable is needed because it's initialized with fields / variables that are later modified. See #282
jsotuyod commentedon Apr 17, 2017
I'm having second thoughts about this rule. I've ceased to see scenarios were this would report on any case where:
UnnecessaryLocalBeforeReturnIf someone does see a use-case for this, please let me know, otherwise, I'd just drop it; and add a property on
UnnecessaryLocalBeforeReturnto assume statement order matters (go back to the old behavior of having statements immediately follow one another) to solve issues such as #310