@@ -25,7 +25,7 @@ import dotty.tools.dotc.core.Definitions
2525import dotty .tools .dotc .core .NameKinds .WildcardParamName
2626import dotty .tools .dotc .core .Symbols .Symbol
2727import dotty .tools .dotc .core .StdNames .nme
28-
28+ import scala . math . Ordering
2929
3030/**
3131 * A compiler phase that checks for unused imports or definitions
@@ -64,7 +64,7 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
6464
6565 override def transformUnit (tree : tpd.Tree )(using Context ): tpd.Tree =
6666 unusedDataApply { ud =>
67- aggregateUnused (ud, ud.getUnused )
67+ finishAggregation (ud)
6868 if (phaseMode == PhaseMode .Report ) then
6969 ud.unusedAggregate.foreach(reportUnused)
7070 }
@@ -253,20 +253,21 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
253253 private def traverseAnnotations (sym : Symbol )(using Context ): Unit =
254254 sym.denot.annotations.foreach(annot => traverser.traverse(annot.tree))
255255
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
257258 data.unusedAggregate match {
258259 case None =>
259- data.unusedAggregate = Some (res )
260+ data.unusedAggregate = Some (unusedInThisStage )
260261 case Some (prevUnused) =>
261- val intersection = res .warnings.filter(sym => prevUnused.warnings.contains(sym) )
262+ val intersection = unusedInThisStage .warnings.intersect( prevUnused.warnings)
262263 data.unusedAggregate = Some (UnusedResult (intersection))
263264 }
264265
265266
266267
267268 /** Do the actual reporting given the result of the anaylsis */
268269 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 =>
270271 s match
271272 case UnusedSymbol (t, _, WarnTypes .Imports ) =>
272273 report.warning(s " unused import " , t)
@@ -321,7 +322,7 @@ object CheckUnused:
321322 import UnusedData .*
322323
323324 /** The current scope during the tree traversal */
324- var currScopeType : MutStack [ScopeType ] = MutStack (ScopeType .Other )
325+ val currScopeType : MutStack [ScopeType ] = MutStack (ScopeType .Other )
325326
326327 var unusedAggregate : Option [UnusedResult ] = None
327328
@@ -736,7 +737,7 @@ object CheckUnused:
736737
737738 case class UnusedSymbol (pos : SrcPos , name : Name , warnType : WarnTypes )
738739 /** A container for the results of the used elements analysis */
739- case class UnusedResult (warnings : List [UnusedSymbol ])
740+ case class UnusedResult (warnings : Set [UnusedSymbol ])
740741 object UnusedResult :
741742 val Empty = UnusedResult (Nil )
742743
0 commit comments