use of com.sun.source.tree.SwitchTree 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();
}
use of com.sun.source.tree.SwitchTree 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();
}
use of com.sun.source.tree.SwitchTree in project st-js by st-js.
the class SwitchWriter method visit.
@Override
public JS visit(WriterVisitor<JS> visitor, SwitchTree tree, GenerationContext<JS> context) {
Tree expr = tree.getExpression();
if (expr instanceof ParenthesizedTree) {
// remove the parans
expr = ((ParenthesizedTree) expr).getExpression();
}
JS jsExpr = visitor.scan(expr, context);
List<JS> cases = new ArrayList<JS>();
for (Tree c : tree.getCases()) {
cases.add(visitor.scan(c, context));
}
return context.withPosition(tree, context.js().switchStatement(jsExpr, cases));
}
use of com.sun.source.tree.SwitchTree in project error-prone by google.
the class MissingCasesInEnumSwitch method matchSwitch.
@Override
public Description matchSwitch(SwitchTree tree, VisitorState state) {
Type switchType = ASTHelpers.getType(tree.getExpression());
if (switchType.asElement().getKind() != ElementKind.ENUM) {
return Description.NO_MATCH;
}
// default case is present
if (tree.getCases().stream().anyMatch(c -> c.getExpression() == null)) {
return Description.NO_MATCH;
}
ImmutableSet<String> handled = tree.getCases().stream().map(CaseTree::getExpression).filter(IdentifierTree.class::isInstance).map(e -> ((IdentifierTree) e).getName().toString()).collect(toImmutableSet());
Set<String> unhandled = Sets.difference(ASTHelpers.enumValues(switchType.asElement()), handled);
if (unhandled.isEmpty()) {
return Description.NO_MATCH;
}
return buildDescription(tree).setMessage(buildMessage(unhandled)).build();
}
use of com.sun.source.tree.SwitchTree in project error-prone by google.
the class UnnecessaryDefaultInEnumSwitch method matchSwitch.
@Override
public Description matchSwitch(SwitchTree tree, VisitorState state) {
TypeSymbol switchType = ((JCSwitch) tree).getExpression().type.tsym;
if (switchType.getKind() != ElementKind.ENUM) {
return NO_MATCH;
}
CaseTree caseBeforeDefault = null;
CaseTree defaultCase = null;
for (CaseTree caseTree : tree.getCases()) {
if (caseTree.getExpression() == null) {
defaultCase = caseTree;
break;
} else {
caseBeforeDefault = caseTree;
}
}
if (defaultCase == null) {
return NO_MATCH;
}
Set<String> handledCases = tree.getCases().stream().map(CaseTree::getExpression).filter(IdentifierTree.class::isInstance).map(p -> ((IdentifierTree) p).getName().toString()).collect(toImmutableSet());
if (!ASTHelpers.enumValues(switchType).equals(handledCases)) {
return NO_MATCH;
}
Fix fix;
List<? extends StatementTree> defaultStatements = defaultCase.getStatements();
if (trivialDefault(defaultStatements)) {
// deleting `default:` or `default: break;` is a no-op
fix = SuggestedFix.delete(defaultCase);
} else {
String defaultSource = state.getSourceCode().subSequence(((JCTree) defaultStatements.get(0)).getStartPosition(), state.getEndPosition(getLast(defaultStatements))).toString();
String initialComments = comments(state, defaultCase, defaultStatements);
if (!canCompleteNormally(tree)) {
// if the switch statement cannot complete normally, then deleting the default
// and moving its statements to after the switch statement is a no-op
fix = SuggestedFix.builder().delete(defaultCase).postfixWith(tree, initialComments + defaultSource).build();
} else {
// and the code is unreachable -- so use (2) as the strategy. Otherwise, use (1).
if (!SuggestedFixes.compilesWithFix(SuggestedFix.delete(defaultCase), state)) {
// case (3)
return NO_MATCH;
}
if (!canCompleteNormally(caseBeforeDefault)) {
// case (2) -- If the case before the default can't complete normally,
// it's OK to to delete the default.
fix = SuggestedFix.delete(defaultCase);
} else {
// case (1) -- If it can complete, we need to merge the default into it.
fix = SuggestedFix.builder().delete(defaultCase).postfixWith(caseBeforeDefault, initialComments + defaultSource).build();
}
}
}
return describeMatch(defaultCase, fix);
}
Aggregations