@@ -25,9 +25,7 @@ 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
- import config .Printers .{implicits , implicitsDetailed }
31
29
import config .{JavaPlatform , SJSPlatform , Platform , ScalaSettings , ScalaRelease }
32
30
import classfile .ReusableDataReader
33
31
import StdNames .nme
@@ -1027,7 +1025,6 @@ object Contexts:
1027
1025
/** Collect information about the run for purposes of additional diagnostics.
1028
1026
*/
1029
1027
class Usages :
1030
- import rewrites .Rewrites .patch
1031
1028
private val selectors = mutable.Map .empty[ImportInfo , Set [untpd.ImportSelector ]].withDefaultValue(Set .empty)
1032
1029
private val importInfos = mutable.Map .empty[CompilationUnit , List [(ImportInfo , Symbol )]].withDefaultValue(Nil )
1033
1030
@@ -1043,60 +1040,28 @@ object Contexts:
1043
1040
selectors(info) += selector
1044
1041
1045
1042
// 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 ])] =
1048
1044
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 ])]
1052
1046
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 ]
1055
1049
def cull (toCheck : List [untpd.ImportSelector ]): Unit =
1056
1050
toCheck match
1057
1051
case selector :: rest =>
1058
1052
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
1062
1055
case _ =>
1063
1056
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)
1086
1058
end checkUsed
1087
1059
importInfos.remove(ctx.compilationUnit).foreach(_.foreach(checkUsed))
1088
- unusages
1060
+ unusages
1061
+ else
1062
+ Nil
1089
1063
end unused
1090
1064
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
-
1100
1065
def clear ()(using Context ): Unit =
1101
1066
importInfos.clear()
1102
1067
selectors.clear()
0 commit comments