未使用的格式参数¶
ID: java/unused-format-argument
Kind: problem
Security severity:
Severity: warning
Precision: very-high
Tags:
- maintainability
- useless-code
- external/cwe/cwe-685
Query suites:
- java-security-and-quality.qls
在使用 printf
样式格式字符串格式化字符串时,必须确保提供的参数数量与格式字符串引用的参数数量匹配。额外的参数将被静默丢弃,这可能不是预期行为,而参数不足会导致 IllegalFormatException
。
格式字符串由 String
、Formatter
、Console
、PrintWriter
和 PrintStream
类上的 format
函数使用。这些类中的几个也提供函数别名 printf
。类 Console
有两个额外的函数 readLine
和 readPassword
,它们也使用格式字符串。
建议¶
更改格式字符串以使用所有参数,或删除不必要的参数。
示例¶
以下示例提供了三个要格式化的参数,但格式字符串只引用了两个参数,因此这将静默忽略第三个参数。
System.out.format("First string: %s Second string: %s", "Hello", "world", "!");