Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collections.singleton and Collections.singletonList allow null, Set.of and List.of not #250

Open
JosRoseboom opened this issue Jul 6, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@JosRoseboom
Copy link

Using openrewrite 6.1.8 using gradle:

rewrite {
	activeRecipe(
			"org.openrewrite.java.migrate.UpgradeToJava17"
	)
}

dependencies {
	rewrite(platform("org.openrewrite.recipe:rewrite-recipe-bom:2.0.4"))
	rewrite("org.openrewrite.recipe:rewrite-migrate-java")
}

This recipe replaces Collections.singleton with Set.of and Collections.singletonList by List.of . However, the former allows null while the latter does not. So these 2 lines:

var mySet = Collections.singleton(myVarThatIsNull); // results in a Set: [null]
var myList = Collections.singletonList(myVarThatIsNull); // results in a List: [null]

are replaced by the recipe by:

var mySet = Set.of(myVarThatIsNull); // results in a NPE thrown
var myList = List.of(myVarThatIsNull); // results in a NPE thrown

which creates a potential NPE that would not happened in the original code.

@JosRoseboom JosRoseboom added the bug Something isn't working label Jul 6, 2023
@timtebeek
Copy link
Contributor

Hmm; that's odd; we had a similar case come up in review recently, where we specifically recommended to introduce data flow analysis before applying the change there; sounds like this recipe needs a similar change. Would you be open to making that change with help?

@timtebeek
Copy link
Contributor

In the short term it's probably best to disable this recipe in any collections of recipes; then people can still decide to run it explicitly but know they might need to review for nullability.

@knutwannheden
Copy link
Contributor

Currently this recipe is part of org.openrewrite.java.migrate.util.JavaUtilAPIs which in turn is part of org.openrewrite.java.migrate.Java8toJava11. Since all of the recipes in org.openrewrite.java.migrate.util.JavaUtilAPIs have this flaw, we should probably remove org.openrewrite.java.migrate.util.JavaUtilAPIs entirely from org.openrewrite.java.migrate.Java8toJava11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Backlog
Development

No branches or pull requests

3 participants