Skip to content

Commit 38c35bd

Browse files
committed
Apply review suggestions to WUnused PR
1 parent fa07b41 commit 38c35bd

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

compiler/src/dotty/tools/dotc/transform/CheckUnused.scala

+9-8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import dotty.tools.dotc.core.Definitions
2525
import dotty.tools.dotc.core.NameKinds.WildcardParamName
2626
import dotty.tools.dotc.core.Symbols.Symbol
2727
import 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

@@ -732,7 +733,7 @@ object CheckUnused:
732733

733734
case class UnusedSymbol(pos: SrcPos, name: Name, warnType: WarnTypes)
734735
/** A container for the results of the used elements analysis */
735-
case class UnusedResult(warnings: List[UnusedSymbol])
736+
case class UnusedResult(warnings: Set[UnusedSymbol])
736737
object UnusedResult:
737738
val Empty = UnusedResult(Nil)
738739

0 commit comments

Comments
 (0)