Skip to content

Commit 5b1247b

Browse files
szymon-rdKordyjan
authored andcommitted
Fix WUnused with indents in derived code
1 parent 3a7e248 commit 5b1247b

File tree

2 files changed

+65
-13
lines changed

2 files changed

+65
-13
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,15 @@ import dotty.tools.dotc.reporting.Message
1515
import dotty.tools.dotc.typer.ImportInfo
1616
import dotty.tools.dotc.util.Property
1717
import dotty.tools.dotc.core.Mode
18-
import dotty.tools.dotc.core.Types.TypeTraverser
19-
import dotty.tools.dotc.core.Types.Type
20-
import dotty.tools.dotc.core.Types.AnnotatedType
18+
import dotty.tools.dotc.core.Types.{AnnotatedType, ConstantType, NameFilter, NoType, TermRef, Type, TypeTraverser}
2119
import dotty.tools.dotc.core.Flags.flagsString
2220
import dotty.tools.dotc.core.Flags
2321
import dotty.tools.dotc.core.Names.Name
2422
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
2523
import dotty.tools.dotc.core.Annotations
2624
import dotty.tools.dotc.core.Definitions
27-
import dotty.tools.dotc.core.Types.ConstantType
2825
import dotty.tools.dotc.core.NameKinds.WildcardParamName
29-
import dotty.tools.dotc.core.Types.TermRef
30-
import dotty.tools.dotc.core.Types.NameFilter
26+
import dotty.tools.dotc.core.Symbols.Symbol
3127

3228

3329

@@ -81,6 +77,12 @@ class CheckUnused extends MiniPhase:
8177

8278
override def prepareForIdent(tree: tpd.Ident)(using Context): Context =
8379
if tree.symbol.exists then
80+
val prefixes = LazyList.iterate(tree.typeOpt.normalizedPrefix)(_.normalizedPrefix).takeWhile(_ != NoType)
81+
for {
82+
prefix <- prefixes
83+
} {
84+
unusedDataApply(_.registerUsed(prefix.classSymbol, None))
85+
}
8486
unusedDataApply(_.registerUsed(tree.symbol, Some(tree.name)))
8587
else if tree.hasType then
8688
unusedDataApply(_.registerUsed(tree.tpe.classSymbol, Some(tree.name)))
@@ -407,7 +409,6 @@ object CheckUnused:
407409
val kept = used.filterNot { t =>
408410
val (sym, isAccessible, optName) = t
409411
// keep the symbol for outer scope, if it matches **no** import
410-
411412
// This is the first matching wildcard selector
412413
var selWildCard: Option[ImportSelector] = None
413414

tests/neg-custom-args/fatal-warnings/i15503i.scala

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ package foo.test.companionprivate:
7878
package foo.test.i16678:
7979
def foo(func: Int => String, value: Int): String = func(value) // OK
8080

81-
def run =
81+
def run =
8282
println(foo(number => number.toString, value = 5)) // OK
8383
println(foo(number => "<number>", value = 5)) // error
8484
println(foo(func = number => "<number>", value = 5)) // error
8585
println(foo(func = number => number.toString, value = 5)) // OK
8686
println(foo(func = _.toString, value = 5)) // OK
87-
87+
8888
package foo.test.possibleclasses:
8989
case class AllCaseClass(
9090
k: Int, // OK
@@ -93,7 +93,7 @@ package foo.test.possibleclasses:
9393
s: Int, // error /* But not these */
9494
val t: Int, // OK
9595
private val z: Int // error
96-
)
96+
)
9797

9898
case class AllCaseUsed(
9999
k: Int, // OK
@@ -113,7 +113,7 @@ package foo.test.possibleclasses:
113113
s: Int, // error
114114
val t: Int, // OK
115115
private val z: Int // error
116-
)
116+
)
117117

118118
class AllUsed(
119119
k: Int, // OK
@@ -124,10 +124,61 @@ package foo.test.possibleclasses:
124124
private val z: Int // OK
125125
) {
126126
def a = k + y + s + t + z
127-
}
127+
}
128128

129129
package foo.test.from.i16675:
130130
case class PositiveNumber private (i: Int) // OK
131131
object PositiveNumber:
132-
def make(i: Int): Option[PositiveNumber] = //OK
132+
def make(i: Int): Option[PositiveNumber] = //OK
133133
Option.when(i >= 0)(PositiveNumber(i)) // OK
134+
135+
package foo.test.i16822:
136+
enum ExampleEnum {
137+
case Build(context: String) // OK
138+
case List // OK
139+
}
140+
141+
def demo = {
142+
val x = ExampleEnum.List // OK
143+
println(x) // OK
144+
}
145+
146+
package foo.test.i16877:
147+
import scala.collection.immutable.HashMap // OK
148+
import scala.annotation.StaticAnnotation // OK
149+
150+
class ExampleAnnotation(val a: Object) extends StaticAnnotation // OK
151+
152+
@ExampleAnnotation(new HashMap()) // OK
153+
class Test //OK
154+
155+
package foo.test.i16926:
156+
def hello(): Unit =
157+
for {
158+
i <- (0 to 10).toList
159+
(a, b) = "hello" -> "world" // OK
160+
} yield println(s"$a $b")
161+
162+
package foo.test.i16925:
163+
def hello =
164+
for {
165+
i <- 1 to 2 if true
166+
_ = println(i) // OK
167+
} yield ()
168+
169+
package foo.test.i16679:
170+
object myPackage:
171+
trait CaseClassName[A]:
172+
def name: String
173+
object CaseClassName:
174+
trait CaseClassByStringName[A] extends CaseClassName[A]
175+
import scala.deriving.Mirror
176+
object CaseClassByStringName:
177+
inline final def derived[A](using inline A: Mirror.Of[A]): CaseClassByStringName[A] =
178+
new CaseClassByStringName[A]:
179+
def name: String = A.toString
180+
181+
object secondPackage:
182+
import myPackage.CaseClassName // OK
183+
case class CoolClass(i: Int) derives CaseClassName.CaseClassByStringName
184+
println(summon[CaseClassName[CoolClass]].name)

0 commit comments

Comments
 (0)