The same string literal is written several times in a file

tx0/java:repeated-string-literalstrictwarningmaintainability

What it finds

The same string literal written many times in one file, as a key, a header name or a message, has to be changed in every place at once, and a typo in one copy goes unnoticed. A constant keeps it in one place.

Counted are string literals of at least six characters, outside annotations and case labels. A literal that occurs at least the parameter min-count times, 3 by default, is reported once, at its first occurrence.

From the rule's source, repeated_string.rs.

Example from the rule's tests
import java.util.Map; class Headers {  void fill(Map<String, String> headers) {    headers.put("content-type", "text/plain");tx0 reports: the string content-type occurs 3 times in this file    headers.remove("content-type");    headers.get("content-type");    headers.put("accept", "text/plain");  }}
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 from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:repeated-string-literal" = "warning"

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:repeated-string-literal] reason="..."

tx0 explain tx0/java:repeated-string-literal shows the same in the terminal. Found a false positive? Report it.