|
27 | 27 | import com.google.errorprone.VisitorState;
|
28 | 28 | import com.google.errorprone.bugpatterns.BugChecker;
|
29 | 29 | import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
|
| 30 | +import com.google.errorprone.bugpatterns.BugChecker.NewClassTreeMatcher; |
30 | 31 | import com.google.errorprone.fixes.SuggestedFix;
|
31 | 32 | import com.google.errorprone.matchers.Description;
|
32 | 33 | import com.google.errorprone.util.ASTHelpers;
|
33 | 34 | import com.sun.source.tree.ExpressionTree;
|
34 | 35 | import com.sun.source.tree.LiteralTree;
|
35 | 36 | import com.sun.source.tree.MethodInvocationTree;
|
| 37 | +import com.sun.source.tree.NewClassTree; |
| 38 | +import java.util.List; |
36 | 39 |
|
37 | 40 | /** A BugPattern; see the summary. */
|
38 | 41 | @BugPattern(
|
|
41 | 44 | "The number of arguments provided to lenient format methods should match the positional"
|
42 | 45 | + " specifiers.")
|
43 | 46 | public final class LenientFormatStringValidation extends BugChecker
|
44 |
| - implements MethodInvocationTreeMatcher { |
| 47 | + implements MethodInvocationTreeMatcher, NewClassTreeMatcher { |
45 | 48 |
|
46 | 49 | @Override
|
47 | 50 | public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
|
| 51 | + return match(tree, tree.getArguments(), state); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public Description matchNewClass(NewClassTree tree, VisitorState state) { |
| 56 | + return match(tree, tree.getArguments(), state); |
| 57 | + } |
| 58 | + |
| 59 | + private Description match( |
| 60 | + ExpressionTree tree, List<? extends ExpressionTree> args, VisitorState state) { |
48 | 61 | int formatStringPosition = getLenientFormatStringPosition(tree, state);
|
49 | 62 | if (formatStringPosition < 0) {
|
50 | 63 | return NO_MATCH;
|
51 | 64 | }
|
52 |
| - var args = tree.getArguments(); |
53 | 65 | if (args.size() <= formatStringPosition) {
|
54 | 66 | return NO_MATCH;
|
55 | 67 | }
|
|
0 commit comments