Skip to content

Inaccurate warning message for the last redundant wildcard case in pattern match #21724

Closed
@noti0na1

Description

@noti0na1

Compiler version

3.6.0-RC1-bin-SNAPSHOT-nonbootstrapped-git-6ceaab5

Minimized code

trait A
trait B

def test(x: A | B) = x match
  case _: A => println("A")
  case null => println("null")
  case _: B => println("B")
  case _ => println("default") // unreachable

Output

The last case is reported as unreachable except for null, and suggested to be rewritten to case null =>.

-- [E121] Pattern Match Warning: Stest.scala:1240:7 ----------------------------
1240 |  case _ => println("default")
     |       ^
     |Unreachable case except for null (if this is intentional, consider writing case null => instead).
1 warning found

If we replace the wildcard to literal null in the last case, it will be reported as unreachable directly.

-- [E030] Match case Unreachable Warning: Stest.scala:1240:7 -------------------
1240 |  case null => println("default")
     |       ^^^^
     |       Unreachable case
1 warning found

Expectation

The last redundant wildcard case should be reported as unreachable.

Issue

In checkReachability:

if pat != EmptyTree // rethrow case of catch uses EmptyTree
    && !pat.symbol.isAllOf(SyntheticCase, butNot=Method) // ExpandSAMs default cases use SyntheticCase
    && isSubspace(covered, prev)
then {
  val nullOnly = isNullable && i == len - 1 && isWildcardArg(pat)
  val msg = if nullOnly then MatchCaseOnlyNullWarning() else MatchCaseUnreachable()
  report.warning(msg, pat.srcPos)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions