A string is copied with new String, which only makes a duplicate

tx0/java:string-copy-constructorrecommendedwarningperformance

What it finds

Strings are immutable, so new String("text") or new String(name) only makes a second object with the same characters, which takes memory and defeats == on interned literals. The original string serves the same purpose.

Reported is new String(x) with a single argument of type String.

From the rule's source, string_copy_constructor.rs.

Example from the rule's tests
class Copies {  String copy(String name, char[] chars) {    String a = new String(name);tx0 reports: copies a string    String b = new String(chars);    return a + b;  }}
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 recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:string-copy-constructor" = "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:string-copy-constructor] reason="..."

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