-
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 AssociativeMethodInvocation
check
#560
Merged
Merged
Changes from all commits
Commits
Show all changes
5 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
96 changes: 96 additions & 0 deletions
96
...contrib/src/main/java/tech/picnic/errorprone/bugpatterns/AssociativeMethodInvocation.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,96 @@ | ||
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 com.google.errorprone.matchers.Matchers.allOf; | ||
import static com.google.errorprone.matchers.Matchers.not; | ||
import static com.google.errorprone.matchers.Matchers.staticMethod; | ||
import static com.google.errorprone.matchers.Matchers.toType; | ||
import static tech.picnic.errorprone.bugpatterns.util.Documentation.BUG_PATTERNS_BASE_URL; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.google.common.collect.ImmutableSet; | ||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; | ||
import com.google.errorprone.fixes.SuggestedFix; | ||
import com.google.errorprone.fixes.SuggestedFixes; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.google.errorprone.matchers.Matchers; | ||
import com.google.errorprone.refaster.Refaster; | ||
import com.google.errorprone.suppliers.Supplier; | ||
import com.google.errorprone.suppliers.Suppliers; | ||
import com.google.errorprone.util.ASTHelpers; | ||
import com.sun.source.tree.ExpressionTree; | ||
import com.sun.source.tree.MethodInvocationTree; | ||
import com.sun.tools.javac.code.Type; | ||
import tech.picnic.errorprone.bugpatterns.util.SourceCode; | ||
|
||
/** | ||
* A {@link BugChecker} that flags unnecessarily nested usage of methods that implement an | ||
* associative operation. | ||
* | ||
* <p>The arguments to such methods can be flattened without affecting semantics, while making the | ||
* code more readable. | ||
*/ | ||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
summary = | ||
"These methods implement an associative operation, so the list of operands can be flattened", | ||
link = BUG_PATTERNS_BASE_URL + "AssociativeMethodInvocation", | ||
linkType = CUSTOM, | ||
severity = SUGGESTION, | ||
tags = SIMPLIFICATION) | ||
public final class AssociativeMethodInvocation extends BugChecker | ||
implements MethodInvocationTreeMatcher { | ||
private static final long serialVersionUID = 1L; | ||
private static final Supplier<Type> ITERABLE = Suppliers.typeFromClass(Iterable.class); | ||
private static final ImmutableSet<Matcher<ExpressionTree>> ASSOCIATIVE_OPERATIONS = | ||
ImmutableSet.of( | ||
allOf( | ||
staticMethod().onClass(Suppliers.typeFromClass(Matchers.class)).named("allOf"), | ||
toType(MethodInvocationTree.class, not(hasArgumentOfType(ITERABLE)))), | ||
allOf( | ||
staticMethod().onClass(Suppliers.typeFromClass(Matchers.class)).named("anyOf"), | ||
toType(MethodInvocationTree.class, not(hasArgumentOfType(ITERABLE)))), | ||
staticMethod().onClass(Suppliers.typeFromClass(Refaster.class)).named("anyOf")); | ||
|
||
/** Instantiates a new {@link AssociativeMethodInvocation} instance. */ | ||
public AssociativeMethodInvocation() {} | ||
|
||
@Override | ||
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { | ||
if (tree.getArguments().isEmpty()) { | ||
/* Absent any arguments, there is nothing to simplify. */ | ||
return Description.NO_MATCH; | ||
} | ||
|
||
for (Matcher<ExpressionTree> matcher : ASSOCIATIVE_OPERATIONS) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we start with the following if statement for fast return (tests still pass)?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pitest will flag it, but I agree 👍 |
||
if (matcher.matches(tree, state)) { | ||
SuggestedFix.Builder fix = SuggestedFix.builder(); | ||
for (ExpressionTree arg : tree.getArguments()) { | ||
if (matcher.matches(arg, state)) { | ||
MethodInvocationTree invocation = (MethodInvocationTree) arg; | ||
fix.merge( | ||
invocation.getArguments().isEmpty() | ||
? SuggestedFixes.removeElement(arg, tree.getArguments(), state) | ||
: SourceCode.unwrapMethodInvocation(invocation, state)); | ||
} | ||
} | ||
|
||
return fix.isEmpty() ? Description.NO_MATCH : describeMatch(tree, fix.build()); | ||
} | ||
rickie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return Description.NO_MATCH; | ||
} | ||
|
||
private static Matcher<MethodInvocationTree> hasArgumentOfType(Supplier<Type> type) { | ||
return (tree, state) -> | ||
tree.getArguments().stream() | ||
.anyMatch(arg -> ASTHelpers.isSubtype(ASTHelpers.getType(arg), type.get(state), state)); | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
...rib/src/test/java/tech/picnic/errorprone/bugpatterns/AssociativeMethodInvocationTest.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,110 @@ | ||
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 AssociativeMethodInvocationTest { | ||
@Test | ||
void identification() { | ||
CompilationTestHelper.newInstance(AssociativeMethodInvocation.class, getClass()) | ||
.addSourceLines( | ||
"A.java", | ||
"import com.google.common.collect.ImmutableList;", | ||
"import com.google.errorprone.matchers.Matchers;", | ||
"import com.google.errorprone.refaster.Refaster;", | ||
"", | ||
"class A {", | ||
" void m() {", | ||
" Matchers.allOf();", | ||
" Matchers.anyOf();", | ||
" Refaster.anyOf();", | ||
"", | ||
" Matchers.allOf((t, s) -> true);", | ||
" Matchers.anyOf((t, s) -> true);", | ||
" Refaster.anyOf(0);", | ||
"", | ||
" Matchers.allOf(Matchers.anyOf((t, s) -> true));", | ||
" Matchers.anyOf(Matchers.allOf((t, s) -> true));", | ||
" Refaster.anyOf(Matchers.allOf((t, s) -> true));", | ||
"", | ||
" // BUG: Diagnostic contains:", | ||
" Matchers.allOf(Matchers.allOf((t, s) -> true));", | ||
" // BUG: Diagnostic contains:", | ||
" Matchers.anyOf(Matchers.anyOf((t, s) -> true));", | ||
" // BUG: Diagnostic contains:", | ||
" Refaster.anyOf(Refaster.anyOf(0));", | ||
"", | ||
" Matchers.allOf(Matchers.allOf(ImmutableList.of((t, s) -> true)));", | ||
" Matchers.anyOf(Matchers.anyOf(ImmutableList.of((t, s) -> true)));", | ||
"", | ||
" // BUG: Diagnostic contains:", | ||
" Matchers.allOf(", | ||
" (t, s) -> true, Matchers.allOf((t, s) -> false, (t, s) -> true), (t, s) -> false);", | ||
" // BUG: Diagnostic contains:", | ||
" Matchers.anyOf(", | ||
" (t, s) -> true, Matchers.anyOf((t, s) -> false, (t, s) -> true), (t, s) -> false);", | ||
" // BUG: Diagnostic contains:", | ||
" Refaster.anyOf(0, Refaster.anyOf(1, 2), 3);", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
void replacement() { | ||
BugCheckerRefactoringTestHelper.newInstance(AssociativeMethodInvocation.class, getClass()) | ||
.addInputLines( | ||
"A.java", | ||
"import com.google.errorprone.matchers.Matchers;", | ||
"import com.google.errorprone.refaster.Refaster;", | ||
"", | ||
"class A {", | ||
" void m() {", | ||
" Matchers.allOf(Matchers.allOf());", | ||
" Matchers.anyOf(Matchers.anyOf());", | ||
" Refaster.anyOf(Refaster.anyOf());", | ||
"", | ||
" Matchers.allOf(Matchers.allOf((t, s) -> true));", | ||
" Matchers.anyOf(Matchers.anyOf((t, s) -> true));", | ||
" Refaster.anyOf(Refaster.anyOf(0));", | ||
"", | ||
" Matchers.allOf(", | ||
" Matchers.anyOf(),", | ||
" Matchers.allOf((t, s) -> false, (t, s) -> true),", | ||
" Matchers.allOf(),", | ||
" Matchers.anyOf((t, s) -> false));", | ||
" Matchers.anyOf(", | ||
" Matchers.allOf(),", | ||
" Matchers.anyOf((t, s) -> false, (t, s) -> true),", | ||
" Matchers.anyOf(),", | ||
" Matchers.allOf((t, s) -> false));", | ||
" Refaster.anyOf(Matchers.allOf(), Refaster.anyOf(1, 2), Matchers.anyOf());", | ||
" }", | ||
"}") | ||
.addOutputLines( | ||
"A.java", | ||
"import com.google.errorprone.matchers.Matchers;", | ||
"import com.google.errorprone.refaster.Refaster;", | ||
"", | ||
"class A {", | ||
" void m() {", | ||
" Matchers.allOf();", | ||
" Matchers.anyOf();", | ||
" Refaster.anyOf();", | ||
"", | ||
" Matchers.allOf((t, s) -> true);", | ||
" Matchers.anyOf((t, s) -> true);", | ||
" Refaster.anyOf(0);", | ||
"", | ||
" Matchers.allOf(", | ||
" Matchers.anyOf(), (t, s) -> false, (t, s) -> true, Matchers.anyOf((t, s) -> false));", | ||
" Matchers.anyOf(", | ||
" Matchers.allOf(), (t, s) -> false, (t, s) -> true, Matchers.allOf((t, s) -> false));", | ||
" Refaster.anyOf(Matchers.allOf(), 1, 2, Matchers.anyOf());", | ||
" }", | ||
"}") | ||
.doTest(TestMode.TEXT_MATCH); | ||
} | ||
} |
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.
Why is it "to" in this context, that doesn't sound right 🤔.
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.
I know that both forms are commonly used (with "of" being more prevalent), but IMHO "to" is better here: we're passing arguments to a method, and they aren't "owned" by the method.