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 false positive for use on right side of assignment

See original GitHub issue

Tell us about your environment

What parser (default, Babel-ESLint, etc.) are you using?

Default

Please show your full configuration:

None, inline

What did you do? Please include the actual source code causing the issue.

/* eslint no-unused-vars: "error" */
// 'foo' is defined but never used. (no-unused-vars)
var foo;
foo = 42;
foo = foo();
foo = foo.bar();
foo = bar(foo);

// No error
var bar;
bar = 42;
bar();

What did you expect to happen?

No error. foo is used multiple times on the right side of the assignment expressions.

What actually happened? Please include the actual, raw output from ESLint.

3:5 - ‘foo’ is defined but never used. (no-unused-vars)

It looks like references are being excluded on both sides of assignment expressions instead of just the left side.

Originally seen on http://stackoverflow.com/questions/39787723/eslint-no-unused-vars-stream-conditional-pipe-use-case

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
btmillscommented, Sep 30, 2016

The left side of the final assignment expression is unnecessary, but the variable is “used”, at least according to my interpretation of the rule. You can look at the linked StackOverflow question for a less trivial example. In an ideal world (and I believe this is what you were getting at when you suggested warning on line 7 instead of the declaration), we’d be able to determine statically that the result of the final assignment expression is unused and suggest using the right side by itself. Even supposing that were possible (it may be, and that be really cool), I don’t think that warning should come from no-unused-vars. Put another way, my interpretation of no-unused-vars is that when it declares a variable as unused, I ought to be able to remove that variable from my program without changing the program’s behavior. Does that fit?

Also, welcome to the team! 🎉

0reactions
nzakascommented, Oct 3, 2016

@not-an-aardvark I’m open to changing the message that’s reported in order to avoid confusion in those cases. Maybe something like “foo is assigned a value but is never used” instead?

@mysticatea no need to apologize. It’s just that you didn’t leave a comment explaining why you removed “accepted,” so I wasn’t sure if you disagreed that this was a bug, or something else.

Read more comments on GitHub >

github_iconTop Results From Across the Web

typescript-eslint/no-unused-vars false positive in type ...
And we have @typescript-eslint/no-unused-vars error in string with type declaration, which says 'name' is defined but never used.
Read more >
no-unused-vars - ESLint - Pluggable JavaScript Linter
This rule is aimed at eliminating unused variables, functions, and function parameters. A variable foo is considered to be used if any of...
Read more >
babel-plugin-remove-unused-vars - npm
This plugin is NOT perfect. It'll have false-positives and remove used variables or sometimes keep unused variables around.
Read more >
babel-plugin-remove-unused-vars - npm package - Snyk
This plugin is NOT perfect. It'll have false-positives and remove used variables or sometimes keep unused variables around. It's highly rare but still...
Read more >
Warning Options (Using the GNU Compiler Collection (GCC))
That option can then be used with -Werror= and -Wno-error= as described above. ... flow sensitive warnings, in some cases it may also...
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