Skip to content

Commit d41c6dd

Browse files
committed
Remove report unused implicit imports
- Named import pointing to an implicit - Given imports `import foo.bar.given` - Add new tests, remove the ones on implicit reporting
1 parent f354b96 commit d41c6dd

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,12 @@ object CheckUnused:
192192

193193
/** Register an import */
194194
def registerImport(imp: tpd.Import)(using Context): Unit =
195-
if !tpd.languageImport(imp.expr).nonEmpty then
195+
val tpd.Import(qual, selectors) = imp
196+
if !tpd.languageImport(qual).nonEmpty then
196197
impInScope.top += imp
197-
unusedImport ++= imp.selectors.filter(s => !isImportExclusion(s))
198+
unusedImport ++= selectors.filter{ s =>
199+
!isImportExclusion(s) && !s.isGiven && !isSelectorOnAGiven(qual, s)
200+
}
198201

199202
/** Register (or not) some `val` or `def` according to the context, scope and flags */
200203
def registerDef(valOrDef: tpd.ValOrDefDef)(using Context): Unit =
@@ -293,6 +296,9 @@ object CheckUnused:
293296
end getUnused
294297
//============================ HELPERS ====================================
295298

299+
private def isSelectorOnAGiven(qual: tpd.Tree, sel: ImportSelector)(using Context): Boolean =
300+
qual.tpe.member(sel.name).alternatives.exists(_.symbol.is(Given))
301+
296302
private def isImportExclusion(sel: ImportSelector): Boolean = sel.renamed match
297303
case untpd.Ident(name) => name == StdNames.nme.WILDCARD
298304
case _ => false

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

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@ object FooNested:
3131
object Nested:
3232
def hello = Set()
3333

34-
object FooGivenUnused:
35-
import SomeGivenImports.given // error
36-
37-
object FooGiven:
38-
import SomeGivenImports.given // OK
39-
import SomeGivenImports._ // error
40-
41-
val foo = summon[Int]
4234

4335
/**
4436
* Import used as type name are considered
@@ -92,12 +84,6 @@ object IgnoreExclusion:
9284
def check =
9385
val a = Set(1)
9486
val b = Map(1 -> 2)
95-
/**
96-
* Some given values for the test
97-
*/
98-
object SomeGivenImports:
99-
given Int = 0
100-
given String = "foo"
10187

10288
/* BEGIN : Check on packages*/
10389
package p {
@@ -115,9 +101,10 @@ package p {
115101
/* END : Check on packages*/
116102

117103
/* BEGIN : tests on meta-language features */
118-
object TestGivenCoversionScala2:
104+
object TestLanguageImportAreIgnored:
119105
/* note: scala3 Conversion[U,T] do not require an import */
120106
import language.implicitConversions // OK
107+
import language._ // OK
121108

122109
implicit def doubleToInt(d:Double):Int = d.toInt
123110

@@ -153,4 +140,43 @@ object GivenImportOrderBtoA:
153140
import A._ // error
154141
def t = implicitly[X]
155142
}
156-
/* END : tests on given import order */
143+
/* END : tests on given import order */
144+
145+
/*
146+
* Advanced tests on given imports meta-programming
147+
*
148+
* - Currently also tests that no imported implicits are reported
149+
*/
150+
151+
package summoninlineconflict:
152+
package lib:
153+
trait A
154+
trait B
155+
trait C
156+
trait X
157+
158+
given willBeUnused: (A & X) = new A with X {}
159+
given willBeUsed: (A & B) = new A with B {}
160+
given notUsedAtAll: Int = 0
161+
162+
package use:
163+
import lib.{A, B, C, willBeUnused, willBeUsed, notUsedAtAll} // OK
164+
import compiletime.summonInline // OK
165+
166+
transparent inline given conflictInside: C =
167+
summonInline[A]
168+
new {}
169+
170+
transparent inline given potentialConflict: C =
171+
summonInline[B]
172+
new {}
173+
174+
val b: B = summon[B]
175+
val c: C = summon[C]
176+
177+
package unusedgivensimports:
178+
package foo:
179+
given Int = 0
180+
181+
package bar:
182+
import foo.given // OK

0 commit comments

Comments
 (0)