no-unused-vars incorrectly raised when variable is used with for…in
See original GitHub issueThe no-unused-vars error is incorrectly triggered when a variable is only used in a for in
statement.
The following snippet gives the error no-unused-vars
:
(function () {
"use strict";
var key;
for (key in this) return;
})();
3:6 error key is defined but never used no-unused-vars
However, the variable is clearly used in for (key in this)
.
Furthermore, omitting the declaration also results in an error:
(function () {
"use strict";
for (key in this) return;
})();
3:7 error "key" is not defined no-undef
The expected behavior is that the first snippet does not yield any error; the second error is correct.
<bountysource-plugin>
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource. </bountysource-plugin>
Issue Analytics
- State:
- Created 8 years ago
- Comments:40 (23 by maintainers)
Top Results From Across the Web
typescript-eslint/no-unused-vars raises a warn where it shouldn't
I can't achive argv value without achiving env . Is the only way to avoid the error in this case - is to...
Read more >no-unused-vars - ESLint - Pluggable JavaScript Linter
A variable is not considered to be used if it is only ever declared ( var foo = 5 ) or assigned to...
Read more >User input and error handling - Various writings
If something goes wrong when executing the statements in the try block, Python raises what is known as an exception. The execution jumps...
Read more >Typescript Eslint Incorrectly Reports: "Function Is Defined But ...
This rule is aimed at eliminating unused variables, functions and variables in parameters of functions, 'y' is defined but never used*/ y 5;...
Read more >no-unused-vars - TypeScript ESLint
How to Use .eslintrc.cjs. module.exports = { "rules": { // Note: you must disable the base rule as it can report incorrect errors...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Assignment to the variable is not considered usage. So this is not usage:
That’s what the
for-in
loop is doing.Assigning the variable to another variable is usage:
Marked this as accepted and removed documentation as it seems the decision was reached to make an implicit exception for
for (x in y) return;
pattern only (and same pattern for for…of).