Skip to content

Commit c697b31

Browse files
cushonError Prone Team
authored andcommitted
Update MultipleNullnessAnnotations to print the qualified names of the conflicting nullness annotations
PiperOrigin-RevId: 786842535
1 parent a96d1d9 commit c697b31

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

check_api/src/main/java/com/google/errorprone/dataflow/nullnesspropagation/NullnessAnnotations.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,20 @@ public static Optional<Nullness> fromAnnotationMirrors(
7474

7575
public static boolean annotationsAreAmbiguous(
7676
Collection<? extends AnnotationMirror> annotations) {
77-
return annotations.stream()
78-
.map(a -> simpleName(a).toString())
79-
.filter(ANNOTATION_RELEVANT_TO_NULLNESS)
80-
.map(NULLABLE_ANNOTATION::test)
77+
return annotationsRelevantToNullness(annotations).stream()
78+
.map(a -> NULLABLE_ANNOTATION.test(simpleName(a).toString()))
8179
.distinct()
8280
.count()
8381
== 2;
8482
}
8583

84+
public static ImmutableList<AnnotationMirror> annotationsRelevantToNullness(
85+
Collection<? extends AnnotationMirror> annotations) {
86+
return annotations.stream()
87+
.filter(a -> ANNOTATION_RELEVANT_TO_NULLNESS.test(simpleName(a).toString()))
88+
.collect(toImmutableList());
89+
}
90+
8691
public static ImmutableList<AnnotationTree> annotationsRelevantToNullness(
8792
List<? extends AnnotationTree> annotations) {
8893
return annotations.stream()

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
package com.google.errorprone.bugpatterns.nullness;
1818

1919
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
20+
import static com.google.errorprone.dataflow.nullnesspropagation.NullnessAnnotations.annotationsAreAmbiguous;
21+
import static com.google.errorprone.dataflow.nullnesspropagation.NullnessAnnotations.annotationsRelevantToNullness;
2022
import static com.google.errorprone.matchers.Description.NO_MATCH;
2123
import static com.google.errorprone.util.ASTHelpers.getSymbol;
2224
import static com.google.errorprone.util.ASTHelpers.getType;
25+
import static java.util.stream.Collectors.joining;
2326

2427
import com.google.common.collect.ImmutableSet;
2528
import com.google.errorprone.BugPattern;
@@ -28,7 +31,6 @@
2831
import com.google.errorprone.bugpatterns.BugChecker.AnnotatedTypeTreeMatcher;
2932
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher;
3033
import com.google.errorprone.bugpatterns.BugChecker.VariableTreeMatcher;
31-
import com.google.errorprone.dataflow.nullnesspropagation.NullnessAnnotations;
3234
import com.google.errorprone.matchers.Description;
3335
import com.sun.source.tree.AnnotatedTypeTree;
3436
import com.sun.source.tree.MethodTree;
@@ -75,9 +77,14 @@ private Description match(Tree tree, Symbol symbol, Type type) {
7577
}
7678

7779
private Description match(Tree tree, Collection<? extends AnnotationMirror> annotations) {
78-
if (NullnessAnnotations.annotationsAreAmbiguous(annotations)) {
79-
return describeMatch(tree);
80+
if (!annotationsAreAmbiguous(annotations)) {
81+
return NO_MATCH;
8082
}
81-
return NO_MATCH;
83+
return buildDescription(tree)
84+
.setMessage(
85+
annotationsRelevantToNullness(annotations).stream()
86+
.map(a -> a.getAnnotationType().asElement().toString())
87+
.collect(joining(", ", "This type use has conflicting nullness annotations: ", "")))
88+
.build();
8289
}
8390
}

0 commit comments

Comments
 (0)