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] ForLoopCanBeForeach reports on loop for (int i = 0; i < list.size(); i += 2) #4569

Closed
Luro02 opened this issue May 22, 2023 · 0 comments · Fixed by #4908
Closed

[java] ForLoopCanBeForeach reports on loop for (int i = 0; i < list.size(); i += 2) #4569

Luro02 opened this issue May 22, 2023 · 0 comments · Fixed by #4908
Labels
a:false-positive PMD flags a piece of code that is not problematic
Milestone

Comments

@Luro02
Copy link

Luro02 commented May 22, 2023

Affects PMD Version: 7.0.0-rc2 (Note: does not affect 6.55.0)

Rule: ForLoopCanBeForeach

https://docs.pmd-code.org/pmd-doc-7.0.0-rc1/pmd_rules_java_bestpractices.html#forloopcanbeforeach

Description:

I guess with the update of the ast and type resolution, the code broke? (github does not show any recent changes to the check and there is no test case present for += increments)

Code Sample demonstrating the issue:

import java.util.*;
                                
public class Test {
    public static void main(String[] args) {
        List<Integer> list = List.of(1, 2, 3, 4, 5, 6);
                                
        for (int i = 0; i < list.size(); i += 2) { // Ok
            System.out.println(list.get(i));
        }
                        
        for (int i = 0; i < list.size(); i += 1) { // Not Ok
            System.out.println(list.get(i));
        }

        for (int i = 0; i < list.size(); i += 3) { // Ok
            System.out.println(list.get(i));
        }
    }
}

Expected outcome:

PMD reports a violation for all three loops. It should only report the one in the middle.
For PMD 6.55.0 it does not report any of the loops.

Running PMD through: Other

@Luro02 Luro02 added the a:false-positive PMD flags a piece of code that is not problematic label May 22, 2023
@jsotuyod jsotuyod added needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale and removed needs:pmd7-revalidation The issue hasn't yet been retested vs PMD 7 and may be stale labels Mar 17, 2024
@adangel adangel added this to the 7.0.1 milestone Apr 2, 2024
@adangel adangel changed the title [java] ForLoopCanBeForeach reports on loop for (int i = 0; i < list.size(); i += 2) { [java] ForLoopCanBeForeach reports on loop for (int i = 0; i < list.size(); i += 2) Apr 2, 2024
@adangel adangel closed this as completed in 4ce9507 Apr 2, 2024
adangel added a commit that referenced this issue Apr 2, 2024
adangel added a commit that referenced this issue Apr 2, 2024
[java] Fix #4569 issue with ForLoopCanBeForeach when stride is not 1 #4908
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.

3 participants