|
18 | 18 |
|
19 | 19 | import static com.google.common.collect.Iterables.getLast; |
20 | 20 | import static com.google.common.collect.MoreCollectors.toOptional; |
| 21 | +import static com.google.common.collect.Streams.stream; |
21 | 22 | import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; |
22 | 23 | import static com.google.errorprone.BugPattern.StandardTags.FRAGILE_CODE; |
23 | 24 | import static com.google.errorprone.matchers.Description.NO_MATCH; |
24 | 25 | import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod; |
25 | 26 | import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod; |
26 | | -import static com.google.errorprone.util.ASTHelpers.findEnclosingNode; |
27 | 27 | import static com.google.errorprone.util.ASTHelpers.getReceiver; |
28 | 28 | import static com.google.errorprone.util.ASTHelpers.getSymbol; |
29 | 29 | import static com.google.errorprone.util.ASTHelpers.hasAnnotation; |
30 | 30 | import static com.google.errorprone.util.AnnotationNames.FORMAT_METHOD_ANNOTATION; |
| 31 | +import static java.util.stream.Stream.empty; |
31 | 32 |
|
32 | 33 | import com.google.errorprone.BugPattern; |
33 | 34 | import com.google.errorprone.VisitorState; |
|
42 | 43 | import com.sun.tools.javac.code.Symbol; |
43 | 44 | import com.sun.tools.javac.code.Symbol.VarSymbol; |
44 | 45 | import java.util.List; |
| 46 | +import java.util.stream.Stream; |
45 | 47 | import org.jspecify.annotations.Nullable; |
46 | 48 |
|
47 | 49 | /** A BugPattern; see the summary. */ |
@@ -85,27 +87,38 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState |
85 | 87 | if (formatString == null || formatArgs == null) { |
86 | 88 | return NO_MATCH; |
87 | 89 | } |
88 | | - MethodTree enclosingMethod = findEnclosingNode(state.getPath(), MethodTree.class); |
89 | | - if (enclosingMethod == null |
90 | | - || !getSymbol(enclosingMethod).isVarArgs() |
91 | | - || hasAnnotation(enclosingMethod, FORMAT_METHOD_ANNOTATION, state)) { |
92 | | - return NO_MATCH; |
93 | | - } |
94 | | - List<? extends VariableTree> enclosingParameters = enclosingMethod.getParameters(); |
95 | | - VariableTree formatParameter = findParameterWithSymbol(enclosingParameters, formatString); |
96 | | - VariableTree argumentsParameter = findParameterWithSymbol(enclosingParameters, formatArgs); |
97 | | - if (formatParameter == null || argumentsParameter == null) { |
98 | | - return NO_MATCH; |
99 | | - } |
100 | | - if (!argumentsParameter.equals(getLast(enclosingParameters))) { |
101 | | - return NO_MATCH; |
102 | | - } |
103 | | - // We can only generate a fix if the format string is the penultimate parameter. |
104 | | - boolean fixable = |
105 | | - formatParameter.equals(enclosingParameters.get(enclosingParameters.size() - 2)); |
106 | | - return buildDescription(enclosingMethod) |
107 | | - .setMessage(fixable ? message() : (message() + REORDER)) |
108 | | - .build(); |
| 90 | + |
| 91 | + return stream(state.getPath()) |
| 92 | + .flatMap( |
| 93 | + node -> { |
| 94 | + if (!(node instanceof MethodTree methodTree)) { |
| 95 | + return empty(); |
| 96 | + } |
| 97 | + if (!getSymbol(methodTree).isVarArgs() |
| 98 | + || hasAnnotation(methodTree, FORMAT_METHOD_ANNOTATION, state)) { |
| 99 | + return empty(); |
| 100 | + } |
| 101 | + List<? extends VariableTree> enclosingParameters = methodTree.getParameters(); |
| 102 | + VariableTree formatParameter = |
| 103 | + findParameterWithSymbol(enclosingParameters, formatString); |
| 104 | + VariableTree argumentsParameter = |
| 105 | + findParameterWithSymbol(enclosingParameters, formatArgs); |
| 106 | + if (formatParameter == null || argumentsParameter == null) { |
| 107 | + return empty(); |
| 108 | + } |
| 109 | + if (!argumentsParameter.equals(getLast(enclosingParameters))) { |
| 110 | + return empty(); |
| 111 | + } |
| 112 | + // We can only generate a fix if the format string is the penultimate parameter. |
| 113 | + boolean fixable = |
| 114 | + formatParameter.equals(enclosingParameters.get(enclosingParameters.size() - 2)); |
| 115 | + return Stream.of( |
| 116 | + buildDescription(methodTree) |
| 117 | + .setMessage(fixable ? message() : (message() + REORDER)) |
| 118 | + .build()); |
| 119 | + }) |
| 120 | + .findFirst() |
| 121 | + .orElse(NO_MATCH); |
109 | 122 | } |
110 | 123 |
|
111 | 124 | private static VariableTree findParameterWithSymbol( |
|
0 commit comments