openrewrite / rewrite-migrate-java

OpenRewrite recipes for migrating to newer versions of Java.
Apache License 2.0
102 stars 71 forks source link

Recipe for java:S3599 : Double Brace Initialization should not be used #69

Closed yeikel closed 2 years ago

yeikel commented 2 years ago
Map<String,String> source = new HashMap(){{ 
    put("firstName", "John");
    put("lastName", "Smith");
}};

Java 8 compliant :

Map<String,String> source = new HashMap();
source.put("firstName", "John");
source.put("lastName", "Smith");

Java 11 compliant (immutable) :

Map<String,String> source = Map.of("firstName", "John","lastName", "Smith");

More context : https://rules.sonarsource.com/java/RSPEC-3599

pway99 commented 2 years ago

Good Morning @yeikel :) Both of these conversions should be accounted for:

https://github.com/openrewrite/rewrite/blob/main/rewrite-java/src/main/java/org/openrewrite/java/cleanup/NoDoubleBraceInitialization.java

https://github.com/openrewrite/rewrite-migrate-java/blob/main/src/main/java/org/openrewrite/java/migrate/lang/UseMapOf.java

yeikel commented 2 years ago

https://github.com/openrewrite/rewrite/blob/main/rewrite-java/src/main/java/org/openrewrite/java/cleanup/NoDoubleBraceInitialization.java

That's awesome. Thank you!

Unfortunately, it seems that this doc is outdated : https://docs.openrewrite.org/reference/recipes/java/cleanup/cleanup

I was able to find that rule in the sidebar but not in the main recipe list

image

pway99 commented 2 years ago

sure there are a lot of recipes to look through and not all of them are included in the cleanup or static-code-analysis recipes. 🤔 NoDoubleBraceInitialization may be a good Cleanup recipe candidate.

yeikel commented 2 years ago

Oh, maybe I misunderstood but I thought that the fact that it is under the cleanup package makes it already a cleanup recipe

pway99 commented 2 years ago

That is correct, and I can appreciate the misunderstanding. The Cleanup recipe is a selected set of recipes intended to catch most of the cleanup issues without being too noisy for a reasonable code-review effort. In some cases like NoDoubleBraceInitialization and UseMapOf the developer has an option and the result may require a more careful review.

yeikel commented 2 years ago

That is correct, and I can appreciate the misunderstanding. The Cleanup recipe is a selected set of recipes intended to catch most of the cleanup issues without being too noisy for a reasonable code-review effort. In some cases like NoDoubleBraceInitialization and UseMapOf the developer has an option and the result may require a more careful review.

Thank you for clarifying :)