Skip to content
Merged
Changes from 1 commit
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
11 changes: 8 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,14 @@ 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)
if (inMatch)
ctx.warning(
em"this case will always be taken because the scrutinee has type `$testType`",
tree.pos)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error message doesn't make sense for the following test case:

object Foo {
  def unapply(x: Int): Option[Int] = if (x % 2 == 0) Some(x) else None
}

class Test {
  def foo(a: Int) = a match { case Foo(x: Int) => x }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • To address both @zoltanelek and @liufengyun's concerns, I'd at least say "this pattern will always succeed", where "this" refers to x: Int (as shown by the position) — I'd prefer in fact "the highlighted pattern match".

I think both this message and the current one can still be a bit confusing because they might only refer to one pattern out of many nested ones, but maybe that can be clarified in the message explanation.

  • I'd also please keep showing both $foundCls and $testCls, since in the presence of type aliases they might be nontrivial to deduce from the scrutinee type and the pattern match.

  • Separately, "since $foundCls is a subclass of $testCls" says more than "because the scrutinee has type $testType": who wrote the code and fixes the warning might know the scrutinee's type, but what they probably missed is that $foundCls is a subclass of $testCls.

else
ctx.warning(
em"this will always yield true, since `$foundCls` is a subclass of `$testCls`",
expr.pos)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would merge the two together by making inMatch a parameter of the message class and name the class something like TypeTestAlwaysSucceeds

constant(expr, Literal(Constant(true)))
}
else expr.testNotNull
Expand Down