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

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>

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 40 (23 by maintainers)

Commits related to this issue

Most upvoted comments

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