From 80cb6723914bb9304ce5b4902e86ae21580362c9 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sat, 2 Nov 2024 10:59:00 +0100 Subject: [PATCH] Do not add nullable annotations for computeIfAbsent/Present Lead to excessive nullable annotations and warnings that in practice could not happen unless the computed value was null. --- .../staticanalysis/AnnotateNullableMethods.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/openrewrite/staticanalysis/AnnotateNullableMethods.java b/src/main/java/org/openrewrite/staticanalysis/AnnotateNullableMethods.java index 94763084a..30c749a77 100644 --- a/src/main/java/org/openrewrite/staticanalysis/AnnotateNullableMethods.java +++ b/src/main/java/org/openrewrite/staticanalysis/AnnotateNullableMethods.java @@ -29,6 +29,7 @@ import java.util.Arrays; import java.util.Comparator; import java.util.List; +import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; public class AnnotateNullableMethods extends Recipe { @@ -83,13 +84,16 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl private static class FindNullableReturnStatements extends JavaIsoVisitor { private static final List KNOWN_NULLABLE_METHODS = Arrays.asList( - new MethodMatcher("java.util.Map computeIfAbsent(..)"), - new MethodMatcher("java.util.Map computeIfPresent(..)"), + // These mostly return a nullable current or previous value, which is more often null new MethodMatcher("java.util.Map get(..)"), new MethodMatcher("java.util.Map merge(..)"), new MethodMatcher("java.util.Map put(..)"), new MethodMatcher("java.util.Map putIfAbsent(..)"), + // These two return the current or computed value, which is less likely to be null in common usage + //new MethodMatcher("java.util.Map computeIfAbsent(..)"), + //new MethodMatcher("java.util.Map computeIfPresent(..)"), + new MethodMatcher("java.util.Queue poll(..)"), new MethodMatcher("java.util.Queue peek(..)"),