-
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
StreamMapTo{Double,Int,Long}Sum
Refaster rules (#497)
As well as a new `IsLambdaExpressionOrMethodReference` matcher.
- Loading branch information
1 parent
fd6a45e
commit 5bb1dd1
Showing
5 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
20 changes: 20 additions & 0 deletions
20
...in/java/tech/picnic/errorprone/refaster/matchers/IsLambdaExpressionOrMethodReference.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,20 @@ | ||
package tech.picnic.errorprone.refaster.matchers; | ||
|
||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.sun.source.tree.ExpressionTree; | ||
import com.sun.source.tree.LambdaExpressionTree; | ||
import com.sun.source.tree.MemberReferenceTree; | ||
|
||
/** A matcher of lambda expressions and method references. */ | ||
public final class IsLambdaExpressionOrMethodReference implements Matcher<ExpressionTree> { | ||
private static final long serialVersionUID = 1L; | ||
|
||
/** Instantiates a new {@link IsLambdaExpressionOrMethodReference} instance. */ | ||
public IsLambdaExpressionOrMethodReference() {} | ||
|
||
@Override | ||
public boolean matches(ExpressionTree tree, VisitorState state) { | ||
return tree instanceof LambdaExpressionTree || tree instanceof MemberReferenceTree; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...ava/tech/picnic/errorprone/refaster/matchers/IsLambdaExpressionOrMethodReferenceTest.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,72 @@ | ||
package tech.picnic.errorprone.refaster.matchers; | ||
|
||
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; | ||
|
||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.CompilationTestHelper; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import org.junit.jupiter.api.Test; | ||
|
||
final class IsLambdaExpressionOrMethodReferenceTest { | ||
@Test | ||
void matches() { | ||
CompilationTestHelper.newInstance(MatcherTestChecker.class, getClass()) | ||
.addSourceLines( | ||
"A.java", | ||
"import com.google.common.base.Predicates;", | ||
"import java.util.function.Function;", | ||
"import java.util.function.Predicate;", | ||
"", | ||
"class A {", | ||
" boolean negative1() {", | ||
" return true;", | ||
" }", | ||
"", | ||
" String negative2() {", | ||
" return new String(new byte[0]);", | ||
" }", | ||
"", | ||
" Predicate<String> negative3() {", | ||
" return Predicates.alwaysTrue();", | ||
" }", | ||
"", | ||
" Predicate<String> positive1() {", | ||
" // BUG: Diagnostic contains:", | ||
" return str -> true;", | ||
" }", | ||
"", | ||
" Predicate<String> positive2() {", | ||
" // BUG: Diagnostic contains:", | ||
" return str -> {", | ||
" return true;", | ||
" };", | ||
" }", | ||
"", | ||
" Predicate<String> positive3() {", | ||
" // BUG: Diagnostic contains:", | ||
" return String::isEmpty;", | ||
" }", | ||
"", | ||
" Function<byte[], String> positive4() {", | ||
" // BUG: Diagnostic contains:", | ||
" return String::new;", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
/** A {@link BugChecker} that simply delegates to {@link IsLambdaExpressionOrMethodReference}. */ | ||
@BugPattern( | ||
summary = "Flags expressions matched by `IsLambdaExpressionOrMethodReference`", | ||
severity = ERROR) | ||
public static final class MatcherTestChecker extends AbstractMatcherTestChecker { | ||
private static final long serialVersionUID = 1L; | ||
|
||
// XXX: This is a false positive reported by Checkstyle. See | ||
// https://github.com/checkstyle/checkstyle/issues/10161#issuecomment-1242732120. | ||
@SuppressWarnings("RedundantModifier") | ||
public MatcherTestChecker() { | ||
super(new IsLambdaExpressionOrMethodReference()); | ||
} | ||
} | ||
} |