-
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.
- Loading branch information
1 parent
8a0abf5
commit 73cf28e
Showing
5 changed files
with
467 additions
and
16 deletions.
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/DirectReturn.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,131 @@ | ||
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.argument; | ||
import static com.google.errorprone.matchers.Matchers.isSameType; | ||
import static com.google.errorprone.matchers.Matchers.isVariable; | ||
import static com.google.errorprone.matchers.Matchers.not; | ||
import static com.google.errorprone.matchers.Matchers.returnStatement; | ||
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.errorprone.BugPattern; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import com.google.errorprone.bugpatterns.BugChecker.BlockTreeMatcher; | ||
import com.google.errorprone.fixes.SuggestedFix; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.google.errorprone.util.ASTHelpers; | ||
import com.sun.source.tree.AssignmentTree; | ||
import com.sun.source.tree.BlockTree; | ||
import com.sun.source.tree.ExpressionStatementTree; | ||
import com.sun.source.tree.ExpressionTree; | ||
import com.sun.source.tree.MethodInvocationTree; | ||
import com.sun.source.tree.ReturnTree; | ||
import com.sun.source.tree.StatementTree; | ||
import com.sun.source.tree.Tree; | ||
import com.sun.source.tree.VariableTree; | ||
import com.sun.tools.javac.code.Symbol; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import tech.picnic.errorprone.bugpatterns.util.MoreASTHelpers; | ||
import tech.picnic.errorprone.bugpatterns.util.SourceCode; | ||
|
||
/** | ||
* A {@link BugChecker} that flags unnecessary local variable assignments preceding a return | ||
* statement. | ||
*/ | ||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
summary = "Variable assignment is redundant; value can be returned directly", | ||
link = BUG_PATTERNS_BASE_URL + "DirectReturn", | ||
linkType = CUSTOM, | ||
severity = SUGGESTION, | ||
tags = SIMPLIFICATION) | ||
public final class DirectReturn extends BugChecker implements BlockTreeMatcher { | ||
private static final long serialVersionUID = 1L; | ||
private static final Matcher<StatementTree> VARIABLE_RETURN = returnStatement(isVariable()); | ||
private static final Matcher<ExpressionTree> MOCKITO_MOCK_OR_SPY_WITH_IMPLICIT_TYPE = | ||
allOf( | ||
not(toType(MethodInvocationTree.class, argument(0, isSameType(Class.class.getName())))), | ||
staticMethod().onClass("org.mockito.Mockito").namedAnyOf("mock", "spy")); | ||
|
||
/** Instantiates a new {@link DirectReturn} instance. */ | ||
public DirectReturn() {} | ||
|
||
@Override | ||
public Description matchBlock(BlockTree tree, VisitorState state) { | ||
List<? extends StatementTree> statements = tree.getStatements(); | ||
if (statements.size() < 2) { | ||
return Description.NO_MATCH; | ||
} | ||
|
||
StatementTree finalStatement = statements.get(statements.size() - 1); | ||
if (!VARIABLE_RETURN.matches(finalStatement, state)) { | ||
return Description.NO_MATCH; | ||
} | ||
|
||
Symbol variableSymbol = ASTHelpers.getSymbol(((ReturnTree) finalStatement).getExpression()); | ||
StatementTree precedingStatement = statements.get(statements.size() - 2); | ||
|
||
return tryMatchAssignment(variableSymbol, precedingStatement) | ||
.filter(resultExpr -> canInlineToReturnStatement(resultExpr, state)) | ||
.map( | ||
resultExpr -> | ||
describeMatch( | ||
precedingStatement, | ||
SuggestedFix.builder() | ||
.replace( | ||
precedingStatement, | ||
String.format("return %s;", SourceCode.treeToString(resultExpr, state))) | ||
.delete(finalStatement) | ||
.build())) | ||
.orElse(Description.NO_MATCH); | ||
} | ||
|
||
private static Optional<ExpressionTree> tryMatchAssignment(Symbol targetSymbol, Tree tree) { | ||
if (tree instanceof ExpressionStatementTree) { | ||
return tryMatchAssignment(targetSymbol, ((ExpressionStatementTree) tree).getExpression()); | ||
} | ||
|
||
if (tree instanceof AssignmentTree) { | ||
AssignmentTree assignment = (AssignmentTree) tree; | ||
return targetSymbol.equals(ASTHelpers.getSymbol(assignment.getVariable())) | ||
? Optional.of(assignment.getExpression()) | ||
: Optional.empty(); | ||
} | ||
|
||
if (tree instanceof VariableTree) { | ||
VariableTree declaration = (VariableTree) tree; | ||
return declaration.getModifiers().getAnnotations().isEmpty() | ||
&& targetSymbol.equals(ASTHelpers.getSymbol(declaration)) | ||
? Optional.ofNullable(declaration.getInitializer()) | ||
: Optional.empty(); | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
|
||
/** | ||
* Tells whether inlining the given expression to the associated return statement can be done | ||
* safely. | ||
* | ||
* <p>Inlining is generally safe, but in rare cases the operation may have a functional impact. | ||
* The sole case considered here is the inlining of a Mockito mock or spy construction without an | ||
* explicit type. In such a case the type created depends on context, such as the method's return | ||
* type. | ||
*/ | ||
private static boolean canInlineToReturnStatement( | ||
ExpressionTree expressionTree, VisitorState state) { | ||
return !MOCKITO_MOCK_OR_SPY_WITH_IMPLICIT_TYPE.matches(expressionTree, state) | ||
|| MoreASTHelpers.findMethodExitedOnReturn(state) | ||
.filter(m -> MoreASTHelpers.areSameType(expressionTree, m.getReturnType(), state)) | ||
.isPresent(); | ||
} | ||
} |
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
Oops, something went wrong.