GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] ForLoopCanBeForeach: inconsistent detection between `i += 1` and `i = i + 1` update forms · Issue #6692 · pmd/pmd

[java] ForLoopCanBeForeach: inconsistent detection between i += 1 and i = i + 1 update forms #6692

New issue
Copy link
New issue
Copy link
Closed
#6727
Closed
[java] ForLoopCanBeForeach: inconsistent detection between i += 1 and i = i + 1 update forms#6692
#6727
Copy link
Labels
a:false-negativePMD doesn't flag a problematic piece of code
Milestone
7.26.0

Description

@Chordrain
Chordrain
opened on May 17, 2026
Issue body actions

Affects PMD Version: 7.24.0

Rule: ForLoopCanBeForeach — https://docs.pmd-code.org/latest/pmd_rules_java_bestpractices.html#forloopcanbeforeach

Description:

The ForLoopCanBeForeach rule correctly flags standard for loops that can be coverted into enhanced for-each loops when the update expression uses i++ or i += 1. However, it fails to detect the violation (resulting in a False Negative) when the update expression is written as a direct assignment i = i + 1, even though the logic is semantically identical.

Code Sample demonstrating the issue:

import java.util.List;

public class Sample {
    public static void main(String[] args) {
        List<Integer> list = List.of(1, 2, 3, 4, 5, 6);

        // Case 1: Flagged correctly by ForLoopCanBeForeach
        for (int i = 0; i < list.size(); i += 1) {
            System.out.println(list.get(i));
        }

        // Case 2: NOT flagged (False Negative)
        for (int i = 0; i < list.size(); i = i + 1) {
            System.out.println(list.get(i));
        }
    }
}

Expected outcome: PMD should report a violation at line 13, but doesn't. This is a false-negative.

Running PMD through: CLI

Activity

  1. Chordrain
    added
    a:false-negativePMD doesn't flag a problematic piece of code
    on May 17, 2026
  2. qwerty7878 commented on May 27, 2026

    @qwerty7878
    qwerty7878
    on May 27, 2026
    Contributor
    More actions

    I'm working on a fix for this.

    Reacted by Xiao Te
  3. UncleOwen
    added a commit that references this issue on May 31, 2026

    [doc] Update release notes (pmd#6692)

    0f36c16
  4. UncleOwen
    added this to the 7.26.0 milestone on May 31, 2026
  5. UncleOwen
    added a commit that references this issue on May 31, 2026

    [java] Fix #6692: ForLoopCanBeForeach detect i = i + 1 update form (#…

    This issue will close once commit 8a740a4 is merged into the 'main' branch.
    8a740a4
  6. UncleOwen
    closed this as completedin #6727on May 31, 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

    a:false-negativePMD doesn't flag a problematic piece of code

    Type

    No type

    Projects

    No projects

      Milestone

      • 7.26.0

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.