-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
no-useless-assignment
rule (#17625)
* feat: add `no-useless-assignment` rule * fix: add test cases and fix * chore: remove wrong comment * test: add test and update * feat: report if it is not used between assignments * fix: add test case and fix * Apply suggestions from code review Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> * docs: update * fix: false negatives for same segments & refactor * fix: rule-types.json * fix: add update assignment test case & fix * fix: add test for global vars & fix * Update docs/src/rules/no-useless-assignment.md Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> * docs: update description * docs: add further_reading * chore: improve jsdoc and rename function * docs: update doc * fix: false positives for try catch * test: fix tests/conf/eslint-all.js * feat: add no-useless-assignment to eslint-config-eslint * docs: fix document * Update docs/src/rules/no-useless-assignment.md Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * fix: false negatives * docs: add example code * test: move test case * fix: false positives for assignment pattern * test: use flat-rule-tester * Update lib/rules/no-useless-assignment.js Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com> * fix: false positives for `finally` block * fix: false positives for markVariableAsUsed() * chore: rename function * fix: false positives for unreachable segments * Update lib/rules/no-useless-assignment.js Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> * test: revert tests/conf/eslint-all.js * test: use rule-tester --------- Co-authored-by: Nicholas C. Zakas <nicholas@humanwhocodes.com> Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
- Loading branch information
1 parent
806f708
commit b4e0503
Showing
25 changed files
with
1,800 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
--- | ||
title: no-unused-vars | ||
rule_type: problem | ||
related_rules: | ||
- no-useless-assignment | ||
--- | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
--- | ||
title: no-useless-assignment | ||
rule_type: suggestion | ||
related_rules: | ||
- no-unused-vars | ||
further_reading: | ||
- https://en.wikipedia.org/wiki/Dead_store | ||
- https://rules.sonarsource.com/javascript/RSPEC-1854/ | ||
- https://cwe.mitre.org/data/definitions/563.html | ||
- https://wiki.sei.cmu.edu/confluence/display/c/MSC13-C.+Detect+and+remove+unused+values | ||
- https://wiki.sei.cmu.edu/confluence/display/java/MSC56-J.+Detect+and+remove+superfluous+code+and+values | ||
--- | ||
|
||
|
||
[Wikipedia describes a "dead store"](https://en.wikipedia.org/wiki/Dead_store) as follows: | ||
|
||
> In computer programming, a local variable that is assigned a value but is not read by any subsequent instruction is referred to as a **dead store**. | ||
"Dead stores" waste processing and memory, so it is better to remove unnecessary assignments to variables. | ||
|
||
Also, if the author intended the variable to be used, there is likely a mistake around the dead store. | ||
For example, | ||
|
||
* you should have used a stored value but forgot to do so. | ||
* you made a mistake in the name of the variable to be stored. | ||
|
||
```js | ||
let id = "x1234"; // this is a "dead store" - this value ("x1234") is never read | ||
|
||
id = generateId(); | ||
|
||
doSomethingWith(id); | ||
``` | ||
|
||
## Rule Details | ||
|
||
This rule aims to report variable assignments when the value is not used. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
::: incorrect | ||
|
||
```js | ||
/* eslint no-useless-assignment: "error" */ | ||
|
||
function fn1() { | ||
let v = 'used'; | ||
doSomething(v); | ||
v = 'unused'; | ||
} | ||
|
||
function fn2() { | ||
let v = 'used'; | ||
if (condition) { | ||
v = 'unused'; | ||
return | ||
} | ||
doSomething(v); | ||
} | ||
|
||
function fn3() { | ||
let v = 'used'; | ||
if (condition) { | ||
doSomething(v); | ||
} else { | ||
v = 'unused'; | ||
} | ||
} | ||
|
||
function fn4() { | ||
let v = 'unused'; | ||
if (condition) { | ||
v = 'used'; | ||
doSomething(v); | ||
return | ||
} | ||
} | ||
|
||
function fn5() { | ||
let v = 'used'; | ||
if (condition) { | ||
let v = 'used'; | ||
console.log(v); | ||
v = 'unused'; | ||
} | ||
console.log(v); | ||
} | ||
``` | ||
|
||
::: | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
::: correct | ||
|
||
```js | ||
/* eslint no-useless-assignment: "error" */ | ||
|
||
function fn1() { | ||
let v = 'used'; | ||
doSomething(v); | ||
v = 'used-2'; | ||
doSomething(v); | ||
} | ||
|
||
function fn2() { | ||
let v = 'used'; | ||
if (condition) { | ||
v = 'used-2'; | ||
doSomething(v); | ||
return | ||
} | ||
doSomething(v); | ||
} | ||
|
||
function fn3() { | ||
let v = 'used'; | ||
if (condition) { | ||
doSomething(v); | ||
} else { | ||
v = 'used-2'; | ||
doSomething(v); | ||
} | ||
} | ||
|
||
function fn4() { | ||
let v = 'used'; | ||
for (let i = 0; i < 10; i++) { | ||
doSomething(v); | ||
v = 'used in next iteration'; | ||
} | ||
} | ||
``` | ||
|
||
::: | ||
|
||
This rule will not report variables that are never read. | ||
Because it's clearly an unused variable. If you want it reported, please enable the [no-unused-vars](./no-unused-vars) rule. | ||
|
||
::: correct | ||
|
||
```js | ||
/* eslint no-useless-assignment: "error" */ | ||
|
||
function fn() { | ||
let v = 'unused'; | ||
v = 'unused-2' | ||
doSomething(); | ||
} | ||
``` | ||
|
||
::: | ||
|
||
## When Not To Use It | ||
|
||
If you don't want to be notified about values that are never read, you can safely disable this rule. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.