pmd / pmd/pmd

[java] Improve ForLoopCanBeForeach to use StablePathMatcher

Open
#4,289 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

an:enhancement
Dominant language
Java
Stars
5.5k
Forks
1.6k
Avg merge
1d 19h
Merged PRs (30d)
54

Description

Rule: ForLoopCanBeForeach

Use the StablePathMatcher of #3191 to improve ForLoopCanBeForeach (see https://github.com/pmd/pmd/pull/3191#discussion_r606865642):

Note: this thing, might look overweight for the task (there's a single test case in BrokenNullCheck that requires it), but I think it could be used in other rules to make them smarter. For instance, in ForLoopCanBeForeach, we could recognise looping on any "stable path", eg this.field.collection or this.getCollection().

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ForLoopCanBeForeach rule and read the StablePathMatcher work from pull request #3191, including the referenced discussion. Determine how the rule can use stable paths such as this.field.collection or this.getCollection(), then verify the rule's existing tests and add coverage for the supported cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.