Skip to content

Commit

Permalink
Ignore primitive casts in ClassCastLambdaUsage check (#1385)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedsamehsalah authored Oct 30, 2024
1 parent 9940576 commit ce6931c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {",
Expand All @@ -31,9 +32,10 @@ void identification() {
" i -> {",
" return (Integer) i;",
" });",
" Stream.<ImmutableSet>of(ImmutableSet.of(5)).map(l -> (ImmutableSet<Number>) l);",
" Stream.of(ImmutableSet.of(6)).map(l -> (ImmutableSet<?>) l);",
" Stream.<ImmutableSet>of(ImmutableSet.of(5)).map(s -> (ImmutableSet<Number>) 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);",
Expand Down

0 comments on commit ce6931c

Please sign in to comment.