A format string and its arguments do not match

tx0/java:format-argumentsdefaultwarningbug

What it finds

String.format("%s: %d", name) throws MissingFormatArgumentException: the format asks for two arguments and gets one. An argument the format never uses is dropped without a word, which is as likely a mistake. A conversion that does not exist, such as %i, throws too.

Checked are String.format, formatted on a string and printf and format on System.out and System.err, with a string literal as the format. A single argument whose type is an array, or not known, may be the array of all arguments, and its count is not compared.

From the rule's source, format_arguments.rs.

Example from the rule's tests
import java.util.Locale; class Messages {  String describe(String name, int count, Object[] all, Object any) {    String a = String.format("%s has %d items", name);tx0 reports: the format asks for 2 arguments but gets 1    String b = String.format("%s", name, count);tx0 reports: the format uses 1 argument but gets 2    String c = String.format(Locale.ROOT, "%s and %s", name);tx0 reports: asks for 2 arguments but gets 1    String d = "%s: %d".formatted(name);tx0 reports: asks for 2 arguments but gets 1    System.out.printf("%d%n", count, count);tx0 reports: uses 1 argument but gets 2    String e = String.format("%i", count);tx0 reports: %i is not a format conversion    String f = String.format("%1$s %1$s %<s %n %%", name);    String g = String.format("%s %s", all);    String h = String.format("%s %s", any);tx0 reports: asks for 2 arguments but gets 1    String i = String.format("%-10s|%08.3f|%,d", name, 1.5, count);    String j = String.format("%tY-%<tm", any);    String k = String.format("100%%");    return a + b + c + d + e + f + g + h + i + j + k;  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/java:format-arguments" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:format-arguments] reason="..."

tx0 explain tx0/java:format-arguments shows the same in the terminal. Found a false positive? Report it.