Skip to content

string-literal-equality

Javacorrectnessno fixdefault: warn

Reports == or != with a string literal as an operand.

== on strings compares references, not text. It works for two literals because they are interned and fails for a string that was built or read, so the code passes its tests and breaks on real data.

To fix, use equals: "a".equals(s).

Reported
class A {
...
return o == "s" || o.toString() != "t" || o.hashCode() == 1;
}
boolean ok(boolean done, String s) {
...
java:
lint:
rules:
string-literal-equality: warn # off, warn (default), error

Suppress one line with sudsy-disable-next-line string-literal-equality in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
checkstyle StringLiteralEquality full