Fred Tingaud d3cfe19d7e
Fix broken or dangerous backquotes
Co-authored-by: Marco Borgeaud <89914223+marco-antognini-sonarsource@users.noreply.github.com>
2023-10-30 10:33:56 +01:00

68 lines
1.7 KiB
Plaintext

include::../why.adoc[]
=== Exceptions
The rule ignores
* Initializations to `-1`, `0`, `1`, `undefined`, `[]`, `{}`, `true`, `false` and `""`.
* Variables that start with an underscore (e.g. ``++_unused++``) are ignored.
* Assignment of `null` is ignored because it is sometimes used to help garbage collection
* Increment and decrement expressions are ignored because they are often used idiomatically instead of `x+1`
* This rule also ignores variables declared with object destructuring using rest syntax (used to exclude some properties from object)
[source,javascript]
----
let {a, b, ...rest} = obj; // 'a' and 'b' are compliant
doSomething(rest);
let [x1, x2, x3] = arr; // 'x1' is noncompliant, as omitting syntax can be used: "let [, x2, x3] = arr;"
doSomething(x2, x3);
----
include::../howtofixit.adoc[]
=== Code examples
==== Noncompliant code example
[source,javascript,diff-id=1,diff-type=noncompliant]
----
function foo(y) {
let x = 100; // Noncompliant: dead store
x = 150; // Noncompliant: dead store
x = 200;
return x + y;
}
----
==== Compliant solution
[source,javascript,diff-id=1,diff-type=compliant]
----
function foo(y) {
let x = 200; // Compliant: no unnecessary assignment
return x + y;
}
----
include::../see.adoc[]
=== Related rules
* S1763 - All code should be reachable
* S2589 - Boolean expressions should not be gratuitous
* S3516 - Function returns should not be invariant
* S3626 - Jump statements should not be redundant
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]