-
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 IsRefasterAsVarargs
matcher for use by Refaster templates
#623
Merged
Stephan202
merged 4 commits into
master
from
mlrprananta/is-refaster-as-varargs-matcher
May 14, 2023
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...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,47 @@ | ||
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];", | ||
" }", | ||
"", | ||
" String[] negative2() {", | ||
" return \"foo\".split(\"o\");", | ||
" }", | ||
"", | ||
" String[] positive1() {", | ||
" // BUG: Diagnostic contains:", | ||
" return Refaster.asVarargs(\"o\");", | ||
" }", | ||
"}") | ||
.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()); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
When passing only one, we can use
#named
😄.