question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

no-unused-vars incorrectly raised when variable is used with for…in

See original GitHub issue

The 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:closed
  • Created 8 years ago
  • Comments:40 (23 by maintainers)

github_iconTop GitHub Comments

4reactions
nzakascommented, Apr 20, 2015

Assignment to the variable is not considered usage. So this is not usage:

var x;
x=1;   // unused

That’s what the for-in loop is doing.

Assigning the variable to another variable is usage:

var x;
x=1;
var y=x;  // now x is used but y isn't
0reactions
ilyavolodincommented, Feb 11, 2016

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).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found