A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/4569 below:

[java] ForLoopCanBeForeach reports on loop `for (int i = 0; i < list.size(); i += 2)` · Issue #4569 · pmd/pmd · GitHub

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


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4