Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[java] UnusedLocalVariable: false-positive with multiple for-loop indices #4518

Closed
adangel opened this issue Apr 28, 2023 · 0 comments · Fixed by #4501
Closed

[java] UnusedLocalVariable: false-positive with multiple for-loop indices #4518

adangel opened this issue Apr 28, 2023 · 0 comments · Fixed by #4501
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@adangel
Copy link
Member

adangel commented Apr 28, 2023

Affects PMD Version: 6.x

Rule: UnusedLocalVariable

Description:

Index variables of for loops should be ignored by this rule. This works if a single index is used, but for multiple variables, a false positive violation is reported.

Note: This is already fixed with PMD 7.0.0-rc1.
Found via #3123.

Code Sample demonstrating the issue:

public class UnusedLocalVarsMultiple {
    public void sample1() {
        int a = 0, b = 1; // a and b unused (line 3)
    }
    public void sample2() {
        int a = 0, b = a; // only b unused (line 6)
    }
    public void sample3() {
        for (int i = 0; i < 10; i++); // i is loop index, is used and should be ignored
    }
    public void sample4() {
        for (int i = 0, j = 0; i < 10; i++, j++); // i and j are loop indices, both are used (line 12)
    }
    public void sample5() {
        for (int i = 0, j = 0; i < 10; i++); // i and j are loop indices, but j is not used (line 15)
    }
}

Expected outcome:

PMD reports a violation at line 12, but that's wrong. That's a false positive.

@adangel adangel added the a:false-positive PMD flags a piece of code that is not problematic label Apr 28, 2023
@adangel adangel added this to the 7.0.0 milestone Apr 28, 2023
@adangel adangel changed the title [java] UnusedLocalVariable: false-positive with multiple for-loop indexes [java] UnusedLocalVariable: false-positive with multiple for-loop indices Apr 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:false-positive PMD flags a piece of code that is not problematic
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant