Skip to content

Closes #4300 adds better worded pattern match unreachability warning #4552

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public enum ErrorMessageID {
DoubleDeclarationID,
MatchCaseOnlyNullWarningID,
ImportRenamedTwiceID,
TypeTestAlwaysSucceedsID,
;

public int errorNumber() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2117,4 +2117,9 @@ object messages {
val explanation: String = ""
}

case class TypeTestAlwaysSucceeds(foundCls: Symbol, testCls: Symbol)(implicit ctx: Context) extends Message(TypeTestAlwaysSucceedsID) {
val kind = "Syntax"
val msg = s"The highlighted type test will always succeed since the scrutinee type ($foundCls) is a subtype of ${testCls}"
val explanation = ""
}
}
5 changes: 2 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ValueClasses._
import SymUtils._
import core.Flags._
import util.Positions._
import reporting.diagnostic.messages.TypeTestAlwaysSucceeds
import reporting.trace


Expand Down Expand Up @@ -90,9 +91,7 @@ object TypeTestsCasts {

if (expr.tpe <:< testType)
if (expr.tpe.isNotNull) {
ctx.warning(
em"this will always yield true, since `$foundCls` is a subclass of `$testCls`",
expr.pos)
ctx.warning(TypeTestAlwaysSucceeds(foundCls, testCls), tree.pos)
constant(expr, Literal(Constant(true)))
}
else expr.testNotNull
Expand Down