Skip to content

Commit d42af91

Browse files
cushonError Prone Team
authored and
Error Prone Team
committed
Handle locals in InlineFormatString
PiperOrigin-RevId: 749587178
1 parent 3277d72 commit d42af91

File tree

2 files changed

+73
-6
lines changed

2 files changed

+73
-6
lines changed

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

+13-6
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
import static com.google.errorprone.matchers.Matchers.allOf;
2222
import static com.google.errorprone.matchers.Matchers.anyOf;
2323
import static com.google.errorprone.matchers.Matchers.staticMethod;
24+
import static com.google.errorprone.util.ASTHelpers.constValue;
2425
import static com.google.errorprone.util.ASTHelpers.getSymbol;
2526
import static com.google.errorprone.util.ASTHelpers.hasAnnotation;
27+
import static com.google.errorprone.util.ASTHelpers.isConsideredFinal;
2628
import static com.google.errorprone.util.ASTHelpers.isSubtype;
2729
import static com.google.errorprone.util.AnnotationNames.FORMAT_METHOD_ANNOTATION;
2830
import static com.google.errorprone.util.AnnotationNames.FORMAT_STRING_ANNOTATION;
@@ -37,7 +39,6 @@
3739
import com.google.errorprone.fixes.SuggestedFix;
3840
import com.google.errorprone.matchers.Description;
3941
import com.google.errorprone.matchers.Matcher;
40-
import com.google.errorprone.util.ASTHelpers;
4142
import com.sun.source.tree.CompilationUnitTree;
4243
import com.sun.source.tree.ExpressionTree;
4344
import com.sun.source.tree.IdentifierTree;
@@ -54,7 +55,6 @@
5455
import java.util.LinkedHashMap;
5556
import java.util.List;
5657
import java.util.Map;
57-
import javax.lang.model.element.ElementKind;
5858
import org.jspecify.annotations.Nullable;
5959

6060
/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */
@@ -134,14 +134,21 @@ private void handle(MethodInvocationTree tree) {
134134
return;
135135
}
136136
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)) {
141138
return;
142139
}
143140
uses.put(variable, arg);
144141
}
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+
}
145152
},
146153
null);
147154
// find all uses of the candidate fields, and reject them if they are used outside for calls to

core/src/test/java/com/google/errorprone/bugpatterns/formatstring/InlineFormatStringTest.java

+60
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,64 @@ void f() {
206206
""")
207207
.doTest();
208208
}
209+
210+
@Test
211+
public void negativeLocal() {
212+
compilationHelper
213+
.addSourceLines(
214+
"Test.java",
215+
"""
216+
class Test {
217+
void f(String a) {
218+
String format = a;
219+
System.err.printf(format, 42);
220+
}
221+
}
222+
""")
223+
.doTest();
224+
}
225+
226+
@Test
227+
public void refactoringLocal() {
228+
refactoringHelper
229+
.addInputLines(
230+
"Test.java",
231+
"""
232+
class Test {
233+
void f() {
234+
String format = "hello %s";
235+
System.err.printf(format, 42);
236+
}
237+
}
238+
""")
239+
.addOutputLines(
240+
"Test.java",
241+
"""
242+
class Test {
243+
void f() {
244+
System.err.printf("hello %s", 42);
245+
}
246+
}
247+
""")
248+
.doTest();
249+
}
250+
251+
@Test
252+
public void multipleUses() {
253+
compilationHelper
254+
.addSourceLines(
255+
"Test.java",
256+
"""
257+
class Test {
258+
void f() {
259+
String format = "hello %s";
260+
// BUG: Diagnostic contains
261+
System.err.printf(format, 42);
262+
// BUG: Diagnostic contains
263+
System.err.printf(format, 42);
264+
}
265+
}
266+
""")
267+
.doTest();
268+
}
209269
}

0 commit comments

Comments
 (0)