Skip to content

Scala 2.13.15: warn unused false positive when pattern matching on HOF parameter #13061

@PiotrBosak

Description

@PiotrBosak

Reproduction steps

https://scastie.scala-lang.org/LI6ZuhHfS3CjvYepfhJjyw
Scala version: 2.13.15 with "-Ywarn-unused" flag
(Is the bug only in Scala 3.x? If so, report it at scala/scala3 instead.)

    List(("first", "second"))
      .map { case (_, _) =>
        for {
          a <- Some("a")
          b = a // b is not used warning
        } yield b
      }

Problem

When using a pattern match anonymous function in a HOF, variables assigned inside
a for-comprehension(b = a) are reported as unused.
Same snippet in 2.13.14 works fine.

Explain how the above behavior isn't what you expected.
Do not report it as unused since it clearly is used

Workaround

  List(("first", "second"))
    .map { a =>
      a match {
        case (_, _) =>
          for {
            a <- Some("a")
            b = a
          } yield b
      }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions