@@ -25,7 +25,6 @@ import reporting._
2525import io .{AbstractFile , NoAbstractFile , PlainFile , Path }
2626import scala .io .Codec
2727import collection .mutable
28- import parsing .Parsers
2928import printing ._
3029import config .{JavaPlatform , SJSPlatform , Platform , ScalaSettings , ScalaRelease }
3130import classfile .ReusableDataReader
@@ -1024,7 +1023,6 @@ object Contexts:
10241023 /** Collect information about the run for purposes of additional diagnostics.
10251024 */
10261025 class Usages :
1027- import rewrites .Rewrites .patch
10281026 private val selectors = mutable.Map .empty[ImportInfo , Set [untpd.ImportSelector ]].withDefaultValue(Set .empty)
10291027 private val importInfos = mutable.Map .empty[CompilationUnit , List [(ImportInfo , Symbol )]].withDefaultValue(Nil )
10301028
@@ -1040,60 +1038,28 @@ object Contexts:
10401038 selectors(info) += selector
10411039
10421040 // unused import, owner, which selector
1043- def unused (using Context ): List [(ImportInfo , Symbol , untpd.ImportSelector )] =
1044- var unusages = List .empty[(ImportInfo , Symbol , untpd.ImportSelector )]
1041+ def unused (using Context ): List [(ImportInfo , Symbol , List [untpd.ImportSelector ])] =
10451042 if ctx.settings.WunusedHas .imports && ! ctx.compilationUnit.isJava then
1046- // if ctx.settings.Ydebug.value then
1047- // println(importInfos.get(ctx.compilationUnit).map(iss => iss.map((ii, s) => s"${ii.show} ($ii)")).getOrElse(Nil).mkString("Registered ImportInfos\n", "\n", ""))
1048- // println(selectors.toList.flatMap((k,v) => v.toList.map(sel => s"${k.show} -> $sel")).mkString("Used selectors\n", "\n", ""))
1043+ var unusages = List .empty[(ImportInfo , Symbol , List [untpd.ImportSelector ])]
10491044 def checkUsed (info : ImportInfo , owner : Symbol ): Unit =
1050- val used = selectors(info)
1051- var needsPatch = false
1045+ val usedSelectors = selectors(info)
1046+ var unusedSelectors = List .empty[untpd. ImportSelector ]
10521047 def cull (toCheck : List [untpd.ImportSelector ]): Unit =
10531048 toCheck match
10541049 case selector :: rest =>
10551050 cull(rest) // reverse
1056- if ! selector.isMask && ! used(selector) then
1057- unusages ::= ((info, owner, selector))
1058- needsPatch = true
1051+ if ! selector.isMask && ! usedSelectors(selector) then
1052+ unusedSelectors ::= selector
10591053 case _ =>
10601054 cull(info.selectors)
1061- if needsPatch && ctx.settings.YrewriteImports .value then
1062- val src = ctx.compilationUnit.source
1063- val infoPos = info.qualifier.sourcePos
1064- val lineSource = SourceFile .virtual(name = " import-line.scala" , content = infoPos.lineContent)
1065- val PackageDef (_, pieces) = Parsers .Parser (lineSource).parse(): @ unchecked
1066- // patch if there's just one import on the line, i.e., not import a.b, c.d
1067- if pieces.length == 1 then
1068- val retained = info.selectors.filter(sel => sel.isMask || used(sel))
1069- val selectorSpan = info.selectors.map(_.span).reduce(_ union _)
1070- val lineSpan = src.lineSpan(infoPos.start)
1071- if retained.isEmpty then
1072- patch(src, lineSpan, " " ) // line deletion
1073- else if retained.size == 1 && info.selectors.size > 1 then
1074- var starting = info.selectors.head.span.start
1075- while starting > lineSpan.start && src.content()(starting) != '{' do starting -= 1
1076- var ending = info.selectors.last.span.end
1077- while ending <= lineSpan.end && src.content()(ending) != '}' do ending += 1
1078- if ending < lineSpan.end then ending += 1 // past the close brace
1079- val widened = selectorSpan.withStart(starting).withEnd(ending)
1080- patch(src, widened, toText(retained)) // try to remove braces
1081- else
1082- patch(src, selectorSpan, toText(retained))
1055+ if unusedSelectors.nonEmpty then unusages ::= (info, owner, unusedSelectors)
10831056 end checkUsed
10841057 importInfos.remove(ctx.compilationUnit).foreach(_.foreach(checkUsed))
1085- unusages
1058+ unusages
1059+ else
1060+ Nil
10861061 end unused
10871062
1088- // just the selectors, no need to add braces
1089- private def toText (retained : List [untpd.ImportSelector ])(using Context ): String =
1090- def selected (sel : untpd.ImportSelector ) =
1091- if sel.isGiven then " given"
1092- else if sel.isWildcard then " *"
1093- else if sel.name == sel.rename then sel.name.show
1094- else s " ${sel.name.show} as ${sel.rename.show}"
1095- retained.map(selected).mkString(" , " )
1096-
10971063 def clear ()(using Context ): Unit =
10981064 importInfos.clear()
10991065 selectors.clear()
0 commit comments