Skip to content

[java] New Rule: UnnecessaryLocal #241

Closed

Description

@adangel

Rule Set: probably java-unnecessary

Description:
See discussion in #221:

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...

Activity

  1. added this to the 5.6.0 milestone on Feb 6, 2017
  2. added
    a:new-rule
    on Feb 6, 2017
  3. jsotuyod commented on Feb 6, 2017

    @jsotuyod
    Member

    There is already a UnusedLocalVariable rule in the unused ruleset, which could also have some collisions with this.

  4. adangel commented on Feb 6, 2017

    @adangel
    MemberAuthor

    So we should have already an overlapping between UnnecessaryLocalBeforeReturn and UnusedLocalVariable?

  5. jsotuyod commented on Feb 6, 2017

    @jsotuyod
    Member

    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.

  6. adangel commented on Feb 6, 2017

    @adangel
    MemberAuthor

    You're right - seems like we can close the issue then....

  7. jsotuyod commented on Feb 6, 2017

    @jsotuyod
    Member

    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 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?

  8. jsotuyod commented on Feb 17, 2017

    @jsotuyod
    Member

    The new rule should also consider annotated variables as valid, even if used only once. See #262

  9. jsotuyod commented on Mar 18, 2017

    @jsotuyod
    Member

    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

  10. self-assigned this
    on Apr 16, 2017
  11. jsotuyod commented on Apr 17, 2017

    @jsotuyod
    Member

    I'm having second thoughts about this rule. I've ceased to see scenarios were this would report on any case where:

    1. it's not already detected by UnnecessaryLocalBeforeReturn
    2. 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

  12. modified the milestones: 5.7.0, 5.6.0 on Apr 22, 2017
  13. modified the milestones: 5.8.0, 5.7.0 on May 20, 2017
  14. modified the milestones: 6.0.0, 5.8.0 on Jun 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

a:new-rule

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions