Improperly formatted string arguments Info

Format strings appropriately for their argument types. For example, use %d, not %s, for integers. This ensures locale-sensitive formatting.

Detector ID
java/string-format-arguments@v1.0
Category
Common Weakness Enumeration (CWE) external icon
-

Noncompliant example

1void formatStringNoncompliant(final File file) {
2    final long length = file.length();
3    // Noncompliant: avoids using the correct format strings for their argument types.
4    final String s = String.format("File length is %s", length);
5    log.info(s);
6}

Compliant example

1void formatStringCompliant(final File file) {
2    final long length = file.length();
3    // Compliant: uses the correct format strings for their argument types.
4    final String s = String.format("File length is %d", length);
5    log.info(s);
6}