Skip to content

Commit c13422f

Browse files
zoltanelekallanrenucci
authored andcommitted
Closes #4300 adds better worded pattern match unreachability warning (#4552)
1 parent b35309c commit c13422f

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public enum ErrorMessageID {
130130
DoubleDeclarationID,
131131
MatchCaseOnlyNullWarningID,
132132
ImportRenamedTwiceID,
133+
TypeTestAlwaysSucceedsID,
133134
;
134135

135136
public int errorNumber() {

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,4 +2095,9 @@ object messages {
20952095
val explanation: String = ""
20962096
}
20972097

2098+
case class TypeTestAlwaysSucceeds(foundCls: Symbol, testCls: Symbol)(implicit ctx: Context) extends Message(TypeTestAlwaysSucceedsID) {
2099+
val kind = "Syntax"
2100+
val msg = s"The highlighted type test will always succeed since the scrutinee type ($foundCls) is a subtype of ${testCls}"
2101+
val explanation = ""
2102+
}
20982103
}

compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import ValueClasses._
1010
import SymUtils._
1111
import core.Flags._
1212
import util.Positions._
13+
import reporting.diagnostic.messages.TypeTestAlwaysSucceeds
1314
import reporting.trace
1415

1516

@@ -90,9 +91,7 @@ object TypeTestsCasts {
9091

9192
if (expr.tpe <:< testType)
9293
if (expr.tpe.isNotNull) {
93-
ctx.warning(
94-
em"this will always yield true, since `$foundCls` is a subclass of `$testCls`",
95-
expr.pos)
94+
ctx.warning(TypeTestAlwaysSucceeds(foundCls, testCls), tree.pos)
9695
constant(expr, Literal(Constant(true)))
9796
}
9897
else expr.testNotNull

0 commit comments

Comments
 (0)