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
The property statementOrderMatters is not so helpful. If it is set to false, the rule is relaxed to ignore any statements between the local variable assignment and the return statement under the assumption, that these statements have no side-effects. This is just too simple. We should assume, that all methods calls could have side-effects and reordering the local var assignment could lead to different results. That means, by fixing a PMD violation, a application bug could be introduced.
Code Sample demonstrating the issue:
publicclassFoo {
publicintnoViolation() {
intres = calcValue(); // might throw, might have a side-effect for doSomething()doSomething();
returnres;
}
publicintviolation() { // can be reordered, because res is not used somewhere elseintres = 2; // a constantdoSomething();
returnres;
}
publicintnoViolation2() {
intres = 2; // a constantdoSomething(res);
returnres;
}
publicvoiddoSomething() {
// omitted
}
publicvoiddoSomething(intc) {
// omitted
}
publicintcalcValue() {
// omitted
}
}
Expected outcome:
In the end, the rule should be smart enough to not produce false-positive/false-negatives even without this property.
VariableCanBeInlined declares statementOrderMatters with defaultValue(true), so the conservative behavior is now the default.
Checked with the sample from this issue: on 6.31.0 with statementOrderMatters=false, the noViolation() method is reported (the unsafe suggestion this issue is about); on main, both the deprecated rule with its default and VariableCanBeInlined with its default report nothing there, while the consecutive case int res = 2; return res; is still reported by VariableCanBeInlined.
Activity
adangel commentedon Sep 12, 2025
Note - the rule UnnecessaryLocalBeforeReturn has been deprecated with 7.17.0 and the replacement rule is called VariableCanBeInlined
See #5770 / #5847
fudianchn commentedon Aug 28, 2026
Current state on main (
6dbb11d):statementOrderMatterswithdefaultValue(true), so the conservative behavior is now the default.statementOrderMatters=false, thenoViolation()method is reported (the unsafe suggestion this issue is about); on main, both the deprecated rule with its default and VariableCanBeInlined with its default report nothing there, while the consecutive caseint res = 2; return res;is still reported by VariableCanBeInlined.Can this be closed?
UncleOwen commentedon Sep 12, 2026
No, I don't think
statementOrderMatters=falsemakes any sense and should be deprecated on the new rule.[-][java] UnnecessaryLocalBeforeReturn - remove property statementOrderMatters [/-][+][java] UnnecessaryLocalBeforeReturn/VariableCanBeInlined - remove property statementOrderMatters [/+][java] Deprecate statementOrderMatters property of VariableCanBeInlin…
[-][java] UnnecessaryLocalBeforeReturn/VariableCanBeInlined - remove property statementOrderMatters [/-][+][java] UnnecessaryLocalBeforeReturn/VariableCanBeInlined: deprecate property statementOrderMatters [/+][-][java] UnnecessaryLocalBeforeReturn/VariableCanBeInlined: deprecate property statementOrderMatters [/-][+][java] UnnecessaryLocalBeforeReturn/VariableCanBeInlined: deprecate property statementOrderMatters[/+]