string-literal-equality
What it does
Section titled “What it does”Reports == or != with a string literal as an operand.
Why it matters
Section titled “Why it matters”== 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).
Example
Section titled “Example”class A {... return o == "s" || o.toString() != "t" || o.hashCode() == 1; }
boolean ok(boolean done, String s) {...Configuration
Section titled “Configuration”java: lint: rules: string-literal-equality: warn # off, warn (default), errorSuppress one line with sudsy-disable-next-line string-literal-equality in a comment of the file’s language.
Upstream equivalents
Section titled “Upstream equivalents”The same check in other tools; Sudsy reports what each upstream rule reports.
| Tool | Rule | Match |
|---|---|---|
| checkstyle | StringLiteralEquality |
full |