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
no-unused-vars incorrectly raised when variable is used with for…in #2342
Comments
In the code you provided, key is actually not used for anything. This rule considers a variable to be used when it's value is assigned to something, or used as part of the call. In your case, neither is done. |
It seems that “usage” is broader than just assignments or calls. Here is a meaningful example: function countNumberOfKeys(object) {
var key, i = 0;
for (key in object)
i++;
return i;
} It would not seem right to have the above example linted by setting an exception for that line only. |
We probably need to add this exception for both |
And thinking about this some more: |
Assignment to the variable is not considered usage. So this is not usage:
That's what the Assigning the variable to another variable is usage:
|
Indeed; I misread the definition above ("when its value is assigned to something"). I guess |
@nzakas I think we can close this as looking at the comments there is no issue here. |
I don't think closing is the appropriate action at this time; @nzakas said On Sunday, May 17, 2015, Gyandeep Singh notifications@github.com wrote:
|
Yeah, we need to add the exception for these loops. |
I don't think we should make an exception for these. They are unused. You can disable the rule for this line. I believe a previous issue also proposed that particular names be exempt from this rule: |
It all depends on your exact definition of used. The variable is not read, no. But it is written to. And there is no way to write this code without the variable, nor is it necessary to read it. The variable is used in the sense that it is necessary for the code to have the variable. |
Also note that there is no way to write a functionally identical piece of code without switching off at least one warning: either |
We've already defined usage as a read. I'm claiming that this is not a common or important pattern. I would use |
I would then suggest clarifying on http://eslint.org/docs/rules/no-unused-vars.html that |
Somewhat unrelated, @RubenVerborgh unless I completely missed something, code you posted above is also O(n) and is an exact equivalent of what @michaelficarra suggested. |
I don't want to start a code-complexity war here 😉, but: for (key in this) return; only needs to assign one value to The code I posted was a simplified example from a real-word function, which needs to return I keep finding it strange that |
The problem I see here is that turning off the rule around an area is the nuclear option, you could be missing other unused variables. I think we need an exception of some sort, and am open to suggestions. |
Could we pass an option to the |
Passing an option is an option (ha!) but the problem with options is always how to do them in such a way that they make sense and aren't overly broad or narrow. Like, do you really want to always to exempt all for (var foo in bar) {
doSomething(bar);
} In this case, it's more likely than not that you meant to include If we then say only your specific case I'm very much against adding options that could lead to you missing an error - we always try to add options that always do the right thing so that there's no way an end user can choose incorrectly and end up missing something. |
There seems to be no way in general to guard against the BTW For a couple of scenarios where this case occurs, see this code, in particular 46–49 and 70–74. |
We'd need a more generic message, as declarations aren't always assignments and I really don't want to special-case messaging for assignments. We could maybe do I'm a bit concerned that using "read" instead of "used" is introducing more ambiguity, but I'd like to hear opinions on that. |
I prefer the message use "used" as it does now. If someone doesn't understand that, the documentation should describe in detail what actually constitutes a "use" with examples. |
Yeah, I thought we had updated the docs at some point, but apparently not. We should do that. So, circling back to the original point, I don't think it's safe to have a flag that blocks warnings for all |
for (var key in object) return; |
Sorry, I misread "is the" as "is in the". |
In case it's relevant, I don't remember either JSHint or JSCS complaining about my code, so they may have done something like this. |
Pretty sure JSCS doesn't check variable usage, and JSHint has only very basic usage checking that tends to miss things. I still don't see any good solution to this. We can choose to ignore I'm definitely against the @ilyavolodin who else brought this problem up? |
It was brought up a few times in gitter chat room, I believe. I also don't see a good solution for the problem. |
Here's my proposal: we just always allow patterns like If there's disagreement on that, then I'd say this is best left to a custom rule. |
I think I can buy exception for |
Marked this as accepted and removed documentation as it seems the decision was reached to make an implicit exception for |
…#2342) Also clean jsdocs for internal helpers
…int#2342) Also clean jsdocs for internal helpers
…ixes eslint#2342) Also clean jsdocs for internal helpers
Applied only for `for...in` loops with only one `return` statement. Also clean jsdocs for internal helpers
Applied only for `for...in` loops with only one `return` statement. Also clean jsdocs for internal helpers
Applied only for `for...in` loops with only one `return` statement. Also clean jsdocs for internal helpers
Applied only for `for...in` loops with only one `return` statement. Also clean jsdocs for internal helpers
Applied only for `for-in` loops with only one `return` statement. Also clean jsdocs for internal helpers
Applied only for `for-in` loops with only one `return` statement. Also clean jsdocs for internal helpers
Applied only for `for-in` loops with only one `return` statement. Also clean jsdocs for internal helpers
Applied only for `for-in` loops with only one `return` statement. Also clean jsdocs for internal helpers
Applied only for `for-in` loops with only one `return` statement. Also clean jsdocs for internal helpers
Applied only for `for-in` loops with only one `return` statement. Also clean jsdocs for internal helpers
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
:However, the variable is clearly used in
for (key in this)
.Furthermore, omitting the declaration also results in an error:
The expected behavior is that the first snippet does not yield any error; the second error is correct.
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: