diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java index b9941ee11151..87136eda5ebe 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java @@ -130,6 +130,7 @@ public enum ErrorMessageID { DoubleDeclarationID, MatchCaseOnlyNullWarningID, ImportRenamedTwiceID, + TypeTestAlwaysSucceedsID, ; public int errorNumber() { diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index ef685b0dc0f2..93ac0c1832b2 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -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 = "" + } } diff --git a/compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala b/compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala index c40cc7b213f2..39ea428c5ac9 100644 --- a/compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala +++ b/compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala @@ -10,6 +10,7 @@ import ValueClasses._ import SymUtils._ import core.Flags._ import util.Positions._ +import reporting.diagnostic.messages.TypeTestAlwaysSucceeds import reporting.trace @@ -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