68 lines
1.7 KiB
Plaintext
Raw Normal View History

2023-10-13 12:26:37 +02:00
include::../why.adoc[]
2020-06-30 12:47:33 +02:00
=== Exceptions
2020-06-30 12:47:33 +02:00
2023-10-13 12:26:37 +02:00
The rule ignores
* Initializations to `-1`, `0`, `1`, `undefined`, `[]`, `{}`, `true`, `false` and `""`.
* Variables that start with an underscore (e.g. ``++_unused++``) are ignored.
2023-10-13 12:26:37 +02:00
* 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`
2023-10-13 12:26:37 +02:00
* This rule also ignores variables declared with object destructuring using rest syntax (used to exclude some properties from object)
[source,javascript]
2020-06-30 12:47:33 +02:00
----
2023-10-13 12:26:37 +02:00
let {a, b, ...rest} = obj; // 'a' and 'b' are compliant
2020-06-30 12:47:33 +02:00
doSomething(rest);
2023-10-13 12:26:37 +02:00
let [x1, x2, x3] = arr; // 'x1' is noncompliant, as omitting syntax can be used: "let [, x2, x3] = arr;"
2020-06-30 12:47:33 +02:00
doSomething(x2, x3);
----
2023-10-13 12:26:37 +02:00
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;
}
----
2020-06-30 12:47:33 +02:00
include::../see.adoc[]
2023-10-13 12:26:37 +02:00
=== 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[]