question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No warning is triggered for non-exhaustive pattern matching on case class field

See original GitHub issue

Compiler version

3.0.0-RC3 from print scalaVersion on sbt console.

Minimized code

sealed trait Status
object Status {
  case object Active   extends Status
  case object Inactive extends Status
}

final case class Foo(status: Status)
def bar(user: Foo): Unit = user match {
  case Foo(Status.Active) =>
    println("active")
    // no compile-time warning for missing Status.Inactive case
}

scalacOptions has

List(
  "-unchecked",
  "-deprecation",
  "-feature",
  "-Xfatal-warnings"
)

NOTE:

  • Replacing the Status above with the enum like below doesn’t make any difference.

    enum Status {
      case Active
      case Inactive
    }
    
  • final case class (i.e. final case class Foo(status: Status)) makes no difference.

Output

It compiles without any warning or error even with -Xfatal-warnings, and it results in the following runtime error.

[error] (run-main-1) scala.MatchError: Foo(Inactive) (of class kevinlee.Example1$Foo)
[error] scala.MatchError: Foo(Inactive) (of class kevinlee.Example1$Foo)
...

Expectation

The compile-time warning like

match may not be exhaustive.
It would fail on the following input: Foo(Inactive)
    def bar(user: Foo): Unit = user match {

(or the compile-time error with "-Xfatal-warnings" compiler option)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
som-snyttcommented, May 5, 2021

final is also not required to see the warning in Scala 2. All that is required is that it walk like a duck.

1reaction
Kevin-Leecommented, May 6, 2021

@liufengyun Here is the same code in Scala 2.13.5. As you can see, the non-exhaustive warning is triggered. ~~https://scastie.scala-lang.org/AXKSl4K5S26cPjTE8b1kdw~~ https://scastie.scala-lang.org/KoU8DeZvTuKMlTfHFmHYeg (updated with non-final case class)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scala pattern match is not exhaustive on nested case classes
A workaround appears to be to chose different names for the field of RequestError and ProcessingError . Here's a slightly minimized version: sealed...
Read more >
Scala pattern matching keep saying "match is not ...
Finally, underscores are a Bad Thing(tm) in Scala variable or class names. All UPPERCASE names are not idiomatic either. So: sealed trait Message...
Read more >
4. Pattern Matching - Programming Scala, 2nd Edition [Book]
So, it warns that the match isn't exhaustive. Then we see what happens when we try to match on a value for which...
Read more >
Pattern match not-exhaustive, no warning - Question
At runtime I get an error, because the local function, loop, is called with (Nil,4,None), and there is no case for (Nil,None). However,...
Read more >
Lower Your Guards
A compiler should warn if a function defined by pattern matching does not ... its pattern-match coverage warnings, there are still several cases...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found