-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce ClassCastLambdaUsage
check
#1381
Conversation
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
ClassLiteralCast
bug checkerClassCastLambdaUsage
check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR is an alternative to #1373.
Added a commit and updated the suggested commit message. Tnx @mohamedsamehsalah!
linkType = CUSTOM, | ||
severity = SUGGESTION, | ||
tags = SIMPLIFICATION) | ||
public final class ClassCast extends BugChecker implements LambdaExpressionTreeMatcher { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This name is a bit too generic: it's really about preferring the Class::cast
method reference. Given the precedence set by IsInstanceLambdaUsage
, we could name this check ClassCastLambdaUsage
. (Perhaps a better name is ClassCastMethodReference
, but let's stay consistent.)
|
||
/** | ||
* A {@link BugChecker} that flags lambda expressions that can be replaced with a method reference | ||
* of the form {@code T.class::cast}, if applicable. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those last words are implied by the word "can".
* of the form {@code T.class::cast}, if applicable. | |
* of the form {@code T.class::cast}. |
" Number foo = 0;", | ||
" Number bar = 1;", | ||
"", | ||
" // BUG: Diagnostic contains:", | ||
" Optional.of(foo).map(i -> (Integer) i);", | ||
"", | ||
" Optional.of(foo).map(i -> 2);", | ||
" Optional.of(foo).map(i -> (Integer) 2);", | ||
" Optional.of(foo).map(i -> bar);", | ||
" Optional.of(foo).map(i -> (Integer) bar);", | ||
"", | ||
" ImmutableList.of(Set.of(foo)).stream().map(l -> (ImmutableSet<Number>) l);", | ||
" ImmutableList.of(Set.of(foo)).stream().map(l -> (ImmutableSet<?>) l);", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would have been nice to stick a bit closer to the test cases in IsInstanceLambdaUsageTest
. W.r.t. the ImmutableSet<Number>
case: let's more explicitly generate a stream of raw type elements.
private static boolean isGenericCastExpression(String expression) { | ||
return expression.contains("<") && expression.contains(">"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't be checking the source code like this, but instead ask the type system 😬.
Looks good. All 10 mutations in this change were killed.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work @mohamedsamehsalah @Stephan202 🚀 !
259948b
to
0d45c3a
Compare
Quality Gate passedIssues Measures |
Looks good. All 10 mutations in this change were killed.
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
Suggested commit message