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

-Wunused:locals gives false positives for structural types in local scopes #17631

Closed
matil019 opened this issue May 31, 2023 · 2 comments · Fixed by #17929
Closed

-Wunused:locals gives false positives for structural types in local scopes #17631

matil019 opened this issue May 31, 2023 · 2 comments · Fixed by #17929
Assignees
Labels
area:linting Linting warnings enabled with -W or -Xlint itype:bug
Milestone

Comments

@matil019
Copy link

matil019 commented May 31, 2023

Compiler version

3.3.0

Minimized code

//> using scala "3.3.0"
//> using options "-Wunused:locals"

class Record(elems: (String, Any)*) extends Selectable:
  private val fields = elems.toMap
  def selectDynamic(name: String): Any = fields(name)

type Person = Record { val name: String; val age: Int }

object Main {
  locally {
    def good: Person = ???
    def bad1: Record { val name: String; val age: Int } = ???
    def bad2 = (good: Record { val name: String; val age: Int })
    (bad1, bad2)
  }
}

Compile it with:

scalac -Wunused:locals Main.scala

Output

-- Warning: Main.scala:10:27 ---------------------------------------------------
10 |    def bad1: Record { val name: String; val age: Int } = ???
   |                           ^^^^
   |                           unused local definition
-- Warning: Main.scala:10:45 ---------------------------------------------------
10 |    def bad1: Record { val name: String; val age: Int } = ???
   |                                             ^^^
   |                                             unused local definition
-- Warning: Main.scala:11:35 ---------------------------------------------------
11 |    def bad2 = (good: Record { val name: String; val age: Int })
   |                                   ^^^^
   |                                   unused local definition
-- Warning: Main.scala:11:53 ---------------------------------------------------
11 |    def bad2 = (good: Record { val name: String; val age: Int })
   |                                                     ^^^
   |                                                 unused local definition
4 warnings found

Expectation

No warnings are issued.

Note that removing the locally block and putting the definitions directly into Main suppresses the warnings, presumably because they are no longer local.

@matil019 matil019 added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels May 31, 2023
@bishabosha
Copy link
Member

for comparison the following compiles in Scala 2.13.10 with no warnings, as expected:

//> using option -Wunused:locals
//> using scala 2.13

package foo {
  class Record(elems: (String, Any)*) {
    private val fields = elems.toMap
    def selectDynamic(name: String): Any = fields(name)
  }

  object Main {
    locally {
      def good: Person = ???
      def bad1: Record { val name: String; val age: Int } = ???
      def bad2 = (good: Record { val name: String; val age: Int })
      (bad1, bad2)
    }
  }

}

package object foo {
  type Person = Record { val name: String; val age: Int }
}

@bishabosha bishabosha 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 31, 2023
@bishabosha
Copy link
Member

@KacperFKorban feel free to reassign, I saw you have worked on these warnings previously

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.

4 participants