Search in sources :

Example 16 with Description

use of com.google.errorprone.matchers.Description in project error-prone by google.

the class JdkObsolete method describeIfObsolete.

private Description describeIfObsolete(@Nullable Tree tree, Iterable<Type> types, VisitorState state) {
    for (Type type : types) {
        Obsolete obsolete = OBSOLETE.get(type.asElement().getQualifiedName().toString());
        if (obsolete == null) {
            continue;
        }
        if (shouldSkip(state, type)) {
            continue;
        }
        Description.Builder description = buildDescription(state.getPath().getLeaf()).setMessage(obsolete.message());
        if (tree != null) {
            obsolete.fix(tree, state).ifPresent(description::addFix);
        }
        return description.build();
    }
    return NO_MATCH;
}
Also used : Type(com.sun.tools.javac.code.Type) Description(com.google.errorprone.matchers.Description)

Example 17 with Description

use of com.google.errorprone.matchers.Description in project error-prone by google.

the class DefaultCharset method matchMethodInvocation.

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
    if (state.isAndroidCompatible()) {
        // Android's default platform Charset is always UTF-8
        return NO_MATCH;
    }
    if (STRING_GET_BYTES.matches(tree, state)) {
        Description.Builder description = buildDescription(tree);
        Tree parent = state.getPath().getParentPath().getLeaf();
        if (parent instanceof ExpressionTree && BYTESTRING_COPY_FROM.matches((ExpressionTree) parent, state)) {
            byteStringFixes(description, tree, (ExpressionTree) parent, state);
        } else {
            appendCharsets(description, tree, tree.getMethodSelect(), tree.getArguments(), state);
        }
        return description.build();
    }
    if (FILE_NEW_WRITER.matches(tree, state)) {
        Description.Builder description = buildDescription(tree);
        appendCharsets(description, tree, tree.getMethodSelect(), tree.getArguments(), state);
        return description.build();
    }
    return NO_MATCH;
}
Also used : Description(com.google.errorprone.matchers.Description) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) ImportTree(com.sun.source.tree.ImportTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) JCTree(com.sun.tools.javac.tree.JCTree) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 18 with Description

use of com.google.errorprone.matchers.Description in project error-prone by google.

the class IterablePathParameter method matchVariable.

@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
    Type type = ASTHelpers.getType(tree);
    VarSymbol symbol = ASTHelpers.getSymbol(tree);
    if (type == null || symbol == null) {
        return NO_MATCH;
    }
    if (symbol.getKind() != ElementKind.PARAMETER) {
        return NO_MATCH;
    }
    if (!isSameType(type, state.getSymtab().iterableType, state)) {
        return NO_MATCH;
    }
    if (type.getTypeArguments().isEmpty()) {
        return NO_MATCH;
    }
    if (!isSameType(wildBound(getOnlyElement(type.getTypeArguments())), state.getTypeFromString(Path.class.getName()), state)) {
        return NO_MATCH;
    }
    Description.Builder description = buildDescription(tree);
    if (tree.getType() instanceof ParameterizedTypeTree) {
        description.addFix(SuggestedFix.builder().addImport("java.util.Collection").replace(((ParameterizedTypeTree) tree.getType()).getType(), "Collection").build());
    }
    return description.build();
}
Also used : Path(java.nio.file.Path) WildcardType(com.sun.tools.javac.code.Type.WildcardType) ASTHelpers.isSameType(com.google.errorprone.util.ASTHelpers.isSameType) Type(com.sun.tools.javac.code.Type) Description(com.google.errorprone.matchers.Description) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 19 with Description

use of com.google.errorprone.matchers.Description in project error-prone by google.

the class MissingDefault method matchSwitch.

@Override
public Description matchSwitch(SwitchTree tree, VisitorState state) {
    Type switchType = ASTHelpers.getType(tree.getExpression());
    if (switchType.asElement().getKind() == ElementKind.ENUM) {
        // by MissingCasesInEnumSwitch
        return NO_MATCH;
    }
    Optional<? extends CaseTree> maybeDefault = tree.getCases().stream().filter(c -> c.getExpression() == null).findFirst();
    if (!maybeDefault.isPresent()) {
        Description.Builder description = buildDescription(tree);
        if (!tree.getCases().isEmpty()) {
            // Inserting the default after the last case is easier than finding the closing brace
            // for the switch statement. Hopefully we don't often see switches with zero cases.
            CaseTree lastCase = getLast(tree.getCases());
            String replacement;
            if (lastCase.getStatements().isEmpty() || Reachability.canCompleteNormally(Iterables.getLast(lastCase.getStatements()))) {
                replacement = "\nbreak;\ndefault: // fall out\n";
            } else {
                replacement = "\ndefault: // fall out\n";
            }
            description.addFix(SuggestedFix.postfixWith(getLast(tree.getCases()), replacement));
        }
        return description.build();
    }
    CaseTree defaultCase = maybeDefault.get();
    if (!defaultCase.getStatements().isEmpty()) {
        return NO_MATCH;
    }
    // If `default` case is empty, and last in switch, add `// fall out` comment
    // TODO(epmjohnston): Maybe move comment logic to go/bugpattern/FallThrough
    int idx = tree.getCases().indexOf(defaultCase);
    if (idx != tree.getCases().size() - 1) {
        return NO_MATCH;
    }
    int end = state.getEndPosition(tree);
    if (ErrorProneTokens.getTokens(state.getSourceCode().subSequence(state.getEndPosition(defaultCase), end).toString(), state.context).stream().anyMatch(t -> !t.comments().isEmpty())) {
        return NO_MATCH;
    }
    return buildDescription(defaultCase).setMessage("Default case should be documented with a comment").addFix(SuggestedFix.postfixWith(defaultCase, " // fall out")).build();
}
Also used : Iterables(com.google.common.collect.Iterables) ElementKind(javax.lang.model.element.ElementKind) Iterables.getLast(com.google.common.collect.Iterables.getLast) NO_MATCH(com.google.errorprone.matchers.Description.NO_MATCH) CaseTree(com.sun.source.tree.CaseTree) SwitchTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.SwitchTreeMatcher) SwitchTree(com.sun.source.tree.SwitchTree) Reachability(com.google.errorprone.util.Reachability) VisitorState(com.google.errorprone.VisitorState) ErrorProneTokens(com.google.errorprone.util.ErrorProneTokens) Description(com.google.errorprone.matchers.Description) StandardTags(com.google.errorprone.BugPattern.StandardTags) BugPattern(com.google.errorprone.BugPattern) Optional(java.util.Optional) WARNING(com.google.errorprone.BugPattern.SeverityLevel.WARNING) ProvidesFix(com.google.errorprone.BugPattern.ProvidesFix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) JDK(com.google.errorprone.BugPattern.Category.JDK) ASTHelpers(com.google.errorprone.util.ASTHelpers) Type(com.sun.tools.javac.code.Type) CaseTree(com.sun.source.tree.CaseTree) Type(com.sun.tools.javac.code.Type) Description(com.google.errorprone.matchers.Description)

Example 20 with Description

use of com.google.errorprone.matchers.Description in project error-prone by google.

the class SwitchDefault method matchSwitch.

@Override
public Description matchSwitch(SwitchTree tree, VisitorState state) {
    Optional<? extends CaseTree> maybeDefault = tree.getCases().stream().filter(c -> c.getExpression() == null).findAny();
    if (!maybeDefault.isPresent()) {
        return NO_MATCH;
    }
    // Collect all case trees in the statement group containing the default
    List<CaseTree> defaultStatementGroup = new ArrayList<>();
    Iterator<? extends CaseTree> it = tree.getCases().iterator();
    while (it.hasNext()) {
        CaseTree caseTree = it.next();
        defaultStatementGroup.add(caseTree);
        if (caseTree.getExpression() == null) {
            while (it.hasNext() && caseTree.getStatements().isEmpty()) {
                caseTree = it.next();
                defaultStatementGroup.add(caseTree);
            }
            break;
        }
        if (!caseTree.getStatements().isEmpty()) {
            defaultStatementGroup.clear();
        }
    }
    // Find the position of the default case within the statement group
    int idx = defaultStatementGroup.indexOf(maybeDefault.get());
    SuggestedFix.Builder fix = SuggestedFix.builder();
    CaseTree defaultTree = defaultStatementGroup.get(idx);
    if (it.hasNext()) {
        // Only emit a fix if the default doesn't fall through.
        if (!Reachability.canCompleteNormally(getLast(defaultStatementGroup))) {
            int start = ((JCTree) defaultStatementGroup.get(0)).getStartPosition();
            int end = state.getEndPosition(getLast(defaultStatementGroup));
            String replacement;
            String source = state.getSourceCode().toString();
            // If the default case isn't the last case in its statement group, move it to the end.
            if (idx != defaultStatementGroup.size() - 1) {
                int caseEnd = ((JCTree) getLast(defaultStatementGroup).getStatements().get(0)).getStartPosition();
                int cutStart = ((JCTree) defaultTree).getStartPosition();
                int cutEnd = state.getEndPosition(defaultTree);
                replacement = source.substring(start, cutStart) + source.substring(cutEnd, caseEnd) + "\n" + source.substring(cutStart, cutEnd) + source.substring(caseEnd, end);
            } else {
                replacement = source.substring(start, end);
            }
            fix.replace(start, end, "").postfixWith(getLast(tree.getCases()), replacement);
        }
    } else if (idx != defaultStatementGroup.size() - 1) {
        // If the default case isn't the last case in its statement group, move it to the end.
        fix.delete(defaultTree).prefixWith(getLast(defaultStatementGroup).getStatements().get(0), state.getSourceForNode(defaultTree));
    } else {
        return NO_MATCH;
    }
    Description.Builder description = buildDescription(defaultStatementGroup.get(0));
    if (!fix.isEmpty()) {
        description.addFix(fix.build());
    }
    return description.build();
}
Also used : Iterator(java.util.Iterator) Iterables.getLast(com.google.common.collect.Iterables.getLast) NO_MATCH(com.google.errorprone.matchers.Description.NO_MATCH) CaseTree(com.sun.source.tree.CaseTree) JCTree(com.sun.tools.javac.tree.JCTree) SwitchTreeMatcher(com.google.errorprone.bugpatterns.BugChecker.SwitchTreeMatcher) SwitchTree(com.sun.source.tree.SwitchTree) Reachability(com.google.errorprone.util.Reachability) ArrayList(java.util.ArrayList) VisitorState(com.google.errorprone.VisitorState) List(java.util.List) Description(com.google.errorprone.matchers.Description) BugPattern(com.google.errorprone.BugPattern) Optional(java.util.Optional) SUGGESTION(com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION) ProvidesFix(com.google.errorprone.BugPattern.ProvidesFix) SuggestedFix(com.google.errorprone.fixes.SuggestedFix) Description(com.google.errorprone.matchers.Description) ArrayList(java.util.ArrayList) JCTree(com.sun.tools.javac.tree.JCTree) CaseTree(com.sun.source.tree.CaseTree) SuggestedFix(com.google.errorprone.fixes.SuggestedFix)

Aggregations

Description (com.google.errorprone.matchers.Description)56 Tree (com.sun.source.tree.Tree)23 VisitorState (com.google.errorprone.VisitorState)22 BugPattern (com.google.errorprone.BugPattern)21 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)20 ASTHelpers (com.google.errorprone.util.ASTHelpers)20 WARNING (com.google.errorprone.BugPattern.SeverityLevel.WARNING)17 ExpressionTree (com.sun.source.tree.ExpressionTree)17 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)17 JDK (com.google.errorprone.BugPattern.Category.JDK)16 Symbol (com.sun.tools.javac.code.Symbol)16 ProvidesFix (com.google.errorprone.BugPattern.ProvidesFix)14 Type (com.sun.tools.javac.code.Type)14 DescriptionBasedDiff (com.google.errorprone.apply.DescriptionBasedDiff)11 VariableTree (com.sun.source.tree.VariableTree)11 MethodSymbol (com.sun.tools.javac.code.Symbol.MethodSymbol)11 NO_MATCH (com.google.errorprone.matchers.Description.NO_MATCH)10 ClassTree (com.sun.source.tree.ClassTree)10 Optional (java.util.Optional)10 MethodTree (com.sun.source.tree.MethodTree)9