Skip to content

Commit

Permalink
Do not add nullable annotations for computeIfAbsent/Present
Browse files Browse the repository at this point in the history
Lead to excessive nullable annotations and warnings that in practice could not happen unless the computed value was null.
  • Loading branch information
timtebeek committed Nov 2, 2024
1 parent 6225225 commit 80cb672
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -83,13 +84,16 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDecl
private static class FindNullableReturnStatements extends JavaIsoVisitor<AtomicBoolean> {

private static final List<MethodMatcher> 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(..)"),

Expand Down

0 comments on commit 80cb672

Please sign in to comment.