-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add no-useless-assignment
rule
#17625
Changes from all commits
fa14942
020ee4f
35327ba
ca5c5c2
3d63f0f
7e2692e
6bb7f65
fb050c3
eb5e982
d9e3769
c8ab128
d3c1223
96dd2ac
1a2efc0
e21b292
06f9698
a68ab8d
09a2a22
a102c12
5c56a3e
6ca9835
153e5dc
91c0a23
d4683ce
7bb5240
360674d
7e696f8
a2b9339
d0e6ef9
6fce049
a83dfde
8a10c7f
a6e5843
cf5dd30
b3d0d92
8ecc87c
011330a
adf0fb4
fe8053b
f616420
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
--- | ||
|
||
|
||
|
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We still need more in this section. Please show an example. |
||
For example, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I don't think I was clear. I'm looking for a code example in this section, something like: let id = "x1234"; // this is a "dead store" - this value is never read
id = generateId();
doSomethingWith(id); That way, we can explain that |
||
|
||
* 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); | ||
} | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As it is now, the code in this example cannot be parsed in the Playground with the default |
||
|
||
::: | ||
|
||
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'; | ||
} | ||
} | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, the function names in this code block should be unique. |
||
|
||
ota-meshi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
::: | ||
|
||
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make sure to include more references for people. See the links in #17559 (comment)
We can use the "further_reading" key to list these out.