Fixed
Status Update
Comments
rm...@google.com <rm...@google.com> #2
Thank you for your feedback. Team may reach out for more feedback in reproducing or triaging this issue.
tn...@google.com <tn...@google.com> #3
Sorry, this bug fell off the radar but I just checked with the current version of lint and it does correctly flag the bug now so I must have fixed it at some point. I've added an explicit unit test for your scenario to ensure that it doesn't break in the future:
public void testSpaceInPattern() {
// Regression test for
// 119711823: Invalid format string" check doesn't flag when a space in pattern "% s"
lint().files(
java(
""
+ "package test.pkg;\n"
+ "\n"
+ "import android.content.res.Resources;\n"
+ "\n"
+ "public class FormatSuggestions {\n"
+ " private void format() {\n"
+ " String format = getString(R.string.invalid_format, \"format\"); // inspection error is not reported\n"
+ " }\n"
+ "}\n"),
java(
""
+ "package test.pkg;\n"
+ "\n"
+ "public final class R {\n"
+ " public static final class string {\n"
+ " public static final int invalid_format = 0x7f0a0000;\n"
+ " }\n"
+ "}\n"),
xml(
"res/values/strings.xml",
""
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
+ "<resources>\n"
+ " <string name=\"invalid_format\">Invalid % s</string>\n"
+ "</resources>\n"
+ "\n"))
.run()
.expect(
""
+ "res/values/strings.xml:3: Error: Incorrect formatting string invalid_format; missing conversion character in '% s' ? [StringFormatInvalid]\n"
+ " <string name=\"invalid_format\">Invalid % s</string>\n"
+ " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
+ "1 errors, 0 warnings");
}
Description
Make sure both :
- "Android | Lint | Correctness | Messages | Invalid format string"
- "Android | Lint | Correctness | Messages | String.format string doesn't match the XML format string"
inspections are enabled.
Check how is it reported in the editor or via main menu / Analyze.
Provide resource in res/values/string.xml (note the space between '%' and 's'):
```
<string name="invalid_format">Invalid % s</string>
```
Java:
```
private void format() {
String format = getString(R.string.invalid_format, "format"); // inspection error is not reported
}
```
Notice that:
- inspection error is not reported in editor
- inspection error is only reported for strings.xml running Analyze | Inspect Code...
- running the app triggers crash with FormatFlagsConversionMismatchException