Skip to content

specifying a type in pattern matching results in a spurious warning #4674

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
thesamet opened this issue Jun 17, 2018 · 2 comments
Closed

specifying a type in pattern matching results in a spurious warning #4674

thesamet opened this issue Jun 17, 2018 · 2 comments

Comments

@thesamet
Copy link

thesamet commented Jun 17, 2018

val x: String = "something"
x.foreach {
  case 's' => println("s")
  case c: Char => println(c)
} 

Results in:

[warn] 154 |        case c: Char                   => println(c)
[warn]     |             ^
[warn]     |this will always yield true, since `class Char` is a subclass of `class Char`

While it's true that I can just omit the : Char, the intent here was to increase readability by specifying the type. Feel free to close if this is not a bug but an intended change.

@allanrenucci
Copy link
Contributor

It is an intended change. Since we cannot know the intent of the author, I think we should always emit a warning for fruitless type tests

nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jun 19, 2018
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jun 19, 2018
nicolasstucki added a commit to dotty-staging/dotty that referenced this issue Jun 19, 2018
nicolasstucki added a commit that referenced this issue Jun 19, 2018
Fix #4674: Do not emit waring if types are the same
janrockdev added a commit to janrockdev/dotty that referenced this issue Jun 23, 2018
allanrenucci added a commit that referenced this issue Jul 16, 2018
@odersky
Copy link
Contributor

odersky commented May 7, 2019

This leads to spurious warnings for compiler-generated pattern matches. I believe it's better to revert the decision and check only explicit isInstanceOf, never patterns.

Note the scalac does not check patterns either.

odersky added a commit to dotty-staging/dotty that referenced this issue May 7, 2019
I believe scala#4674 had the wrong resolution. The test leads to spurious warnings for compiler-generated pattern matches.
I believe it's better to revert the decision and check only explicit isInstanceOf, never patterns.

Note the scalac does not check patterns either.
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

4 participants