Skip to content

Commit d37d99e

Browse files
committed
Fix the import precedence in -Wunused:imports
- Wildcard import are reported before named ones - Update the test suit
1 parent 779ec7d commit d37d99e

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,16 +351,29 @@ object CheckUnused:
351351
val used = usedInScope.pop().toSet
352352
// used imports in this scope
353353
val imports = impInScope.pop().toSet
354-
val kept = used.filter { t =>
354+
val kept = used.filterNot { t =>
355355
val (sym, isAccessible, optName) = t
356356
// keep the symbol for outer scope, if it matches **no** import
357-
!imports.exists { imp =>
357+
358+
// This is the first matching wildcard selector
359+
var selWildCard: Option[ImportSelector] = None
360+
361+
val exists = imports.exists { imp =>
358362
sym.isInImport(imp, isAccessible, optName) match
359363
case None => false
364+
case optSel@Some(sel) if sel.isWildcard =>
365+
if selWildCard.isEmpty then selWildCard = optSel
366+
// We keep wildcard symbol for the end as they have the least precedence
367+
false
360368
case Some(sel) =>
361369
unusedImport -= sel
362370
true
363371
}
372+
if !exists && selWildCard.isDefined then
373+
unusedImport -= selWildCard.get
374+
true // a matching import exists so the symbol won't be kept for outer scope
375+
else
376+
exists
364377
}
365378
// if there's an outer scope
366379
if usedInScope.nonEmpty then

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,10 @@ package foo.testing.rename.imports:
244244
import collection.mutable.{Set => MutSet2} // OK
245245
import collection.mutable.{Set => MutSet3} // error
246246
type A[X] = MutSet1[X]
247-
val a = MutSet2(1)
247+
val a = MutSet2(1)
248+
249+
//-------------------------------------
250+
package foo.testing.imports.precedence:
251+
import scala.collection.immutable.{BitSet => _, _} // error
252+
import scala.collection.immutable.BitSet // OK
253+
def t = BitSet.empty

0 commit comments

Comments
 (0)