2023-12-22 10:07:51 +01:00

69 lines
2.2 KiB
Plaintext

:true: true
:false: false
:ops: !, &&, ||, ==, !=
== Why is this an issue?
include::../description.adoc[]
== How to fix it
include::../how-to-fix-it.adoc[]
[source,javascript,diff-id=1,diff-type=noncompliant]
----
if (someValue == true) { /* ... */ } // Noncompliant: Redundant comparison
if (someBooleanValue != true) { /* ... */ } // Noncompliant: Redundant comparison
if (booleanMethod() || false) { /* ... */ } // Noncompliant: Redundant OR
doSomething(!false); // Noncompliant: Redundant negation
----
Remove redundant boolean literals to improve readability.
[source,javascript,diff-id=1,diff-type=compliant]
----
if (someValue) { /* ... */ }
if (!someBooleanValue) { /* ... */ }
if (booleanMethod()) { /* ... */ }
doSomething(true);
----
== Resources
=== Documentation
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness[Equality comparisons and sameness]
* MDN web docs - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean[Boolean]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Refactor the code to avoid using this boolean literal.
'''
=== 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.
endif::env-github,rspecator-view[]