diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/NonCanonicalType.java b/core/src/main/java/com/google/errorprone/bugpatterns/NonCanonicalType.java index a27fb236b60..d679e69b4ba 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/NonCanonicalType.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/NonCanonicalType.java @@ -29,6 +29,7 @@ import com.google.errorprone.fixes.SuggestedFix; import com.google.errorprone.matchers.Description; import com.google.errorprone.util.Visibility; +import com.sun.source.tree.AnnotatedTypeTree; import com.sun.source.tree.MemberSelectTree; import com.sun.source.tree.ParameterizedTypeTree; import com.sun.source.tree.Tree; @@ -135,6 +136,8 @@ private static String getNonCanonicalName(Tree tree) { + memberSelectTree.getIdentifier(); case PARAMETERIZED_TYPE: return getNonCanonicalName(((ParameterizedTypeTree) tree).getType()); + case ANNOTATED_TYPE: + return getNonCanonicalName(((AnnotatedTypeTree) tree).getUnderlyingType()); default: throw new AssertionError(tree.getKind()); } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/NonCanonicalTypeTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/NonCanonicalTypeTest.java index 23d1d6e8ab6..5fb518155f0 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/NonCanonicalTypeTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/NonCanonicalTypeTest.java @@ -254,4 +254,27 @@ public void moduleInfo() { "}") .doTest(); } + + // https://github.com/google/error-prone/issues/4343 + @Test + public void typeAnnotation() { + compilationHelper + .addSourceLines( + "Crash.java", + "import java.lang.annotation.ElementType;", + "import java.lang.annotation.Target;", + " @Target(ElementType.TYPE_USE)", + " @interface TA {}", + " class Crash {", + " class Nested {", + " class DoublyNested {}", + " }", + " class SubNested extends Nested {", + " }", + " void foo(Crash.@TA Nested.DoublyNested p) {}", + " // BUG: Diagnostic contains: Crash.Nested.DoublyNested", + " void bar(Crash.@TA SubNested.DoublyNested p) {}", + " }") + .doTest(); + } }