@@ -364,7 +364,7 @@ object CheckUnused:
364
364
*
365
365
* See the `isAccessibleAsIdent` extension method below in the file
366
366
*/
367
- private val usedInScope = MutStack (MutSet [(Symbol ,Boolean , Option [Name ], Boolean )]())
367
+ private val usedInScope = MutStack (MutSet [(Symbol , Option [Name ], Boolean )]())
368
368
private val usedInPosition = MutMap .empty[Name , MutSet [Symbol ]]
369
369
/* unused import collected during traversal */
370
370
private val unusedImport = MutList .empty[ImportSelectorData ]
@@ -412,12 +412,16 @@ object CheckUnused:
412
412
if sym.isConstructor then
413
413
registerUsed(sym.owner, None , notForImport) // constructor are "implicitly" imported with the class
414
414
else
415
- val accessibleAsIdent = sym.isAccessibleAsIdent
415
+ // If the symbol is accessible in this scope without an import, do not register it for unused import analysis
416
+ val notForImport1 =
417
+ notForImport
418
+ || (! name.exists(_.toTermName != sym.name.toTermName) && sym.isAccessibleAsIdent)
419
+
416
420
def addIfExists (sym : Symbol ): Unit =
417
421
if sym.exists then
418
422
usedDef += sym
419
- if ! notForImport then
420
- usedInScope.top += ((sym, accessibleAsIdent, name, isDerived))
423
+ if ! notForImport1 then
424
+ usedInScope.top += ((sym, name, isDerived))
421
425
addIfExists(sym)
422
426
addIfExists(sym.companionModule)
423
427
addIfExists(sym.companionClass)
@@ -501,9 +505,9 @@ object CheckUnused:
501
505
val selDatas = impInScope.pop()
502
506
503
507
for usedInfo <- usedInfos do
504
- val (sym, isAccessible, optName, isDerived) = usedInfo
508
+ val (sym, optName, isDerived) = usedInfo
505
509
val usedData = selDatas.find { selData =>
506
- sym.isInImport(selData, isAccessible, optName, isDerived)
510
+ sym.isInImport(selData, optName, isDerived)
507
511
}
508
512
usedData match
509
513
case Some (data) =>
@@ -697,15 +701,12 @@ object CheckUnused:
697
701
}
698
702
699
703
/** Given an import and accessibility, return selector that matches import<->symbol */
700
- private def isInImport (selData : ImportSelectorData , isAccessible : Boolean , symName : Option [Name ], isDerived : Boolean )(using Context ): Boolean =
704
+ private def isInImport (selData : ImportSelectorData , symName : Option [Name ], isDerived : Boolean )(using Context ): Boolean =
701
705
assert(sym.exists)
702
706
703
707
val selector = selData.selector
704
708
705
- if isAccessible && ! symName.exists(_.toTermName != sym.name.toTermName) then
706
- // Even if this import matches, it is pointless because the symbol would be accessible anyway
707
- false
708
- else if ! selector.isWildcard then
709
+ if ! selector.isWildcard then
709
710
if ! symName.forall(explicitName => selector.rename == explicitName.toTermName) then
710
711
// if there is an explicit name, it must match
711
712
false
0 commit comments