@@ -25,7 +25,6 @@ import reporting._
25
25
import io .{AbstractFile , NoAbstractFile , PlainFile , Path }
26
26
import scala .io .Codec
27
27
import collection .mutable
28
- import parsing .Parsers
29
28
import printing ._
30
29
import config .{JavaPlatform , SJSPlatform , Platform , ScalaSettings , ScalaRelease }
31
30
import classfile .ReusableDataReader
@@ -1024,7 +1023,6 @@ object Contexts:
1024
1023
/** Collect information about the run for purposes of additional diagnostics.
1025
1024
*/
1026
1025
class Usages :
1027
- import rewrites .Rewrites .patch
1028
1026
private val selectors = mutable.Map .empty[ImportInfo , Set [untpd.ImportSelector ]].withDefaultValue(Set .empty)
1029
1027
private val importInfos = mutable.Map .empty[CompilationUnit , List [(ImportInfo , Symbol )]].withDefaultValue(Nil )
1030
1028
@@ -1040,60 +1038,28 @@ object Contexts:
1040
1038
selectors(info) += selector
1041
1039
1042
1040
// 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 ])] =
1045
1042
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 ])]
1049
1044
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 ]
1052
1047
def cull (toCheck : List [untpd.ImportSelector ]): Unit =
1053
1048
toCheck match
1054
1049
case selector :: rest =>
1055
1050
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
1059
1053
case _ =>
1060
1054
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)
1083
1056
end checkUsed
1084
1057
importInfos.remove(ctx.compilationUnit).foreach(_.foreach(checkUsed))
1085
- unusages
1058
+ unusages
1059
+ else
1060
+ Nil
1086
1061
end unused
1087
1062
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
-
1097
1063
def clear ()(using Context ): Unit =
1098
1064
importInfos.clear()
1099
1065
selectors.clear()
0 commit comments