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.

Used variable is being reported as unused in a small edge case.

See original GitHub issue

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

github_iconTop GitHub Comments

3reactions
nzakascommented, Jul 19, 2016

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.

0reactions
not-an-aardvarkcommented, Oct 10, 2016

Closing this issue as fixed; the error message was improved in https://github.com/eslint/eslint/pull/7315, which hopefully clarifies the error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Temporarily allow unused variables - Google Groups
One of my techniques for finding it, is isolate the problem to a very small case, by commenting out pieces of the code,...
Read more >
Why am I getting "Local variable unused in function" warning?
You get the error "Local variable 'rank' value not used" on the line rank = i + 1 because the the break statement...
Read more >
No warning for unused variable when declared const
This limitation has been fixed and unused const/constexpr variables will be reported as not used in an upcoming release of Visual Studio.. Click...
Read more >
Dead-code elimination - Wikipedia
In compiler theory, dead-code elimination is a compiler optimization to remove code which ... and code that only affects dead variables (written to,...
Read more >
7 Code Refactoring Techniques in Software Engineering
Being a developer how do you start working on a new project… ... Later you also change the code to fix the bug...
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