@@ -25,7 +25,7 @@ import dotty.tools.dotc.core.Definitions
25
25
import dotty .tools .dotc .core .NameKinds .WildcardParamName
26
26
import dotty .tools .dotc .core .Symbols .Symbol
27
27
import dotty .tools .dotc .core .StdNames .nme
28
-
28
+ import scala . math . Ordering
29
29
30
30
/**
31
31
* A compiler phase that checks for unused imports or definitions
@@ -64,7 +64,7 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
64
64
65
65
override def transformUnit (tree : tpd.Tree )(using Context ): tpd.Tree =
66
66
unusedDataApply { ud =>
67
- aggregateUnused (ud, ud.getUnused )
67
+ finishAggregation (ud)
68
68
if (phaseMode == PhaseMode .Report ) then
69
69
ud.unusedAggregate.foreach(reportUnused)
70
70
}
@@ -253,20 +253,21 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
253
253
private def traverseAnnotations (sym : Symbol )(using Context ): Unit =
254
254
sym.denot.annotations.foreach(annot => traverser.traverse(annot.tree))
255
255
256
- private def aggregateUnused (data : UnusedData , res : UnusedData .UnusedResult )(using Context ): Unit =
256
+ private def finishAggregation (data : UnusedData )(using Context ): Unit =
257
+ val unusedInThisStage = data.getUnused
257
258
data.unusedAggregate match {
258
259
case None =>
259
- data.unusedAggregate = Some (res )
260
+ data.unusedAggregate = Some (unusedInThisStage )
260
261
case Some (prevUnused) =>
261
- val intersection = res .warnings.filter(sym => prevUnused.warnings.contains(sym) )
262
+ val intersection = unusedInThisStage .warnings.intersect( prevUnused.warnings)
262
263
data.unusedAggregate = Some (UnusedResult (intersection))
263
264
}
264
265
265
266
266
267
267
268
/** Do the actual reporting given the result of the anaylsis */
268
269
private def reportUnused (res : UnusedData .UnusedResult )(using Context ): Unit =
269
- res.warnings.foreach { s =>
270
+ res.warnings.toList.sortBy(_.pos.line)( using Ordering [ Int ]). foreach { s =>
270
271
s match
271
272
case UnusedSymbol (t, _, WarnTypes .Imports ) =>
272
273
report.warning(s " unused import " , t)
@@ -321,7 +322,7 @@ object CheckUnused:
321
322
import UnusedData .*
322
323
323
324
/** The current scope during the tree traversal */
324
- var currScopeType : MutStack [ScopeType ] = MutStack (ScopeType .Other )
325
+ val currScopeType : MutStack [ScopeType ] = MutStack (ScopeType .Other )
325
326
326
327
var unusedAggregate : Option [UnusedResult ] = None
327
328
@@ -732,7 +733,7 @@ object CheckUnused:
732
733
733
734
case class UnusedSymbol (pos : SrcPos , name : Name , warnType : WarnTypes )
734
735
/** A container for the results of the used elements analysis */
735
- case class UnusedResult (warnings : List [UnusedSymbol ])
736
+ case class UnusedResult (warnings : Set [UnusedSymbol ])
736
737
object UnusedResult :
737
738
val Empty = UnusedResult (Nil )
738
739
0 commit comments