openrewrite / openrewrite/rewrite-static-analysis
Iterate using `map.values()` or `map.keySet()` when only one of the two is used instead of `map.entrySet()`
Open
Nobody has claimed this yet.
recipe
- Dominant language
- Java
- Stars
- 62
- Forks
- 112
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 40
Description
Map<String,String> map = new HashMap<>();
map.put("hello","world");
for (Map.Entry<String, String> entry : map.entrySet()){
System.out.println(entry.getValue()); // the key() is not used here
}
Map<String,String> map = new HashMap<>();
map.put("hello","world");
for (String value : map.values()){
System.out.println(value); // Access the values and iterate over that instead
}
Same applies with the key :
Map<String,String> map = new HashMap<>();
map.put("hello","world");
for (Map.Entry<String, String> entry : map.entrySet()){
System.out.println(entry.getKey()); // the value() is not used here
}
Map<String,String> map = new HashMap<>();
map.put("hello","world");
for (String key: map.keySet()){
System.out.println(key);
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the existing Java static-analysis recipes and their tests in this repository, then find the entry point for rules that inspect map iteration. Add coverage for loops that use only the key or only the value, and verify that the suggested iteration form is produced without changing loops that use both.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100