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