Skip to content

Commit e013a1b

Browse files
committed
Use result of TypeTest
1 parent ff32921 commit e013a1b

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,14 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
6565
// import x.y; x may be rewritten x.y, also import x.z as y
6666
override def transformSelect(tree: Select)(using Context): tree.type =
6767
val name = tree.removeAttachment(OriginalName).getOrElse(nme.NO_NAME)
68-
if tree.qualifier.span.isSynthetic || name.exists(_ != tree.symbol.name) then
68+
if tree.span.isSynthetic && tree.symbol == defn.TypeTest_unapply then
69+
tree.qualifier.tpe.underlying.finalResultType match
70+
case AppliedType(_, args) => // if tycon.typeSymbol == defn.TypeTestClass
71+
val res = args(1)
72+
val target = res.dealias.typeSymbol
73+
resolveUsage(target, target.name, res.importPrefix.skipPackageObject) // case _: T =>
74+
case _ =>
75+
else if tree.qualifier.span.isSynthetic || name.exists(_ != tree.symbol.name) then
6976
if !ignoreTree(tree) then
7077
resolveUsage(tree.symbol, name, tree.qualifier.tpe)
7178
else

tests/warn/i21525.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//> using options -Wunused:imports
2+
3+
import scala.reflect.TypeTest
4+
5+
trait A {
6+
type B
7+
type C <: B
8+
9+
given instance: TypeTest[B, C]
10+
}
11+
12+
def f(a: A, b: a.B): Boolean = {
13+
import a.C
14+
b match {
15+
case _: C =>
16+
true
17+
case _ =>
18+
false
19+
}
20+
}

0 commit comments

Comments
 (0)