openrewrite / rewrite-static-analysis

OpenRewrite recipes for identifying and fixing static analysis issues.
Apache License 2.0
32 stars 52 forks source link

RSPEC-2864 - entrySet()" should be iterated when both the key and value are needed #360

Open blipper opened 1 month ago

blipper commented 1 month ago

Describe the situation before applying the recipe

See https://rules.sonarsource.com/java/RSPEC-2864/

Describe the situation after applying the recipe

Have you considered any alternatives or workarounds?

Any additional context

This is a SpotBugs error https://spotbugs.readthedocs.io/en/stable/bugDescriptions.html#wmi-inefficient-use-of-keyset-iterator-instead-of-entryset-iterator-wmi-wrong-map-iterator

https://github.com/spotbugs/spotbugs/blob/d999c35c134f5310b688f2b4513e3689fb1439bc/spotbugs/src/main/java/edu/umd/cs/findbugs/detect/WrongMapIterator.java#L256

Test cases here https://github.com/spotbugs/spotbugs/blob/d999c35c134f5310b688f2b4513e3689fb1439bc/spotbugsTestCases/src/java/WMITest.java#L10

Are you interested in contributing this recipe to OpenRewrite?

timtebeek commented 1 month ago

Thanks for the helpful example links; indeed would be a neat addition to see this fixed with a recipe.