Skip to content

False positive warning: unused import when var symbol is used only as assignment target #22970

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
OndrejSpanel opened this issue Apr 11, 2025 · 0 comments · Fixed by #22977
Closed
Assignees
Labels
area:linting Linting warnings enabled with -W or -Xlint itype:bug

Comments

@OndrejSpanel
Copy link
Member

OndrejSpanel commented Apr 11, 2025

Compiler version

3.7.0-RC1
3.7.0-RC2

Minimized code

Compile with -Wunused:imports:

object X {
  var global = 0
}

object Main {
  import X.global
  def main(args: Array[String]): Unit = {
    global = 1
  }
}

Output

unused import
  import X.global

Expectation

The warning should not be there. The code requires the import to compile.

@OndrejSpanel OndrejSpanel added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 11, 2025
@OndrejSpanel OndrejSpanel changed the title False position: unused import when var symbol is used only as assignment target False positive warning: unused import when var symbol is used only as assignment target Apr 11, 2025
@som-snytt som-snytt added area:linting Linting warnings enabled with -W or -Xlint and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 11, 2025
@som-snytt som-snytt self-assigned this Apr 11, 2025
tgodzik pushed a commit that referenced this issue May 7, 2025
Fixes #22970

Simple example showing how awkward this is compared to a traversal,
where there is more control when descending.

Previously, it "ignored" the LHS of Assign to avoid taking `x = y` as
ref to `x` instead of assignment to.

Now, "mark" the LHS as assignment, so `transformIdent` and `Select` set
a "mode" flag, such that `x` is correctly taken as either a ref or
assign to `x`.

Probably there is more complex syntax to handle as LHS, but I haven't
had coffee yet.
tgodzik pushed a commit to scala/scala3-lts that referenced this issue May 12, 2025
Fixes scala#22970

Simple example showing how awkward this is compared to a traversal,
where there is more control when descending.

Previously, it "ignored" the LHS of Assign to avoid taking `x = y` as
ref to `x` instead of assignment to.

Now, "mark" the LHS as assignment, so `transformIdent` and `Select` set
a "mode" flag, such that `x` is correctly taken as either a ref or
assign to `x`.

Probably there is more complex syntax to handle as LHS, but I haven't
had coffee yet.
[Cherry-picked 9bee756]
odersky pushed a commit to dotty-staging/dotty that referenced this issue May 12, 2025
Fixes scala#22970

Simple example showing how awkward this is compared to a traversal,
where there is more control when descending.

Previously, it "ignored" the LHS of Assign to avoid taking `x = y` as
ref to `x` instead of assignment to.

Now, "mark" the LHS as assignment, so `transformIdent` and `Select` set
a "mode" flag, such that `x` is correctly taken as either a ref or
assign to `x`.

Probably there is more complex syntax to handle as LHS, but I haven't
had coffee yet.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:linting Linting warnings enabled with -W or -Xlint itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants