|
21 | 21 | import static com.google.errorprone.matchers.Matchers.allOf; |
22 | 22 | import static com.google.errorprone.matchers.Matchers.anyOf; |
23 | 23 | import static com.google.errorprone.matchers.Matchers.staticMethod; |
| 24 | +import static com.google.errorprone.util.ASTHelpers.constValue; |
24 | 25 | import static com.google.errorprone.util.ASTHelpers.getSymbol; |
25 | 26 | import static com.google.errorprone.util.ASTHelpers.hasAnnotation; |
| 27 | +import static com.google.errorprone.util.ASTHelpers.isConsideredFinal; |
26 | 28 | import static com.google.errorprone.util.ASTHelpers.isSubtype; |
27 | 29 | import static com.google.errorprone.util.AnnotationNames.FORMAT_METHOD_ANNOTATION; |
28 | 30 | import static com.google.errorprone.util.AnnotationNames.FORMAT_STRING_ANNOTATION; |
|
37 | 39 | import com.google.errorprone.fixes.SuggestedFix; |
38 | 40 | import com.google.errorprone.matchers.Description; |
39 | 41 | import com.google.errorprone.matchers.Matcher; |
40 | | -import com.google.errorprone.util.ASTHelpers; |
41 | 42 | import com.sun.source.tree.CompilationUnitTree; |
42 | 43 | import com.sun.source.tree.ExpressionTree; |
43 | 44 | import com.sun.source.tree.IdentifierTree; |
|
54 | 55 | import java.util.LinkedHashMap; |
55 | 56 | import java.util.List; |
56 | 57 | import java.util.Map; |
57 | | -import javax.lang.model.element.ElementKind; |
58 | 58 | import org.jspecify.annotations.Nullable; |
59 | 59 |
|
60 | 60 | /** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */ |
@@ -134,14 +134,21 @@ private void handle(MethodInvocationTree tree) { |
134 | 134 | return; |
135 | 135 | } |
136 | 136 | Symbol variable = getSymbol(arg); |
137 | | - if (variable == null |
138 | | - || variable.getKind() != ElementKind.FIELD |
139 | | - || !variable.isPrivate() |
140 | | - || ASTHelpers.constValue(arg, String.class) == null) { |
| 137 | + if (variable == null || !isConstant(variable, arg)) { |
141 | 138 | return; |
142 | 139 | } |
143 | 140 | uses.put(variable, arg); |
144 | 141 | } |
| 142 | + |
| 143 | + private boolean isConstant(Symbol variable, ExpressionTree arg) { |
| 144 | + return switch (variable.getKind()) { |
| 145 | + case FIELD -> variable.isPrivate() && constValue(arg, String.class) != null; |
| 146 | + // locals don't have a computed constant value, this will be handled below when we |
| 147 | + // scan for constant initializers |
| 148 | + case LOCAL_VARIABLE -> isConsideredFinal(variable); |
| 149 | + default -> false; |
| 150 | + }; |
| 151 | + } |
145 | 152 | }, |
146 | 153 | null); |
147 | 154 | // find all uses of the candidate fields, and reject them if they are used outside for calls to |
|
0 commit comments