-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
IsInstanceLambdaUsage
check (#323)
- Loading branch information
Bastien Diederichs
authored
Nov 4, 2022
1 parent
7febccb
commit 42e632e
Showing
3 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/IsInstanceLambdaUsage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import static com.google.errorprone.BugPattern.LinkType.CUSTOM; | ||
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION; | ||
import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION; | ||
import static tech.picnic.errorprone.bugpatterns.util.Documentation.BUG_PATTERNS_BASE_URL; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import com.google.errorprone.bugpatterns.BugChecker.LambdaExpressionTreeMatcher; | ||
import com.google.errorprone.fixes.SuggestedFix; | ||
import com.google.errorprone.matchers.Description; | ||
import com.sun.source.tree.InstanceOfTree; | ||
import com.sun.source.tree.LambdaExpressionTree; | ||
import com.sun.source.tree.Tree.Kind; | ||
import tech.picnic.errorprone.bugpatterns.util.SourceCode; | ||
|
||
/** | ||
* A {@link BugChecker} that flags lambda expressions that can be replaced with a method reference | ||
* of the form {@code T.class::isInstance}. | ||
* | ||
* @see MethodReferenceUsage | ||
*/ | ||
// XXX: Consider folding this logic into the `MethodReferenceUsage` check. | ||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
summary = "Prefer `Class::isInstance` method reference over equivalent lambda expression", | ||
link = BUG_PATTERNS_BASE_URL + "IsInstanceLambdaUsage", | ||
linkType = CUSTOM, | ||
severity = SUGGESTION, | ||
tags = SIMPLIFICATION) | ||
public final class IsInstanceLambdaUsage extends BugChecker implements LambdaExpressionTreeMatcher { | ||
private static final long serialVersionUID = 1L; | ||
|
||
/** Instantiates a new {@link IsInstanceLambdaUsage} instance. */ | ||
public IsInstanceLambdaUsage() {} | ||
|
||
@Override | ||
public Description matchLambdaExpression(LambdaExpressionTree tree, VisitorState state) { | ||
if (tree.getKind() != Kind.LAMBDA_EXPRESSION || tree.getBody().getKind() != Kind.INSTANCE_OF) { | ||
return Description.NO_MATCH; | ||
} | ||
|
||
return describeMatch( | ||
tree, | ||
SuggestedFix.replace( | ||
tree, | ||
SourceCode.treeToString(((InstanceOfTree) tree.getBody()).getType(), state) | ||
+ ".class::isInstance")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...e-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/IsInstanceLambdaUsageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package tech.picnic.errorprone.bugpatterns; | ||
|
||
import com.google.errorprone.BugCheckerRefactoringTestHelper; | ||
import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode; | ||
import com.google.errorprone.CompilationTestHelper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
final class IsInstanceLambdaUsageTest { | ||
private final CompilationTestHelper compilationTestHelper = | ||
CompilationTestHelper.newInstance(IsInstanceLambdaUsage.class, getClass()); | ||
private final BugCheckerRefactoringTestHelper refactoringTestHelper = | ||
BugCheckerRefactoringTestHelper.newInstance(IsInstanceLambdaUsage.class, getClass()); | ||
|
||
@Test | ||
void identification() { | ||
compilationTestHelper | ||
.addSourceLines( | ||
"A.java", | ||
"import java.util.stream.Stream;", | ||
"", | ||
"class A {", | ||
" void m() {", | ||
" // BUG: Diagnostic contains:", | ||
" Stream.of(1).filter(i -> i instanceof Integer);", | ||
" Stream.of(2).filter(Integer.class::isInstance);", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
void replacement() { | ||
refactoringTestHelper | ||
.addInputLines( | ||
"A.java", | ||
"import java.util.stream.Stream;", | ||
"", | ||
"class A {", | ||
" void m() {", | ||
" Stream.of(1).filter(i -> i instanceof Integer);", | ||
" }", | ||
"}") | ||
.addOutputLines( | ||
"A.java", | ||
"import java.util.stream.Stream;", | ||
"", | ||
"class A {", | ||
" void m() {", | ||
" Stream.of(1).filter(Integer.class::isInstance);", | ||
" }", | ||
"}") | ||
.doTest(TestMode.TEXT_MATCH); | ||
} | ||
} |