Unreachable case warning could point to previous matching case
See original GitHub issueGiven:
trait Foo
class One extends Foo
class Two extends Foo
class Bla extends One
object Test {
def test(f: Foo) = f match {
case f: One =>
case f: Two =>
case f: Bla =>
}
}
We get the warning:
-- [E120] Only null matched Warning: try/caseun.scala:10:9 ---------------------
10 | case f: Bla =>
| ^^^^^^
| Only null is matched. Consider using `case null =>` instead.
This is confusing since it doesn’t explain why only null
is matched. Here it’s easy to see that this is because we matched on One
previously and that Bla
is a subtype of One
, but in a long series of case this might be very hard to understand. A better warning message would explain all of this.
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
Unreachable code in Scala Pattern Matching - Stack Overflow
The pattern matching is sequential for match which means it will first match price with 1st case which is price: Int then Float...
Read more >Scala 'unreachable code due to variable pattern' warning ...
I hope this explanation about the "unreachable code due to variable pattern" warning/error message has been helpful. scala · case · compiler.
Read more >Missing unreachable match warning with only two cases #12559
there should be an unreachable warning for case e: LogEventWithMarker , as which happens when we add another child to LogEvent (uncomment ...
Read more >warning CS1062: Unreachable code detected. is this normal ...
Cases in a switch statement do not need to end in break , they just need to have an unreachable end (that the...
Read more >46476 – Missing Warning about unreachable code after return
Running the same static analyser over the source code of a recent gcc found 22 occurrences of this kind of problem. Here they...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Created #4953 for
-Ylog
, and closing withstat:revisit
.I see. That feels like a change with larger implications, though. I’d agree with Fengyun that we either put on hold or close this, since we now have the correct error message that makes more sense. We could file a separate bug for “-Ylog”.