Skip to content

[java] UnnecessaryLocalBeforeReturn/VariableCanBeInlined: deprecate property statementOrderMatters #3124

Closed

Description

@adangel

Affects PMD Version: 6.x

Rule:
UnnecessaryLocalBeforeReturn

Description:

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:

public class Foo {
    public int noViolation() {
        int res = calcValue(); // might throw, might have a side-effect for doSomething()
        doSomething();
        return res;
    }
    public int violation() { // can be reordered, because res is not used somewhere else
        int res = 2; // a constant
        doSomething();
        return res;
    }
    public int noViolation2() {
        int res = 2; // a constant
        doSomething(res);
        return res;
    }
    public void doSomething() {
        // omitted
    }
    public void doSomething(int c) {
        // omitted
    }
    public int calcValue() {
        // omitted
    }
}

Expected outcome:

  • In the end, the rule should be smart enough to not produce false-positive/false-negatives even without this property.

Related issue:

Activity

  1. added
    a:false-positive
    a:false-negative
    on Feb 19, 2021
  2. removed
    a:false-negative
    a:false-positive
    on Apr 5, 2021
  3. added this to the 7.0.0 milestone on Jan 9, 2023
  4. modified the milestones: 7.0.0, 7.x on Jan 23, 2023
  5. adangel commented on Sep 12, 2025

    @adangel
    MemberAuthor

    Note - the rule UnnecessaryLocalBeforeReturn has been deprecated with 7.17.0 and the replacement rule is called VariableCanBeInlined

    See #5770 / #5847

  6. fudianchn commented on Aug 28, 2026

    @fudianchn
    Contributor

    Current state on main (6dbb11d):

    Can this be closed?

  7. UncleOwen commented on Sep 12, 2026

    @UncleOwen
    Member

    No, I don't think statementOrderMatters=false makes any sense and should be deprecated on the new rule.

  8. changed the title [-][java] UnnecessaryLocalBeforeReturn - remove property statementOrderMatters [/-] [+][java] UnnecessaryLocalBeforeReturn/VariableCanBeInlined - remove property statementOrderMatters [/+] on Sep 13, 2026
  9. modified the milestones: 7.x, 7.28.0 on Sep 13, 2026
  10. added
    is:deprecation
    on Sep 14, 2026
  11. changed the title [-][java] UnnecessaryLocalBeforeReturn/VariableCanBeInlined - remove property statementOrderMatters [/-] [+][java] UnnecessaryLocalBeforeReturn/VariableCanBeInlined: deprecate property statementOrderMatters [/+] on Sep 14, 2026
  12. changed the title [-][java] UnnecessaryLocalBeforeReturn/VariableCanBeInlined: deprecate property statementOrderMatters [/-] [+][java] UnnecessaryLocalBeforeReturn/VariableCanBeInlined: deprecate property statementOrderMatters[/+] on Sep 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    is:deprecation

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions