Used variable is being reported as unused in a small edge case.
See original GitHub issueWhat version of ESLint are you using?
3.1.1
What parser (default, Babel-ESLint, etc.) are you using? Online Demo
Please show your full configuration:
{
"rules": {
"no-unused-vars": "error"
}
}
What did you do? Please include the actual source code causing the issue.
function logDigits(string) {
string = string.replace(/\d/, console.log)
}
logDigits('abc-123');
What did you expect to happen?
I expected to see an error on line 2 indicating that I didn’t use the “replaced” version of string
.
What actually happened? Please include the actual, raw output from ESLint.
1:20 error 'string' is defined but never used no-unused-vars
✖ 1 problem (1 error, 0 warnings)
This code does have an issue, the assigned version of string
is not used and should be removed. However, the way it is reported seems to indicate that the string argument is not used at all, which is not true since the execution of the replace
callback is dependant upon which the value of string
that you pass it.
I’m guessing this is related to https://github.com/eslint/eslint/issues/6348.
Issue Analytics
- State:
- Created 7 years ago
- Comments:23 (21 by maintainers)
Top GitHub Comments
I’m of the mind that we should just update the docs to explain this case. The rule is behaving as intended and the only issue is that some may find the error message confusing. Error messages are short and are not meant to encompass all of the meaning on their own, that is precisely why we have documentation for rules: so we can provide more information than that single phrase can.
So I’m 👎 for any code changes and 👍 for clarifying this use case in the docs.
Closing this issue as fixed; the error message was improved in https://github.com/eslint/eslint/pull/7315, which hopefully clarifies the error.