rspec/rules/S1127/rule.adoc

35 lines
718 B
Plaintext
Raw Normal View History

Strings, just like any other ``++Object++``, should be compared using the ``++equals()++`` method.
Using ``++==++`` or ``++!=++`` compares references rather than values, and usually does not work.
== Noncompliant Code Example
----
if (variable == "foo") { /* ... */ }
if (variable != "foo") { /* ... */ }
----
== Compliant Solution
----
if ("foo".equals(variable)) { /* ... */ }
if (!"foo".equals(variable)) { /* ... */ }
----
== See
2021-10-28 10:07:16 +02:00
* https://cwe.mitre.org/data/definitions/597.html[MITRE, CWE-597] - Use of Wrong Operator in String Comparison
2022-01-25 18:36:46 +01:00
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]