diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/ClassCastLambdaUsage.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/ClassCastLambdaUsage.java index 11af5823db..fb4e311a05 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/ClassCastLambdaUsage.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/ClassCastLambdaUsage.java @@ -48,7 +48,14 @@ public Description matchLambdaExpression(LambdaExpressionTree tree, VisitorState } Type type = ASTHelpers.getType(typeCast); - if (type == null || type.isParameterized()) { + if (type == null || type.isParameterized() || type.isPrimitive()) { + /* + * The method reference syntax does not support casting to parameterized types. Additionally, + * `Class#cast` does not support the same range of type conversions between (boxed) primitive + * types as the cast operator. + */ + // XXX: Depending on the declared type of the value being cast, in some cases we _can_ rewrite + // primitive casts. Add support for this. return Description.NO_MATCH; } diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/ClassCastLambdaUsageTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/ClassCastLambdaUsageTest.java index 5b445ecacf..ed5c8f3eb0 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/ClassCastLambdaUsageTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/ClassCastLambdaUsageTest.java @@ -12,6 +12,7 @@ void identification() { .addSourceLines( "A.java", "import com.google.common.collect.ImmutableSet;", + "import java.util.stream.IntStream;", "import java.util.stream.Stream;", "", "class A {", @@ -31,9 +32,10 @@ void identification() { " i -> {", " return (Integer) i;", " });", - " Stream.of(ImmutableSet.of(5)).map(l -> (ImmutableSet) l);", - " Stream.of(ImmutableSet.of(6)).map(l -> (ImmutableSet) l);", + " Stream.of(ImmutableSet.of(5)).map(s -> (ImmutableSet) s);", + " Stream.of(ImmutableSet.of(6)).map(s -> (ImmutableSet) s);", " Stream.of(7).reduce((a, b) -> (Integer) a);", + " IntStream.of(8).mapToObj(i -> (char) i);", "", " // BUG: Diagnostic contains:", " Stream.of(8).map(i -> (Integer) i);",