FormatString
Invalid printf-style format string

Severity
ERROR

The problem

Format strings for the printf family of functions must follow the specification in the documentation for java.util.Formatter.

The syntax for format specifiers is:

%[argument_index$][flags][width][.precision]conversion

Format strings can have the following errors:

DuplicateFormatFlags

Duplicate flags are provided in the format specifier:

String.format("e = %++10.4f", Math.E);

FormatFlagsConversionMismatch

A conversion and flag are incompatible:

String.format("%#b", Math.E);

IllegalFormatCodePoint

The argument is a character with an invalid Unicode code point.

String.format("%c", 0x110000);

IllegalFormatConversion

The argument corresponding to the format specifier is of an incompatible type:

String.format("%f", "abcd");

IllegalFormatFlags

An illegal combination of flags is given:

String.format("%-010d", 5);

IllegalFormatPrecision

The conversion does not support a precision:

String.format("%.c", 'c');

IllegalFormatWidth

The conversion does not support a width:

String.format("%1n");

MissingFormatArgument

There is a format specifier which does not have a corresponding argument or if an argument index refers to an argument that does not exist:

String.format("%<s", "test");

MissingFormatWidth

The format width is required:

String.format("e = %-f", Math.E);

UnknownFormatConversion

An unknown conversion is given:

String.format("%r", "hello");

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("FormatString") to the enclosing element.