bug icon indicating copy to clipboard operation
bug copied to clipboard

-Wunused:locals does not produce a warning for simple values

Open gatisnolv opened this issue 3 years ago • 5 comments

Reproduction steps

Scala version: 2.13.8

  object Example extends App {
    def unusedLocal = {
      val a = 1
      2
    }
  }

with the compiler option

"-Wunused:locals"

(and thus -Xlint:unused ...)

Problem

I would expect 'val a' to be reported as 'never used', but there is no warning.

If either line 3 is changed to e.g. val a = 1+1 OR line 4 changed to e.g. '2+1', there is a warning.

gatisnolv avatar May 11 '22 12:05 gatisnolv

might be @som-snytt territory?

SethTisue avatar May 11 '22 12:05 SethTisue

The trivial assignment is eliminated early, which amazes me. "There is no assignment. There is nothing to see here." That's an old Jedi mind trick.

som-snytt avatar May 11 '22 12:05 som-snytt

So is there any realistic possibility of this progressing?

It's probably merely idle curiosity, but I'm curious what the transform is that causes it to be eliminated?

SethTisue avatar Jun 30 '22 14:06 SethTisue

https://github.com/scala/scala/blob/3faf9a98282a2f06219fd0b05b7fede2ab8fb326/src/compiler/scala/tools/nsc/typechecker/Typers.scala#L6268

I was surprised to find val a: Int = 1; disappeared from here.

jxnu-liguobin avatar Sep 21 '22 04:09 jxnu-liguobin

Probably just inlined.

som-snytt avatar Sep 21 '22 05:09 som-snytt