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 should warn on the offending assignment instead of declaration

See original GitHub issue

Tell us about your environment

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

Please show your full configuration: default

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint. Simple example (from some actual immediate-mode UI code that takes in a y position and returns a height of what was rendered).

function print(v) {
  console.log(v);
  return 1;
}
let y = 1;
y += print(y);
y += print(y);
y += print(y);

eslint.org/demo#…

What did you expect to happen? No error, or, potentially an error on the first unused assignment: 8:1 - 'y' is assigned a value but never used. (no-unused-vars)

What actually happened? Please include the actual, raw output from ESLint. 5:5 - 'y' is assigned a value but never used. (no-unused-vars)

Are you willing to submit a pull request to fix this bug? Probably not, but potentially.

Edits: Title was previously “no-unused-vars erroneously fails if a line both uses and modifies a value via increment”, and I was confused by the error being on the declaration of y instead of the last (actually unused) assignment.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:19 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
gabrieldrscommented, Feb 4, 2020

Although it reports the error at line 5, the actual error seems to occur because in your last line you assign a value to y but don’t use it. If you replace your last line with print(y); the error goes away.

I think this is the expected behaviour, isn’t it?

1reaction
Jimblycommented, Mar 9, 2020

The inherent problem is the code is actually detecting the variable as completely unused (it’s treating x = f(x) as syntactically equivalent to x++), I think it might just be better to fix the code to consider that “used” (error message would completely go away, just like in let x=1; f(x); x = f(x);. The most "correct’ error message when your code looks like let x = 1; x=x+1; would be something like 'x' is assigned a value but only ever assigned back to itself, but that’s not even correct in the x=f(x) case. If you can figure out how to differentiate those cases inside of the isSelfReference logic in order to display a different error message, you could just fix the bug =)

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
How to disable warn about some unused params, but keep ...
1 Answer 1 · 8. To ignore unused variables that begin with an underscore use varsIgnorePattern instead of argsIgnorePattern . – haymez ·...
Read more >
no-unused-vars - TypeScript ESLint
Disallow unused variables. Extending "plugin:@typescript-eslint/recommended" in an ESLint configuration enables this rule.
Read more >
no-unused-vars incorrectly raised when variable is used with ...
The no-unused-vars error is incorrectly triggered when a variable is only used in a for in statement. ... However, the variable is clearly...
Read more >
Ruby Style Guide
This convention is recognized by the Ruby interpreter and tools like RuboCop will suppress their unused variable warnings. # bad result = hash.map...
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