Search in sources :

Example 26 with Fix

use of com.google.errorprone.fixes.Fix in project error-prone by google.

the class BadComparable method matchTypeCast.

@Override
public Description matchTypeCast(TypeCastTree tree, VisitorState state) {
    // Check for a narrowing match first as its simplest match to test.
    if (!matches(tree, state)) {
        return Description.NO_MATCH;
    }
    // Test that the match is in a Comparable.compareTo or Comparator.compare method.
    ClassTree declaringClass = ASTHelpers.findEnclosingNode(state.getPath(), ClassTree.class);
    if (!COMPARABLE_CLASS_MATCHER.matches(declaringClass, state) && !COMPARATOR_CLASS_MATCHER.matches(declaringClass, state)) {
        return Description.NO_MATCH;
    }
    MethodTree method = ASTHelpers.findEnclosingNode(state.getPath(), MethodTree.class);
    if (method == null) {
        return Description.NO_MATCH;
    }
    if (!COMPARABLE_METHOD_MATCHER.matches(method, state) && !COMPARATOR_METHOD_MATCHER.matches(method, state)) {
        return Description.NO_MATCH;
    }
    // Get the unparenthesized expression.
    BinaryTree subtract = (BinaryTree) ASTHelpers.stripParentheses(tree.getExpression());
    ExpressionTree lhs = subtract.getLeftOperand();
    ExpressionTree rhs = subtract.getRightOperand();
    Fix fix;
    if (ASTHelpers.getType(lhs).isPrimitive()) {
        fix = SuggestedFix.replace(tree, "Long.compare(" + lhs + ", " + rhs + ")");
    } else {
        fix = SuggestedFix.replace(tree, lhs + ".compareTo(" + rhs + ")");
    }
    return describeMatch(tree, fix);
}
Also used : Fix(com.google.errorprone.fixes.Fix) ProvidesFix(com.google.errorprone.BugPattern.ProvidesFix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) MethodTree(com.sun.source.tree.MethodTree) ClassTree(com.sun.source.tree.ClassTree) BinaryTree(com.sun.source.tree.BinaryTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 27 with Fix

use of com.google.errorprone.fixes.Fix in project error-prone by google.

the class CatchFail method deleteFix.

// Extract the argument to a call to assertWithMessage, e.g. in:
// assertWithMessage("message").fail();
Optional<Fix> deleteFix(TryTree tree, ImmutableList<CatchTree> catchBlocks, VisitorState state) {
    SuggestedFix.Builder fix = SuggestedFix.builder();
    if (tree.getFinallyBlock() != null || catchBlocks.size() < tree.getCatches().size()) {
        // If the try statement has a finally region, or other catch blocks, delete only the
        // unnecessary blocks.
        catchBlocks.stream().forEachOrdered(fix::delete);
    } else {
        // The try statement has no finally region and all catch blocks are unnecessary. Replace it
        // with the try statements, deleting all catches.
        List<? extends StatementTree> tryStatements = tree.getBlock().getStatements();
        String source = state.getSourceCode().toString();
        // Replace the full region to work around a GJF partial formatting bug that prevents it from
        // re-indenting unchanged lines. This means that fixes may overlap, but that's (hopefully)
        // unlikely.
        // TODO(b/24140798): emit more precise replacements if GJF is fixed
        fix.replace(tree, source.substring(((JCTree) tryStatements.get(0)).getStartPosition(), state.getEndPosition(Iterables.getLast(tryStatements))));
    }
    MethodTree enclosing = findEnclosing(state.getPath());
    if (enclosing == null) {
        // There isn't an enclosing method, possibly because we're in a lambda or initializer block.
        return Optional.empty();
    }
    if (isExpectedExceptionTest(ASTHelpers.getSymbol(enclosing), state)) {
        // tests, so don't use that fix for methods annotated with @Test(expected=...).
        return Optional.empty();
    }
    // Fix up the enclosing method's throws declaration to include the new thrown exception types.
    Collection<Type> thrownTypes = ASTHelpers.getSymbol(enclosing).getThrownTypes();
    Types types = state.getTypes();
    // Find all types in the deleted catch blocks that are not already in the throws declaration.
    ImmutableList<Type> toThrow = catchBlocks.stream().map(c -> ASTHelpers.getType(c.getParameter())).flatMap(t -> t instanceof UnionClassType ? ImmutableList.copyOf(((UnionClassType) t).getAlternativeTypes()).stream() : Stream.of(t)).filter(t -> thrownTypes.stream().noneMatch(x -> types.isAssignable(t, x))).collect(toImmutableList());
    if (!toThrow.isEmpty()) {
        if (!TEST_CASE.matches(enclosing, state)) {
            // not be a safe local refactoring.
            return Optional.empty();
        }
        String throwsString = toThrow.stream().map(t -> SuggestedFixes.qualifyType(state, fix, t)).distinct().collect(joining(", "));
        if (enclosing.getThrows().isEmpty()) {
            // Add a new throws declaration.
            fix.prefixWith(enclosing.getBody(), "throws " + throwsString);
        } else {
            // Append to an existing throws declaration.
            fix.postfixWith(Iterables.getLast(enclosing.getThrows()), ", " + throwsString);
        }
    }
    return Optional.of(fix.build());
}
Also used : SuggestedFixes(com.google.errorprone.fixes.SuggestedFixes) Iterables(com.google.common.collect.Iterables) Matchers.anyOf(com.google.errorprone.matchers.Matchers.anyOf) MethodSymbol(com.sun.tools.javac.code.Symbol.MethodSymbol) MethodTree(com.sun.source.tree.MethodTree) VisitorState(com.google.errorprone.VisitorState) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) Matchers.expressionStatement(com.google.errorprone.matchers.Matchers.expressionStatement) ImmutableList(com.google.common.collect.ImmutableList) TryTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.TryTreeMatcher) IdentifierTree(com.sun.source.tree.IdentifierTree) CatchTree(com.sun.source.tree.CatchTree) BugPattern(com.google.errorprone.BugPattern) Matcher(com.google.errorprone.matchers.Matcher) Fix(com.google.errorprone.fixes.Fix) Tree(com.sun.source.tree.Tree) MethodNameMatcher(com.google.errorprone.matchers.method.MethodMatchers.MethodNameMatcher) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol) TreePath(com.sun.source.util.TreePath) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Matchers.hasAnnotation(com.google.errorprone.matchers.Matchers.hasAnnotation) Collection(java.util.Collection) Compound(com.sun.tools.javac.code.Attribute.Compound) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) NO_MATCH(com.google.errorprone.matchers.Description.NO_MATCH) JCTree(com.sun.tools.javac.tree.JCTree) JUNIT4_TEST_ANNOTATION(com.google.errorprone.matchers.JUnitMatchers.JUNIT4_TEST_ANNOTATION) Collectors.joining(java.util.stream.Collectors.joining) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) JUnitMatchers.isJunit3TestCase(com.google.errorprone.matchers.JUnitMatchers.isJunit3TestCase) TreeScanner(com.sun.source.util.TreeScanner) Objects(java.util.Objects) List(java.util.List) MethodMatchers.staticMethod(com.google.errorprone.matchers.method.MethodMatchers.staticMethod) Types(com.sun.tools.javac.code.Types) Stream(java.util.stream.Stream) JUnitMatchers(com.google.errorprone.matchers.JUnitMatchers) Description(com.google.errorprone.matchers.Description) StatementTree(com.sun.source.tree.StatementTree) TryTree(com.sun.source.tree.TryTree) Optional(java.util.Optional) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) ProvidesFix(com.google.errorprone.BugPattern.ProvidesFix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) ASTHelpers(com.google.errorprone.util.ASTHelpers) Type(com.sun.tools.javac.code.Type) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) Types(com.sun.tools.javac.code.Types) MethodTree(com.sun.source.tree.MethodTree) JCTree(com.sun.tools.javac.tree.JCTree) UnionClassType(com.sun.tools.javac.code.Type.UnionClassType) Type(com.sun.tools.javac.code.Type) SuggestedFix(com.google.errorprone.fixes.SuggestedFix)

Example 28 with Fix

use of com.google.errorprone.fixes.Fix in project error-prone by google.

the class DeadException method matchNewClass.

@Override
public Description matchNewClass(NewClassTree newClassTree, VisitorState state) {
    if (!MATCHER.matches(newClassTree, state)) {
        return Description.NO_MATCH;
    }
    StatementTree parent = (StatementTree) state.getPath().getParentPath().getLeaf();
    boolean isLastStatement = anyOf(new ChildOfBlockOrCase<>(ChildMultiMatcher.MatchType.LAST, Matchers.<StatementTree>isSame(parent)), // it could also be a bare if statement with no braces
    parentNode(parentNode(kindIs(IF)))).matches(newClassTree, state);
    Fix fix;
    if (isLastStatement) {
        fix = SuggestedFix.prefixWith(newClassTree, "throw ");
    } else {
        fix = SuggestedFix.delete(parent);
    }
    return describeMatch(newClassTree, fix);
}
Also used : StatementTree(com.sun.source.tree.StatementTree) Fix(com.google.errorprone.fixes.Fix) ProvidesFix(com.google.errorprone.BugPattern.ProvidesFix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix)

Example 29 with Fix

use of com.google.errorprone.fixes.Fix in project error-prone by google.

the class JUnit3FloatingPointComparisonWithoutDelta method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree methodInvocationTree, VisitorState state) {
    if (!ASSERT_EQUALS_MATCHER.matches(methodInvocationTree, state)) {
        return Description.NO_MATCH;
    }
    List<Type> argumentTypes = getArgumentTypesWithoutMessage(methodInvocationTree, state);
    if (canBeConvertedToJUnit4(state, argumentTypes)) {
        return Description.NO_MATCH;
    }
    Fix fix = addDeltaArgument(methodInvocationTree, state, argumentTypes);
    return describeMatch(methodInvocationTree, fix);
}
Also used : Type(com.sun.tools.javac.code.Type) ProvidesFix(com.google.errorprone.BugPattern.ProvidesFix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Fix(com.google.errorprone.fixes.Fix)

Aggregations

Fix (com.google.errorprone.fixes.Fix)29 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)28 ProvidesFix (com.google.errorprone.BugPattern.ProvidesFix)21 ExpressionTree (com.sun.source.tree.ExpressionTree)13 Description (com.google.errorprone.matchers.Description)7 Tree (com.sun.source.tree.Tree)7 Type (com.sun.tools.javac.code.Type)7 IdentifierTree (com.sun.source.tree.IdentifierTree)5 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)5 JCTree (com.sun.tools.javac.tree.JCTree)5 VisitorState (com.google.errorprone.VisitorState)4 MethodTree (com.sun.source.tree.MethodTree)4 StatementTree (com.sun.source.tree.StatementTree)4 BugPattern (com.google.errorprone.BugPattern)3 ASTHelpers (com.google.errorprone.util.ASTHelpers)3 ClassTree (com.sun.source.tree.ClassTree)3 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)3 ImmutableList (com.google.common.collect.ImmutableList)2 Iterables.getOnlyElement (com.google.common.collect.Iterables.getOnlyElement)2 JDK (com.google.errorprone.BugPattern.Category.JDK)2