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

In NullArgumentForNonNullParameter, don't trigger completion for NullMarked. #4462

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import static com.google.errorprone.util.ASTHelpers.enclosingClass;
import static com.google.errorprone.util.ASTHelpers.enclosingPackage;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.ASTHelpers.hasAnnotation;
import static java.util.Arrays.stream;
import static javax.lang.model.type.TypeKind.TYPEVAR;

Expand Down Expand Up @@ -70,6 +69,10 @@ public final class NullArgumentForNonNullParameter extends BugChecker
memoize(state -> state.getName("com.google.common.collect.Immutable"));
private static final Supplier<Name> GUAVA_GRAPH_IMMUTABLE_PREFIX =
memoize(state -> state.getName("com.google.common.graph.Immutable"));
private static final Supplier<Name> PROTO_NONNULL_API_NAME =
memoize(state -> state.getName("com.google.protobuf.Internal$ProtoNonnullApi"));
private static final Supplier<Name> NULL_MARKED_NAME =
memoize(state -> state.getName("org.jspecify.annotations.NullMarked"));
private static final Supplier<ImmutableSet<Name>> NULL_MARKED_PACKAGES_WE_TRUST =
memoize(
state ->
Expand Down Expand Up @@ -245,17 +248,29 @@ private static boolean isParameterOfMethodOnTypeStartingWith(
private boolean enclosingAnnotationDefaultsNonTypeVariablesToNonNull(
Symbol sym, VisitorState state) {
for (; sym != null; sym = sym.getEnclosingElement()) {
if (hasAnnotation(sym, "com.google.protobuf.Internal$ProtoNonnullApi", state)) {
if (hasDirectAnnotation(sym, PROTO_NONNULL_API_NAME.get(state))) {
return true;
}
if (hasAnnotation(sym, "org.jspecify.annotations.NullMarked", state)
if (hasDirectAnnotation(sym, NULL_MARKED_NAME.get(state))
&& weTrustNullMarkedOn(sym, state)) {
return true;
}
}
return false;
}

/*
* ASTHelpers has hasAnnotation and hasDirectAnnotationWithSimpleName but I think not this.
*
* We avoid hasAnnotation not just because it's unnecessary but also because it would cause issues
* under --release 8 on account of NullMarked's use of @Target(MODULE, ...).
*/
private static boolean hasDirectAnnotation(Symbol sym, Name name) {
return sym.getAnnotationMirrors().stream()
.anyMatch(
a -> ((Symbol) a.getAnnotationType().asElement()).getQualifiedName().equals(name));
}

private boolean weTrustNullMarkedOn(Symbol sym, VisitorState state) {
/*
* Similar to @NonNull (discussed above), the "default to non-null" annotation @NullMarked is
Expand Down
Loading