Skip to content

Commit 77e4f72

Browse files
cpovirkError Prone Team
authored andcommitted
In NullArgumentForNonNullParameter, 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 google/truth#1320. It alone should be sufficient (unless there are other problems that I'm unaware of), but I'll do more, both for people who might not upgrade Error Prone immediately and for anyone else (NullAway?) who writes a check that calls `hasAnnotation(nullMarked)`. PiperOrigin-RevId: 651801963
1 parent c41ee92 commit 77e4f72

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

core/src/main/java/com/google/errorprone/bugpatterns/nullness/NullArgumentForNonNullParameter.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import static com.google.errorprone.util.ASTHelpers.enclosingClass;
2929
import static com.google.errorprone.util.ASTHelpers.enclosingPackage;
3030
import static com.google.errorprone.util.ASTHelpers.getSymbol;
31-
import static com.google.errorprone.util.ASTHelpers.hasAnnotation;
3231
import static java.util.Arrays.stream;
3332
import static javax.lang.model.type.TypeKind.TYPEVAR;
3433

@@ -70,6 +69,10 @@ public final class NullArgumentForNonNullParameter extends BugChecker
7069
memoize(state -> state.getName("com.google.common.collect.Immutable"));
7170
private static final Supplier<Name> GUAVA_GRAPH_IMMUTABLE_PREFIX =
7271
memoize(state -> state.getName("com.google.common.graph.Immutable"));
72+
private static final Supplier<Name> PROTO_NONNULL_API_NAME =
73+
memoize(state -> state.getName("com.google.protobuf.Internal$ProtoNonnullApi"));
74+
private static final Supplier<Name> NULL_MARKED_NAME =
75+
memoize(state -> state.getName("org.jspecify.annotations.NullMarked"));
7376
private static final Supplier<ImmutableSet<Name>> NULL_MARKED_PACKAGES_WE_TRUST =
7477
memoize(
7578
state ->
@@ -245,17 +248,29 @@ private static boolean isParameterOfMethodOnTypeStartingWith(
245248
private boolean enclosingAnnotationDefaultsNonTypeVariablesToNonNull(
246249
Symbol sym, VisitorState state) {
247250
for (; sym != null; sym = sym.getEnclosingElement()) {
248-
if (hasAnnotation(sym, "com.google.protobuf.Internal$ProtoNonnullApi", state)) {
251+
if (hasDirectAnnotation(sym, PROTO_NONNULL_API_NAME.get(state))) {
249252
return true;
250253
}
251-
if (hasAnnotation(sym, "org.jspecify.annotations.NullMarked", state)
254+
if (hasDirectAnnotation(sym, NULL_MARKED_NAME.get(state))
252255
&& weTrustNullMarkedOn(sym, state)) {
253256
return true;
254257
}
255258
}
256259
return false;
257260
}
258261

262+
/*
263+
* ASTHelpers has hasAnnotation and hasDirectAnnotationWithSimpleName but I think not this.
264+
*
265+
* We avoid hasAnnotation not just because it's unnecessary but also because it would cause issues
266+
* under --release 8 on account of NullMarked's use of @Target(MODULE, ...).
267+
*/
268+
private static boolean hasDirectAnnotation(Symbol sym, Name name) {
269+
return sym.getAnnotationMirrors().stream()
270+
.anyMatch(
271+
a -> ((Symbol) a.getAnnotationType().asElement()).getQualifiedName().equals(name));
272+
}
273+
259274
private boolean weTrustNullMarkedOn(Symbol sym, VisitorState state) {
260275
/*
261276
* Similar to @NonNull (discussed above), the "default to non-null" annotation @NullMarked is

0 commit comments

Comments
 (0)