From 0ce5f2acd642b67a236f7f4e3a9571b4914869d3 Mon Sep 17 00:00:00 2001 From: cpovirk Date: Fri, 12 Jul 2024 08:04:52 -0700 Subject: [PATCH] In `hasAnnotation`, don't trigger completion for `NullMarked`. Completion can fail under `--release 8`, leading to `warning: unknown enum constant ElementType.MODULE`. This CL is one of a variety of ways that I'll be addressing https://github.com/google/truth/issues/1320. It alone should be sufficient (unless there are other problems that I'm unaware of), but I'll do more for people who might not upgrade Error Prone immediately, and I'll do something cleaner for the `NullArgumentForNonNullParameter` check that makes the known-problematic call to `hasAnnotation`. PiperOrigin-RevId: 651775836 --- .../java/com/google/errorprone/util/ASTHelpers.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java b/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java index 47a668aefa75..09430a8c9a71 100644 --- a/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java +++ b/check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java @@ -23,6 +23,7 @@ import static com.google.common.collect.ImmutableSet.toImmutableSet; import static com.google.common.collect.Iterables.getOnlyElement; import static com.google.common.collect.Streams.stream; +import static com.google.errorprone.VisitorState.memoize; import static com.google.errorprone.matchers.JUnitMatchers.JUNIT4_RUN_WITH_ANNOTATION; import static com.google.errorprone.matchers.Matchers.isSubtypeOf; import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE; @@ -913,6 +914,14 @@ private static boolean isInherited(VisitorState state, Name annotationName) { .get( annotationName, name -> { + if (name.equals(NULL_MARKED_NAME.get(state))) { + /* + * We avoid searching for @Inherited on NullMarked not just because we already know + * the answer but also because the search would cause issues under --release 8 on + * account of NullMarked's use of @Target(MODULE, ...). + */ + return false; + } Symbol annotationSym = state.getSymbolFromName(annotationName); if (annotationSym == null) { return false; @@ -2830,5 +2839,8 @@ public static Stream getCaseExpressions(CaseTree caseT } } + private static final Supplier NULL_MARKED_NAME = + memoize(state -> state.getName("org.jspecify.annotations.NullMarked")); + private ASTHelpers() {} }