Skip to content

Unreachable code warning for reachable match case #3543

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

Closed
nicolasstucki opened this issue Nov 23, 2017 · 2 comments
Closed

Unreachable code warning for reachable match case #3543

nicolasstucki opened this issue Nov 23, 2017 · 2 comments

Comments

@nicolasstucki
Copy link
Contributor

import scala.util.matching.Regex

object Test {
  def main(args: Array[String]): Unit = {
    foo("c" :: Nil, false)
  }

  def foo(remaining: List[String], inCodeBlock: Boolean): Unit = {
    remaining match {
      case CodeBlockEndRegex(before) :: ls =>
      case SymbolTagRegex(name) :: ls if !inCodeBlock => println("OK")
      case _ =>
    }
  }

  val CodeBlockEndRegex = new Regex("(b)")
  val SymbolTagRegex = new Regex("(c)")
}

Emits the folowing warining but should not.

11 |      case SymbolTagRegex(name) :: ls if !inCodeBlock => println("OK")
   |                                                      ^^^^^^^^^^^^^^^^
   |                                                      unreachable code

The code is correctly compiled and executed.

@nicolasstucki
Copy link
Contributor Author

@liufengyun any additional useful info

@allanrenucci
Copy link
Contributor

Here are two more examples that should not emit warnings:

class Test {
  class Foo {
    def unapply(x: String): Option[String] = ???
  }

  def test(xs: List[String]): Unit = {
    val Yes = new Foo
    val No = new Foo

    xs match {
      case Yes(x) :: ls => println("Yes")
      case No(y) :: ls => println("No") // unreachable code
      case _ =>
    }
  }
}
class Test {
  class Foo(x: Boolean) {
    def unapply(y: String): Boolean = x
  }

  def test(xs: List[String]): Unit = {
    val Yes = new Foo(true)
    val No = new Foo(false)

    xs match {
      case No() :: ls => println("No")
      case Yes() :: ls => println("Yes") // unreachable code
      case _ =>
    }
  }
}

liufengyun added a commit to dotty-staging/dotty that referenced this issue Nov 23, 2017
liufengyun added a commit to dotty-staging/dotty that referenced this issue Nov 23, 2017
nicolasstucki added a commit that referenced this issue Nov 24, 2017
Fix #3543: compare extractor with TermRef instead of MethodType
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants