Skip to content

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

Merged
UncleOwen merged 4 commits into
pmd:mainfrom
qwerty7878:fix/for-loop-can-be-foreach-assign-increment
May 31, 2026
Merged

[java] Fix #6692: ForLoopCanBeForeach detect i = i + 1 update form#6727
UncleOwen merged 4 commits into
pmd:mainfrom
qwerty7878:fix/for-loop-can-be-foreach-assign-increment

Conversation

@qwerty7878

@qwerty7878 qwerty7878 commented May 28, 2026

Copy link
Copy Markdown
Contributor

Describe the PR

Fix a false-negative in the ForLoopCanBeForeach rule where a for loop using i = i + 1 (or i = 1 + i) as the update expression was not detected as convertible to a foreach loop, even though it is semantically identical to i++ or i += 1.
Added a new helper method asIEqualIPlusOne to handle the direct assignment pattern.

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

@qwerty7878 qwerty7878 force-pushed the fix/for-loop-can-be-foreach-assign-increment branch from 1006269 to 9a5eb2d Compare May 28, 2026 10:13
@pmd-actions-helper

pmd-actions-helper Bot commented May 28, 2026

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 15 errors and 9 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-05-31 07:51:25+00:00 for 0f36c16)

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've left some suggestions. Otherwise, this looks good!

@qwerty7878 qwerty7878 requested a review from UncleOwen May 30, 2026 22:20
@qwerty7878

qwerty7878 commented May 30, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for the review! I've addressed all the comments:

  • Reordered the condition for readability
  • Applied static imports for isReferenceToVar and isLiteralInt
  • Added test cases for i = 1 + i, i = 1 - 1, and i = somefunc(i)

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@UncleOwen UncleOwen added this to the 7.26.0 milestone May 31, 2026
@UncleOwen UncleOwen merged commit 8a740a4 into pmd:main May 31, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants