Skip to content

[no-unused-vars] Wrong position reported #4910

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

Closed
bachorp opened this issue May 5, 2022 · 11 comments · Fixed by #5267
Closed

[no-unused-vars] Wrong position reported #4910

bachorp opened this issue May 5, 2022 · 11 comments · Fixed by #5267
Assignees
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@bachorp
Copy link

bachorp commented May 5, 2022

Repro

{
    "parser": "@typescript-eslint/parser",
    "plugins": [
        "@typescript-eslint"
    ],
    "rules": {
        "@typescript-eslint/no-unused-vars":
            "error"
    }
}
function foo(x: null) {
    return x
}

let x = null

x = foo(x)

Expected Result

7:1  error  'x' is assigned a value but never used.  @typescript-eslint/no-unused-vars

x = foo(x)
^ error here

Actual Result

7:9  error  'x' is assigned a value but never used.  @typescript-eslint/no-unused-vars

x = foo(x)
        ^ error here

Versions

package version
@typescript-eslint/eslint-plugin 5.22.0
@typescript-eslint/parser 5.22.0
TypeScript 4.6.2
ESLint 8.14.0
node 16.12.0
@bachorp bachorp added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels May 5, 2022
@bradzacher
Copy link
Member

bradzacher commented May 5, 2022

Could you more clearly annotate which position you expect and which position you got?

Eg do this

let x = 1
//  ^ error here

You said you expect 1:1 which means line 1 column 1 which is clearly wrong because that would suggest you want the function keyword underlined in your example code.

@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party and removed triage Waiting for team members to take a look labels May 5, 2022
@bachorp
Copy link
Author

bachorp commented May 5, 2022

@bradzacher Sorry, I updated my comment

@bradzacher
Copy link
Member

@bachorp theres only 7 lines in your example. So 9:1 doesn't make much more sense.

As per my last comment (I edited after you saw it I think) could you annotate your example code with comments showing the actual and expected locations?

@bachorp
Copy link
Author

bachorp commented May 5, 2022

@bradzacher Done

@bradzacher
Copy link
Member

The rule is actually right here

let x = null

In this code the variable x is not used.

let x = null
foo(x)

In this code the variable x is used after it is assigned a value.

let x = null
foo(x)
x = 1 // error on this line

In this code you will have an error as the last line assigns a value to x which is never used.

Putting these together:

    let x = null

    x = foo(x)
//          ^ usage of the declaration ✅
//  ^ new assignment to x which is never used  ❌

@bradzacher bradzacher added working as intended Issues that are closed as they are working as intended and removed awaiting response Issues waiting for a reply from the OP or another party labels May 5, 2022
@bachorp
Copy link
Author

bachorp commented May 5, 2022

@bradzacher Can you please clarify at which position we would expect the error? If I understand your example correctly (we expect an error at ❌ but not at ✅), the rule is not right here.

@bradzacher bradzacher reopened this May 5, 2022
@bradzacher bradzacher added bug Something isn't working accepting prs Go ahead, send a pull request that resolves this issue and removed working as intended Issues that are closed as they are working as intended labels May 5, 2022
@armano2 armano2 self-assigned this Jun 27, 2022
@armano2
Copy link
Collaborator

armano2 commented Jun 27, 2022

there actually seem to be additional issues, and some of test seem to be incorrect,

interface Foo {
  bar: string;
  baz: Foo['bar'];
  //   ^ reports this
}

@bradzacher
Copy link
Member

@armano2 - that's the same issue - we're reporting the last read instead of the last write.

I don't think we ever merged this from upstream: eslint/eslint@5df5e4a

Separately there are the following commits we should assess and merge in as well:

@armano2
Copy link
Collaborator

armano2 commented Jun 27, 2022

@bradzacher correct, we do not have those changes / our rule is not reporting on them correctly, do you mind filling separate issue about this?

@bradzacher
Copy link
Member

I also didn't realise that this was a duplicate of #3311 oops

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
4 participants
@armano2 @bradzacher @bachorp and others