@@ -25,9 +25,7 @@ import reporting._
2525import io .{AbstractFile , NoAbstractFile , PlainFile , Path }
2626import scala .io .Codec
2727import collection .mutable
28- import parsing .Parsers
2928import printing ._
30- import config .Printers .{implicits , implicitsDetailed }
3129import config .{JavaPlatform , SJSPlatform , Platform , ScalaSettings , ScalaRelease }
3230import classfile .ReusableDataReader
3331import StdNames .nme
@@ -1027,7 +1025,6 @@ object Contexts:
10271025 /** Collect information about the run for purposes of additional diagnostics.
10281026 */
10291027 class Usages :
1030- import rewrites .Rewrites .patch
10311028 private val selectors = mutable.Map .empty[ImportInfo , Set [untpd.ImportSelector ]].withDefaultValue(Set .empty)
10321029 private val importInfos = mutable.Map .empty[CompilationUnit , List [(ImportInfo , Symbol )]].withDefaultValue(Nil )
10331030
@@ -1043,60 +1040,28 @@ object Contexts:
10431040 selectors(info) += selector
10441041
10451042 // unused import, owner, which selector
1046- def unused (using Context ): List [(ImportInfo , Symbol , untpd.ImportSelector )] =
1047- var unusages = List .empty[(ImportInfo , Symbol , untpd.ImportSelector )]
1043+ def unused (using Context ): List [(ImportInfo , Symbol , List [untpd.ImportSelector ])] =
10481044 if ctx.settings.WunusedHas .imports && ! ctx.compilationUnit.isJava then
1049- // if ctx.settings.Ydebug.value then
1050- // println(importInfos.get(ctx.compilationUnit).map(iss => iss.map((ii, s) => s"${ii.show} ($ii)")).getOrElse(Nil).mkString("Registered ImportInfos\n", "\n", ""))
1051- // println(selectors.toList.flatMap((k,v) => v.toList.map(sel => s"${k.show} -> $sel")).mkString("Used selectors\n", "\n", ""))
1045+ var unusages = List .empty[(ImportInfo , Symbol , List [untpd.ImportSelector ])]
10521046 def checkUsed (info : ImportInfo , owner : Symbol ): Unit =
1053- val used = selectors(info)
1054- var needsPatch = false
1047+ val usedSelectors = selectors(info)
1048+ var unusedSelectors = List .empty[untpd. ImportSelector ]
10551049 def cull (toCheck : List [untpd.ImportSelector ]): Unit =
10561050 toCheck match
10571051 case selector :: rest =>
10581052 cull(rest) // reverse
1059- if ! selector.isMask && ! used(selector) then
1060- unusages ::= ((info, owner, selector))
1061- needsPatch = true
1053+ if ! selector.isMask && ! usedSelectors(selector) then
1054+ unusedSelectors ::= selector
10621055 case _ =>
10631056 cull(info.selectors)
1064- if needsPatch && ctx.settings.YrewriteImports .value then
1065- val src = ctx.compilationUnit.source
1066- val infoPos = info.qualifier.sourcePos
1067- val lineSource = SourceFile .virtual(name = " import-line.scala" , content = infoPos.lineContent)
1068- val PackageDef (_, pieces) = Parsers .Parser (lineSource).parse(): @ unchecked
1069- // patch if there's just one import on the line, i.e., not import a.b, c.d
1070- if pieces.length == 1 then
1071- val retained = info.selectors.filter(sel => sel.isMask || used(sel))
1072- val selectorSpan = info.selectors.map(_.span).reduce(_ union _)
1073- val lineSpan = src.lineSpan(infoPos.start)
1074- if retained.isEmpty then
1075- patch(src, lineSpan, " " ) // line deletion
1076- else if retained.size == 1 && info.selectors.size > 1 then
1077- var starting = info.selectors.head.span.start
1078- while starting > lineSpan.start && src.content()(starting) != '{' do starting -= 1
1079- var ending = info.selectors.last.span.end
1080- while ending <= lineSpan.end && src.content()(ending) != '}' do ending += 1
1081- if ending < lineSpan.end then ending += 1 // past the close brace
1082- val widened = selectorSpan.withStart(starting).withEnd(ending)
1083- patch(src, widened, toText(retained)) // try to remove braces
1084- else
1085- patch(src, selectorSpan, toText(retained))
1057+ if unusedSelectors.nonEmpty then unusages ::= (info, owner, unusedSelectors)
10861058 end checkUsed
10871059 importInfos.remove(ctx.compilationUnit).foreach(_.foreach(checkUsed))
1088- unusages
1060+ unusages
1061+ else
1062+ Nil
10891063 end unused
10901064
1091- // just the selectors, no need to add braces
1092- private def toText (retained : List [untpd.ImportSelector ])(using Context ): String =
1093- def selected (sel : untpd.ImportSelector ) =
1094- if sel.isGiven then " given"
1095- else if sel.isWildcard then " *"
1096- else if sel.name == sel.rename then sel.name.show
1097- else s " ${sel.name.show} as ${sel.rename.show}"
1098- retained.map(selected).mkString(" , " )
1099-
11001065 def clear ()(using Context ): Unit =
11011066 importInfos.clear()
11021067 selectors.clear()
0 commit comments