From 438a0b36b3ff9cdfb1047dbff662df52a26de257 Mon Sep 17 00:00:00 2001 From: mohamedsamehsalah Date: Wed, 30 Oct 2024 10:44:02 +0100 Subject: [PATCH 1/2] Exclude primitives in `ClassCastLambdaUsage` check --- .../picnic/errorprone/bugpatterns/ClassCastLambdaUsage.java | 2 +- .../picnic/errorprone/bugpatterns/ClassCastLambdaUsageTest.java | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) 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..ce83ed0bda 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,7 @@ public Description matchLambdaExpression(LambdaExpressionTree tree, VisitorState } Type type = ASTHelpers.getType(typeCast); - if (type == null || type.isParameterized()) { + if (type == null || type.isParameterized() || type.isPrimitive()) { 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..5e210d47c8 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 {", @@ -34,6 +35,7 @@ void identification() { " Stream.of(ImmutableSet.of(5)).map(l -> (ImmutableSet) l);", " Stream.of(ImmutableSet.of(6)).map(l -> (ImmutableSet) l);", " Stream.of(7).reduce((a, b) -> (Integer) a);", + " IntStream.of(8).mapToObj(a -> (char) a);", "", " // BUG: Diagnostic contains:", " Stream.of(8).map(i -> (Integer) i);", From 5754634a8f0346d2770f3aa071c3a25b48c4b026 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Wed, 30 Oct 2024 11:50:36 +0100 Subject: [PATCH 2/2] Suggestions --- .../errorprone/bugpatterns/ClassCastLambdaUsage.java | 7 +++++++ .../errorprone/bugpatterns/ClassCastLambdaUsageTest.java | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-) 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 ce83ed0bda..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 @@ -49,6 +49,13 @@ public Description matchLambdaExpression(LambdaExpressionTree tree, VisitorState Type type = ASTHelpers.getType(typeCast); 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 5e210d47c8..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 @@ -32,10 +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(a -> (char) a);", + " IntStream.of(8).mapToObj(i -> (char) i);", "", " // BUG: Diagnostic contains:", " Stream.of(8).map(i -> (Integer) i);",