diff --git a/check_api/src/main/java/com/google/errorprone/util/MoreAnnotations.java b/check_api/src/main/java/com/google/errorprone/util/MoreAnnotations.java index 5ea7a61acd01..6e3d2695ee53 100644 --- a/check_api/src/main/java/com/google/errorprone/util/MoreAnnotations.java +++ b/check_api/src/main/java/com/google/errorprone/util/MoreAnnotations.java @@ -30,6 +30,7 @@ import com.sun.tools.javac.code.Type; import com.sun.tools.javac.code.TypeAnnotationPosition; import com.sun.tools.javac.code.TypeTag; +import com.sun.tools.javac.tree.JCTree; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -130,6 +131,8 @@ private static boolean targetTypeMatches(Symbol sym, TypeAnnotationPosition posi case LOCAL_VARIABLE: return position.type == TargetType.LOCAL_VARIABLE; case FIELD: + // treated like a field + case ENUM_CONSTANT: return position.type == TargetType.FIELD; case CONSTRUCTOR: case METHOD: @@ -137,8 +140,16 @@ private static boolean targetTypeMatches(Symbol sym, TypeAnnotationPosition posi case PARAMETER: switch (position.type) { case METHOD_FORMAL_PARAMETER: - return ((MethodSymbol) sym.owner).getParameters().indexOf(sym) - == position.parameter_index; + int parameterIndex = position.parameter_index; + if (position.onLambda != null) { + com.sun.tools.javac.util.List lambdaParams = + position.onLambda.params; + return parameterIndex < lambdaParams.size() + && lambdaParams.get(parameterIndex).sym.equals(sym); + } else { + return ((Symbol.MethodSymbol) sym.owner).getParameters().indexOf(sym) + == parameterIndex; + } default: return false; } diff --git a/check_api/src/test/java/com/google/errorprone/util/MoreAnnotationsTest.java b/check_api/src/test/java/com/google/errorprone/util/MoreAnnotationsTest.java index 3c8bdeef03a4..0ab74fe4610a 100644 --- a/check_api/src/test/java/com/google/errorprone/util/MoreAnnotationsTest.java +++ b/check_api/src/test/java/com/google/errorprone/util/MoreAnnotationsTest.java @@ -223,4 +223,33 @@ class InnerStatic {} """) .doTest(); } + + @Test + public void explicitlyAnnotatedLambdaTest() { + CompilationTestHelper.newInstance(GetTopLevelTypeAttributesTester.class, getClass()) + .addSourceLines( + "Annos.java", + """ + import static java.lang.annotation.ElementType.TYPE_USE; + import java.lang.annotation.Target; + + @Target(TYPE_USE) + @interface A {} + """) + .addSourceLines( + "Test.java", + """ + import java.util.function.Consumer; + + class Test { + Consumer<@A String> c; + + void test() { + // BUG: Diagnostic contains: A + c = (@A String s) -> {}; + } + } + """) + .doTest(); + } }