Skip to content
Permalink
Browse files

Clean up description creation

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=242192393
  • Loading branch information...
cushon authored and ronshapiro committed Apr 5, 2019
1 parent 604ab56 commit c568c9a8eac7f2c06702f627cbb38c704afca62b
@@ -113,6 +113,18 @@ protected Description describeMatch(Tree node, Fix fix) {
return buildDescription(node).addFix(fix).build();
}

/** Helper to create a Description for the common case where there is a fix. */
@CheckReturnValue
protected Description describeMatch(JCTree node, Fix fix) {
return describeMatch((Tree) node, fix);
}

/** Helper to create a Description for the common case where there is a fix. */
@CheckReturnValue
protected Description describeMatch(DiagnosticPosition position, Fix fix) {
return buildDescription(position).addFix(fix).build();
}

/** Helper to create a Description for the common case where there is no fix. */
@CheckReturnValue
protected Description describeMatch(Tree node) {
@@ -104,13 +104,12 @@ private Description checkClosed(ExpressionTree tree, VisitorState state) {
}
// The caller method is not annotated, so the closing of the returned resource is not
// enforced. Suggest fixing this by annotating the caller method.
return buildDescription(tree)
.addFix(
SuggestedFix.builder()
.prefixWith(callerMethodTree, "@MustBeClosed\n")
.addImport(MustBeClosed.class.getCanonicalName())
.build())
.build();
return describeMatch(
tree,
SuggestedFix.builder()
.prefixWith(callerMethodTree, "@MustBeClosed\n")
.addImport(MustBeClosed.class.getCanonicalName())
.build());
}
break;
case CONDITIONAL_EXPRESSION:
@@ -102,15 +102,14 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
boolean fixable =
formatParameter.get().equals(enclosingParameters.get(enclosingParameters.size() - 2));
if (fixable) {
return buildDescription(enclosingMethod)
.addFix(
SuggestedFix.builder()
.prefixWith(enclosingMethod, "@FormatMethod ")
.prefixWith(formatParameter.get(), "@FormatString ")
.addImport("com.google.errorprone.annotations.FormatMethod")
.addImport("com.google.errorprone.annotations.FormatString")
.build())
.build();
return describeMatch(
enclosingMethod,
SuggestedFix.builder()
.prefixWith(enclosingMethod, "@FormatMethod ")
.prefixWith(formatParameter.get(), "@FormatString ")
.addImport("com.google.errorprone.annotations.FormatMethod")
.addImport("com.google.errorprone.annotations.FormatString")
.build());
}
return buildDescription(enclosingMethod).setMessage(message() + REORDER).build();
}
@@ -66,12 +66,10 @@ public Description matchTypeCast(TypeCastTree typeCastTree, VisitorState visitor
return Description.NO_MATCH;
}
if (castingMatcher.matches(typeCastTree, visitorState)) {
return buildDescription(typeCastTree)
.addFix(
SuggestedFix.replace(
castingMatcher.nodeToReplace,
visitorState.getSourceForNode(typeCastTree.getType())))
.build();
return describeMatch(
typeCastTree,
SuggestedFix.replace(
castingMatcher.nodeToReplace, visitorState.getSourceForNode(typeCastTree.getType())));
}
return Description.NO_MATCH;
}
@@ -101,7 +101,7 @@ public Description matchClass(ClassTree tree, VisitorState state) {
suggestedFix
.merge(fixClass(tree, state))
.postfixWith(getLast(members), String.format("\nprivate %s() {}", tree.getSimpleName()));
return buildDescription(tree).addFix(suggestedFix.build()).build();
return describeMatch(tree, suggestedFix.build());
}

private static SuggestedFix fixClass(ClassTree classTree, VisitorState state) {
@@ -73,14 +73,13 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
private Description removeMathRoundCall(MethodInvocationTree tree, VisitorState state) {
if (ROUND_CALLS_WITH_INT_ARG.matches(tree, state)) {
if (ASTHelpers.requiresParentheses(Iterables.getOnlyElement(tree.getArguments()), state)) {
return buildDescription(tree)
.addFix(
SuggestedFix.builder()
.prefixWith(tree, "(")
.replace(tree, state.getSourceForNode(tree.getArguments().get(0)))
.postfixWith(tree, ")")
.build())
.build();
return describeMatch(
tree,
SuggestedFix.builder()
.prefixWith(tree, "(")
.replace(tree, state.getSourceForNode(tree.getArguments().get(0)))
.postfixWith(tree, ")")
.build());
}
return describeMatch(
tree, SuggestedFix.replace(tree, state.getSourceForNode(tree.getArguments().get(0))));
@@ -88,6 +88,6 @@ public Description matchMethodInvocation(
return Description.NO_MATCH;
}
SuggestedFix fix = SuggestedFix.swap(expr, expectation);
return buildDescription(methodInvocationTree).addFix(fix).build();
return describeMatch(methodInvocationTree, fix);
}
}
@@ -114,7 +114,7 @@ public Description matchClass(ClassTree classTree, VisitorState state) {
if (batchFindings && !descriptions.isEmpty()) {
SuggestedFix.Builder fix = SuggestedFix.builder();
descriptions.forEach(d -> fix.merge((SuggestedFix) getOnlyElement(d.fixes)));
return buildDescription(descriptions.get(0).position).addFix(fix.build()).build();
return describeMatch(descriptions.get(0).position, fix.build());
}
descriptions.forEach(state::reportMatch);
return NO_MATCH;
@@ -91,7 +91,7 @@ public Description matchMethod(MethodTree methodTree, VisitorState state) {
replaceAssert(fix, foundAssert, state);
}

return buildDescription(methodTree).addFix(fix.build()).build();
return describeMatch(methodTree, fix.build());
}
return Description.NO_MATCH;
}
@@ -68,9 +68,7 @@ public Description matchNewClass(NewClassTree tree, VisitorState state) {
return Description.NO_MATCH;
}

return buildDescription(invocationInfo.tree())
.addFix(changes.buildPermuteArgumentsFix(invocationInfo))
.build();
return describeMatch(invocationInfo.tree(), changes.buildPermuteArgumentsFix(invocationInfo));
}

private static Function<ParameterPair, Double> buildDistanceFunction() {
@@ -181,10 +181,8 @@ private void handleMalformedTags(TextTree node) {
.replace(startOfCurly, startOfCurly + 1, " ")
.build();
state.reportMatch(
buildDescription(
getDiagnosticPosition(beforeAt, getCurrentPath().getTreePath().getLeaf()))
.addFix(fix)
.build());
describeMatch(
getDiagnosticPosition(beforeAt, getCurrentPath().getTreePath().getLeaf()), fix));
}
}

@@ -249,10 +247,8 @@ private void handleDanglingParams(TextTree node) {
SuggestedFix fix =
SuggestedFix.replace(startPos, endPos, String.format("{@code %s}", paramName));
state.reportMatch(
buildDescription(
getDiagnosticPosition(startPos, getCurrentPath().getTreePath().getLeaf()))
.addFix(fix)
.build());
describeMatch(
getDiagnosticPosition(startPos, getCurrentPath().getTreePath().getLeaf()), fix));
}
}

@@ -79,9 +79,9 @@ public Void visitThrows(ThrowsTree throwsTree, Void unused) {
if (type != null && isCheckedException(type)) {
if (methodTree.getThrows().stream().noneMatch(t -> isSubtype(type, getType(t), state))) {
state.reportMatch(
buildDescription(diagnosticPosition(getCurrentPath(), state))
.addFix(Utils.replace(throwsTree, "", state))
.build());
describeMatch(
diagnosticPosition(getCurrentPath(), state),
Utils.replace(throwsTree, "", state)));
}
}
return super.visitThrows(throwsTree, null);
@@ -69,9 +69,8 @@ private VoidReturnTypeChecker(VisitorState state, MethodTree methodTree) {
public Void visitReturn(ReturnTree returnTree, Void unused) {
if (isSameType(getType(methodTree.getReturnType()), state.getSymtab().voidType, state)) {
state.reportMatch(
buildDescription(diagnosticPosition(getCurrentPath(), state))
.addFix(Utils.replace(returnTree, "", state))
.build());
describeMatch(
diagnosticPosition(getCurrentPath(), state), Utils.replace(returnTree, "", state)));
}
return super.visitReturn(returnTree, null);
}
@@ -296,9 +296,9 @@ public Void visitErroneous(ErroneousTree erroneousTree, Void unused) {
}

private Description replacementFix(String replacement) {
return buildDescription(diagnosticPosition(getCurrentPath(), state))
.addFix(replace(getCurrentPath().getLeaf(), replacement, state))
.build();
return describeMatch(
diagnosticPosition(getCurrentPath(), state),
replace(getCurrentPath().getLeaf(), replacement, state));
}
}

@@ -259,7 +259,7 @@ public void staticLastImportOrder() {
@Override
public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) {
SuggestedFix fix = SuggestedFix.builder().addImport("java.util.ArrayList").build();
return buildDescription(tree).addFix(fix).build();
return describeMatch(tree, fix);
}
}
}

0 comments on commit c568c9a

Please sign in to comment.
You can’t perform that action at this time.