63 lines
1.8 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-01-27 13:42:22 +01:00
Boolean literals should be avoided in comparison expressions ``++==++`` and ``++!=++`` to improve code readability.
2021-02-02 15:02:10 +01:00
This rule also reports on redundant boolean operations.
=== Noncompliant code example
2022-02-04 17:28:24 +01:00
[source,javascript]
----
let someValue = "0";
// ...
if (someValue == true) { /* ... */ }
if (someBooleanValue != true) { /* ... */ }
doSomething(!false);
----
=== Compliant solution
2022-02-04 17:28:24 +01:00
[source,javascript]
----
if (someValue && someValue != "0") { /* ... */ }
if (!someBooleanValue) { /* ... */ }
doSomething(true);
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Refactor the code to avoid using this boolean literal.
'''
== Comments And Links
(visible only on this page)
=== on 10 Mar 2017, 17:04:41 Elena Vilchik wrote:
Rule is deprecated as it's a wrong in JS:
----
"0" == false // true
Boolean("0") // true
----
=== on 2 Nov 2017, 20:03:36 Ann Campbell wrote:
\[~elena.vilchik] if it's wrong, why not just remove it? I'm not generally in favor of jerking the rug out from under users in that manner, but what you're saying is essentially that this rule _is_ a bug. As such, it should be fixed / removed.
=== on 6 Nov 2017, 14:50:08 Elena Vilchik wrote:
\[~ann.campbell.2] I see your point, we deprecated it as the matter of habit. We will remove the rule in the next version (\https://github.com/SonarSource/SonarJS/issues/825), there is no point in fixing it.
=== on 4 Dec 2017, 09:44:32 Elena Vilchik wrote:
Eventually we decided to keep the rule but for ``++==++`` and ``++!=++`` use different message. Even though there are some cases then simple drop of literal will break the code, code is still smelly and should be refactored.
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]