-
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
IsRefasterAsVarargs
matcher
- Loading branch information
1 parent
666fe0d
commit 2343585
Showing
4 changed files
with
78 additions
and
6 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
23 changes: 23 additions & 0 deletions
23
...r-support/src/main/java/tech/picnic/errorprone/refaster/matchers/IsRefasterAsVarargs.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,23 @@ | ||
package tech.picnic.errorprone.refaster.matchers; | ||
|
||
import static com.google.errorprone.matchers.Matchers.staticMethod; | ||
|
||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.google.errorprone.refaster.Refaster; | ||
import com.sun.source.tree.ExpressionTree; | ||
|
||
/** A matcher of {@link Refaster#asVarargs} method invocations. */ | ||
public final class IsRefasterAsVarargs implements Matcher<ExpressionTree> { | ||
private static final long serialVersionUID = 1L; | ||
private static final Matcher<ExpressionTree> DELEGATE = | ||
staticMethod().onClass(Refaster.class.getName()).namedAnyOf("asVarargs"); | ||
|
||
/** Instantiates a new {@link IsRefasterAsVarargs} instance. */ | ||
public IsRefasterAsVarargs() {} | ||
|
||
@Override | ||
public boolean matches(ExpressionTree expressionTree, VisitorState state) { | ||
return DELEGATE.matches(expressionTree, state); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...pport/src/test/java/tech/picnic/errorprone/refaster/matchers/IsRefasterAsVarargsTest.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,52 @@ | ||
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 IsRefasterAsVarargsTest { | ||
@Test | ||
void matches() { | ||
CompilationTestHelper.newInstance(MatcherTestChecker.class, getClass()) | ||
.addSourceLines( | ||
"A.java", | ||
"import com.google.errorprone.refaster.Refaster;", | ||
"", | ||
"class A {", | ||
" int[] negative1() {", | ||
" return new int[4];", | ||
" }", | ||
"", | ||
" static String[] util() {", | ||
" return new String[5];", | ||
" }", | ||
"", | ||
" String[] negative2() {", | ||
" return A.util();", | ||
" }", | ||
"", | ||
" String[] positive1() {", | ||
" String a = \"a\";", | ||
" // BUG: Diagnostic contains:", | ||
" return Refaster.asVarargs(a);", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
/** A {@link BugChecker} that simply delegates to {@link IsRefasterAsVarargs}. */ | ||
@BugPattern(summary = "Flags expressions matched by `IsRefasterAsVarargs`", 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 IsRefasterAsVarargs()); | ||
} | ||
} | ||
} |