Search in sources :

Example 1 with BugPattern

use of com.google.errorprone.BugPattern 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)

Aggregations

Iterables (com.google.common.collect.Iterables)1 Iterables.getLast (com.google.common.collect.Iterables.getLast)1 BugPattern (com.google.errorprone.BugPattern)1 JDK (com.google.errorprone.BugPattern.Category.JDK)1 ProvidesFix (com.google.errorprone.BugPattern.ProvidesFix)1 WARNING (com.google.errorprone.BugPattern.SeverityLevel.WARNING)1 StandardTags (com.google.errorprone.BugPattern.StandardTags)1 VisitorState (com.google.errorprone.VisitorState)1 SwitchTreeMatcher (com.google.errorprone.bugpatterns.BugChecker.SwitchTreeMatcher)1 SuggestedFix (com.google.errorprone.fixes.SuggestedFix)1 Description (com.google.errorprone.matchers.Description)1 NO_MATCH (com.google.errorprone.matchers.Description.NO_MATCH)1 ASTHelpers (com.google.errorprone.util.ASTHelpers)1 ErrorProneTokens (com.google.errorprone.util.ErrorProneTokens)1 Reachability (com.google.errorprone.util.Reachability)1 CaseTree (com.sun.source.tree.CaseTree)1 SwitchTree (com.sun.source.tree.SwitchTree)1 Type (com.sun.tools.javac.code.Type)1 Optional (java.util.Optional)1 ElementKind (javax.lang.model.element.ElementKind)1