|
| 1 | +/* |
| 2 | + * Copyright 2021 The Error Prone Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.errorprone.bugpatterns; |
| 18 | + |
| 19 | +import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; |
| 20 | +import static com.google.errorprone.matchers.Description.NO_MATCH; |
| 21 | +import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod; |
| 22 | +import static com.google.errorprone.util.ASTHelpers.getStartPosition; |
| 23 | + |
| 24 | +import com.google.errorprone.BugPattern; |
| 25 | +import com.google.errorprone.VisitorState; |
| 26 | +import com.google.errorprone.fixes.SuggestedFix; |
| 27 | +import com.google.errorprone.matchers.Description; |
| 28 | +import com.google.errorprone.matchers.Matcher; |
| 29 | +import com.google.errorprone.util.ASTHelpers; |
| 30 | +import com.sun.source.tree.BlockTree; |
| 31 | +import com.sun.source.tree.EnhancedForLoopTree; |
| 32 | +import com.sun.source.tree.ExpressionTree; |
| 33 | +import com.sun.source.tree.IdentifierTree; |
| 34 | +import com.sun.source.tree.StatementTree; |
| 35 | +import com.sun.source.tree.Tree; |
| 36 | +import com.sun.source.util.TreeScanner; |
| 37 | +import java.util.List; |
| 38 | + |
| 39 | +/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */ |
| 40 | +@BugPattern( |
| 41 | + name = "LoopOverCharArray", |
| 42 | + summary = "toCharArray allocates a new array, using charAt is more efficient", |
| 43 | + severity = WARNING) |
| 44 | +public class LoopOverCharArray extends BugChecker implements BugChecker.EnhancedForLoopTreeMatcher { |
| 45 | + |
| 46 | + private static final Matcher<ExpressionTree> TO_CHAR_ARRAY = |
| 47 | + instanceMethod().onExactClass("java.lang.String").named("toCharArray"); |
| 48 | + |
| 49 | + @Override |
| 50 | + public Description matchEnhancedForLoop(EnhancedForLoopTree tree, VisitorState state) { |
| 51 | + if (!TO_CHAR_ARRAY.matches(tree.getExpression(), state)) { |
| 52 | + return NO_MATCH; |
| 53 | + } |
| 54 | + ExpressionTree receiver = ASTHelpers.getReceiver(tree.getExpression()); |
| 55 | + if (!(receiver instanceof IdentifierTree)) { |
| 56 | + return NO_MATCH; |
| 57 | + } |
| 58 | + StatementTree body = tree.getStatement(); |
| 59 | + if (!(body instanceof BlockTree)) { |
| 60 | + return NO_MATCH; |
| 61 | + } |
| 62 | + List<? extends StatementTree> statements = ((BlockTree) body).getStatements(); |
| 63 | + if (statements.isEmpty()) { |
| 64 | + return NO_MATCH; |
| 65 | + } |
| 66 | + Description.Builder description = buildDescription(tree); |
| 67 | + // The fix uses `i` as the loop index variable, so give up if there's already an `i` in scope |
| 68 | + if (!alreadyDefinesIdentifier(tree)) { |
| 69 | + description.addFix( |
| 70 | + SuggestedFix.replace( |
| 71 | + getStartPosition(tree), |
| 72 | + getStartPosition(statements.get(0)), |
| 73 | + String.format( |
| 74 | + "for (int i = 0; i < %s.length(); i++) { char %s = %s.charAt(i);", |
| 75 | + state.getSourceForNode(receiver), |
| 76 | + tree.getVariable().getName(), |
| 77 | + state.getSourceForNode(receiver)))); |
| 78 | + } |
| 79 | + return description.build(); |
| 80 | + } |
| 81 | + |
| 82 | + private static boolean alreadyDefinesIdentifier(Tree tree) { |
| 83 | + boolean[] result = {false}; |
| 84 | + new TreeScanner<Void, Void>() { |
| 85 | + @Override |
| 86 | + public Void visitIdentifier(IdentifierTree node, Void unused) { |
| 87 | + if (node.getName().contentEquals("i")) { |
| 88 | + result[0] = true; |
| 89 | + } |
| 90 | + return super.visitIdentifier(node, unused); |
| 91 | + } |
| 92 | + }.scan(tree, null); |
| 93 | + return result[0]; |
| 94 | + } |
| 95 | +} |
0 commit comments