Skip to content

Commit ded6edc

Browse files
graememorganError Prone Team
authored andcommitted
Check constructors too in LenientFormatStringValidation.
Method invocations being different from constructor calls is very irksome. PiperOrigin-RevId: 751016565
1 parent 8e491ab commit ded6edc

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/formatstring/LenientFormatStringValidation.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@
2727
import com.google.errorprone.VisitorState;
2828
import com.google.errorprone.bugpatterns.BugChecker;
2929
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
30+
import com.google.errorprone.bugpatterns.BugChecker.NewClassTreeMatcher;
3031
import com.google.errorprone.fixes.SuggestedFix;
3132
import com.google.errorprone.matchers.Description;
3233
import com.google.errorprone.util.ASTHelpers;
3334
import com.sun.source.tree.ExpressionTree;
3435
import com.sun.source.tree.LiteralTree;
3536
import com.sun.source.tree.MethodInvocationTree;
37+
import com.sun.source.tree.NewClassTree;
38+
import java.util.List;
3639

3740
/** A BugPattern; see the summary. */
3841
@BugPattern(
@@ -41,15 +44,24 @@
4144
"The number of arguments provided to lenient format methods should match the positional"
4245
+ " specifiers.")
4346
public final class LenientFormatStringValidation extends BugChecker
44-
implements MethodInvocationTreeMatcher {
47+
implements MethodInvocationTreeMatcher, NewClassTreeMatcher {
4548

4649
@Override
4750
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) {
4861
int formatStringPosition = getLenientFormatStringPosition(tree, state);
4962
if (formatStringPosition < 0) {
5063
return NO_MATCH;
5164
}
52-
var args = tree.getArguments();
5365
if (args.size() <= formatStringPosition) {
5466
return NO_MATCH;
5567
}

0 commit comments

Comments
 (0)