Skip to content
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 does not report error when comma operator is used #14325

Closed
M393 opened this issue Apr 15, 2021 · 6 comments · Fixed by #14354
Closed

no-unused-vars does not report error when comma operator is used #14325

M393 opened this issue Apr 15, 2021 · 6 comments · Fixed by #14354
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly repro:yes
Projects

Comments

@M393
Copy link

M393 commented Apr 15, 2021

With no-unused-vars enabled eslint does not mark a variable as unused if assignment is combined with the comma operator.

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

Both produce no warning:

let x = 0;
x++, x = 0;
let x = 0;
x++, x = 0;
x = 3;

What did you expect to happen?
It should mark the last occurance of x as an unused variable.

What actually happened? Please copy-paste the actual, raw output from ESLint.
eslint thinks everything is fine.

Steps to reproduce this issue:

  1. eslint demo

Are you willing to submit a pull request to fix this bug?
No

@M393 M393 added bug ESLint is working incorrectly repro:needed labels Apr 15, 2021
@eslint-github-bot eslint-github-bot bot added this to Needs Triage in Triage Apr 15, 2021
@nzakas
Copy link
Member

nzakas commented Apr 16, 2021

This is occurring because of the x++, not because of the comma operator. The post fix increment operation counts as both a read and a write, which is why there is no warning.

This looks like a bug but I’d like another set of eyes. @eslint/eslint-tsc

@nzakas nzakas moved this from Needs Triage to Evaluating in Triage Apr 16, 2021
@M393
Copy link
Author

M393 commented Apr 16, 2021

The increment operator also writes (to itself) after reading.
It has to be related to the comma operator in some way.
These are correctly marked:

let x = 0;
x++;
^
let x = 0;
x++; x = 0;
     ^

@nzakas
Copy link
Member

nzakas commented Apr 17, 2021

It appears you’re right. Here is an example without the increment operator that makes it clear:

let x = 0;
x=2, x = 0;
x = 3;

@nzakas nzakas added the accepted There is consensus among the team that this change meets the criteria for inclusion label Apr 17, 2021
@nzakas nzakas moved this from Evaluating to Ready to Implement in Triage Apr 17, 2021
@snitin315
Copy link
Contributor

I will take this issue 👍🏻

@mdjermanovic mdjermanovic moved this from Ready to Implement to Pull Request Opened in Triage Apr 28, 2021
@aladdin-add
Copy link
Member

seems it was not exactly related to comma operator. imagine this code:

var a;
a; // => rvalue

a will not be marked unused. (demo).
Technologically, it's working as expected. the 2nd a was used as rvalue (its value was read).

and these cases can be caught by some other rules:

var a;
a; // => caught by no-unused-expressions
var a;
a, b; // caught by no-sequences

@mdjermanovic
Copy link
Member

and these cases can be caught by some other rules:

var a;
a; // => caught by no-unused-expressions

I think it's fine to not account for this in no-unused-vars, because a; itself is a possible error and it's reasonable to expect that no-unused-expressions is enabled.

var a;
a, b; // caught by no-sequences

no-sequences is mostly stylistic, but this is also caught by no-unused-expressions.

Triage automation moved this from Pull Request Opened to Complete May 21, 2021
mdjermanovic pushed a commit that referenced this issue May 21, 2021
…) (#14354)

* Fix: report error for sequence expression in no-unused-vars (fixes #14325)

* Chore: add tests

* Update: suggestions

* Chore: refactor

* Chore: refactor

* Fix: logic

* Chore: add tests
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators Nov 18, 2021
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Nov 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly repro:yes
Projects
No open projects
Triage
Complete
Development

Successfully merging a pull request may close this issue.

6 participants
@nzakas @aladdin-add @mdjermanovic @snitin315 @M393 and others