Skip to content

-Wunused:all reports setter instead of unset variable in anon class #23200

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

Open
mberndt123 opened this issue May 20, 2025 · 3 comments · May be fixed by #23211
Open

-Wunused:all reports setter instead of unset variable in anon class #23200

mberndt123 opened this issue May 20, 2025 · 3 comments · May be fixed by #23211
Assignees
Labels
area:linting Linting warnings enabled with -W or -Xlint itype:bug

Comments

@mberndt123
Copy link

mberndt123 commented May 20, 2025

Compiler version

3.7.0

Minimized code

trait Foo
trait Bar

new Foo {
  var x: Int = 42
  val _ = new Bar:
    println(x)
}

The compiler issues an "unused private member" warning for var x in line 5, even though it is used in line 7.
Scastie

@mberndt123 mberndt123 added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels May 20, 2025
@som-snytt
Copy link
Contributor

The strange carets say that the unused member is x_=.

10 |    var x: Int = 42
   |        ^^^
   |        unused private member

Not sure why it gets a no-op setter, but "using" it, of course, silences the warning.

Moreover, the warning "should" say that the var is not set. Adding x_=(27), as simple assignment is simple assignment. It's charming that it is printed as an infix operator!

          final class $anon() extends Object(), Foo {
            var x: Int = 42
            def x_=(x$1: Int): Unit = ()
            {
              final class $anon() extends Object(), Bar {
                println($anon.this.x)
              }
              new $anon():Bar
            }:Bar @unchecked match
              {
                case _ => ()
              }
            this.x = 27
            this x_= 27
          }
          new $anon():Foo

For comparison

def f: Unit =
  var x: Int = 42
  println(x)

warns

17 |  var x: Int = 42
   |      ^
   |      unset local variable, consider using an immutable val instead

As a footnote, scastie is on 3.7.0-RC1, which is so to speak quite aged.

@som-snytt som-snytt changed the title -Wunused:all reports false positive when var is used in nested trait -Wunused:all reports spurious setter instead of unset variable in anon class May 20, 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 May 20, 2025
@som-snytt som-snytt self-assigned this May 20, 2025
@mberndt123
Copy link
Author

mberndt123 commented May 20, 2025

It's not only about the setter. If you reassign the variable inside Bar, you still get a warning.
https://scastie.scala-lang.org/0WRV7uO7Tmuy8Z4DKHYUVw

In fact, this is a minimized version of some code in the project I'm working on, where we do mutate that variable, and we use 3.7.0, not 3.7.0-RC1

@som-snytt som-snytt changed the title -Wunused:all reports spurious setter instead of unset variable in anon class -Wunused:all reports setter instead of unset variable in anon class May 20, 2025
@som-snytt
Copy link
Contributor

@mberndt123 the setter is also not used from inside Bar. To answer my other question, the setter is expanded later.

My fix tweaks the conditions for warning so it reports "unset var" (when that is true).

@som-snytt som-snytt linked a pull request May 20, 2025 that will close this issue
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