@@ -13,6 +13,7 @@ import dotty.tools.dotc.core.Phases.Phase
13
13
import dotty .tools .dotc .core .StdNames
14
14
import dotty .tools .dotc .report
15
15
import dotty .tools .dotc .reporting .Message
16
+ import dotty .tools .dotc .reporting .UnusedSymbol as UnusedSymbolMessage
16
17
import dotty .tools .dotc .typer .ImportInfo
17
18
import dotty .tools .dotc .util .{Property , SrcPos }
18
19
import dotty .tools .dotc .core .Mode
@@ -279,27 +280,27 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
279
280
private def traverseAnnotations (sym : Symbol )(using Context ): Unit =
280
281
sym.denot.annotations.foreach(annot => traverser.traverse(annot.tree))
281
282
282
-
283
283
/** Do the actual reporting given the result of the anaylsis */
284
284
private def reportUnused (res : UnusedData .UnusedResult )(using Context ): Unit =
285
- res.warnings.toList.sortBy(_.pos.line)(using Ordering [Int ]).foreach { s =>
286
- s match
287
- case UnusedSymbol (t, _, WarnTypes .Imports ) =>
288
- report.warning(s " unused import " , t)
289
- case UnusedSymbol (t, _, WarnTypes .LocalDefs ) =>
290
- report.warning(s " unused local definition " , t)
291
- case UnusedSymbol (t, _, WarnTypes .ExplicitParams ) =>
292
- report.warning(s " unused explicit parameter " , t)
293
- case UnusedSymbol (t, _, WarnTypes .ImplicitParams ) =>
294
- report.warning(s " unused implicit parameter " , t)
295
- case UnusedSymbol (t, _, WarnTypes .PrivateMembers ) =>
296
- report.warning(s " unused private member " , t)
297
- case UnusedSymbol (t, _, WarnTypes .PatVars ) =>
298
- report.warning(s " unused pattern variable " , t)
299
- case UnusedSymbol (t, _, WarnTypes .UnsetLocals ) =>
300
- report.warning(s " unset local variable, consider using an immutable val instead " , t)
301
- case UnusedSymbol (t, _, WarnTypes .UnsetPrivates ) =>
302
- report.warning(s " unset private variable, consider using an immutable val instead " , t)
285
+ res.warnings.toList.sortBy(_.namePos.sourcePos.line)(using Ordering [Int ])
286
+ .foreach { s =>
287
+ s match
288
+ case UnusedSymbol (_, namePos, WarnTypes .Imports ) =>
289
+ report.warning(UnusedSymbolMessage .imports(namePos.sourcePos), namePos)
290
+ case UnusedSymbol (treePos, namePos, WarnTypes .LocalDefs ) =>
291
+ report.warning(UnusedSymbolMessage .localDefs(treePos.sourcePos), namePos)
292
+ case UnusedSymbol (treePos, namePos, WarnTypes .ExplicitParams ) =>
293
+ report.warning(UnusedSymbolMessage .explicitParams(treePos.sourcePos), namePos)
294
+ case UnusedSymbol (treePos, namePos, WarnTypes .ImplicitParams ) =>
295
+ report.warning(UnusedSymbolMessage .implicitParams(treePos.sourcePos), namePos)
296
+ case UnusedSymbol (treePos, namePos, WarnTypes .PrivateMembers ) =>
297
+ report.warning(UnusedSymbolMessage .privateMembers(treePos.sourcePos), namePos)
298
+ case UnusedSymbol (treePos, namePos, WarnTypes .PatVars ) =>
299
+ report.warning(UnusedSymbolMessage .patVars(treePos.sourcePos), namePos)
300
+ case UnusedSymbol (_, namePos, WarnTypes .UnsetLocals ) =>
301
+ report.warning(" unset local variable, consider using an immutable val instead" , namePos)
302
+ case UnusedSymbol (_, namePos, WarnTypes .UnsetPrivates ) =>
303
+ report.warning(" unset private variable, consider using an immutable val instead" , namePos)
303
304
}
304
305
305
306
end CheckUnused
@@ -312,7 +313,7 @@ object CheckUnused:
312
313
case Aggregate
313
314
case Report
314
315
315
- private enum WarnTypes :
316
+ enum WarnTypes :
316
317
case Imports
317
318
case LocalDefs
318
319
case ExplicitParams
@@ -513,7 +514,7 @@ object CheckUnused:
513
514
popScope()
514
515
val sortedImp =
515
516
if ctx.settings.WunusedHas .imports || ctx.settings.WunusedHas .strictNoImplicitWarn then
516
- unusedImport.map(d => UnusedSymbol (d.srcPos, d.name , WarnTypes .Imports )).toList
517
+ unusedImport.map(d => UnusedSymbol (d.srcPos, d.srcPos , WarnTypes .Imports )).toList
517
518
else
518
519
Nil
519
520
// Partition to extract unset local variables from usedLocalDefs
@@ -526,24 +527,24 @@ object CheckUnused:
526
527
unusedLocalDefs
527
528
.filterNot(d => usedInPosition.exists { case (pos, name) => d.span.contains(pos.span) && name == d.symbol.name})
528
529
.filterNot(d => containsSyntheticSuffix(d.symbol))
529
- .map(d => UnusedSymbol (d.namePos , d.name , WarnTypes .LocalDefs )).toList
530
- val unsetLocalDefs = usedLocalDefs.filter(isUnsetVarDef).map(d => UnusedSymbol (d.namePos , d.name , WarnTypes .UnsetLocals )).toList
530
+ .map(d => UnusedSymbol (d.sourcePos , d.namePos , WarnTypes .LocalDefs )).toList
531
+ val unsetLocalDefs = usedLocalDefs.filter(isUnsetVarDef).map(d => UnusedSymbol (d.sourcePos , d.namePos , WarnTypes .UnsetLocals )).toList
531
532
532
533
val sortedExplicitParams =
533
534
if ctx.settings.WunusedHas .explicits then
534
535
explicitParamInScope
535
536
.filterNot(d => d.symbol.usedDefContains)
536
537
.filterNot(d => usedInPosition.exists { case (pos, name) => d.span.contains(pos.span) && name == d.symbol.name})
537
538
.filterNot(d => containsSyntheticSuffix(d.symbol))
538
- .map(d => UnusedSymbol (d.namePos , d.name , WarnTypes .ExplicitParams )).toList
539
+ .map(d => UnusedSymbol (d.sourcePos , d.namePos , WarnTypes .ExplicitParams )).toList
539
540
else
540
541
Nil
541
542
val sortedImplicitParams =
542
543
if ctx.settings.WunusedHas .implicits then
543
544
implicitParamInScope
544
545
.filterNot(d => d.symbol.usedDefContains)
545
546
.filterNot(d => containsSyntheticSuffix(d.symbol))
546
- .map(d => UnusedSymbol (d.namePos , d.name , WarnTypes .ImplicitParams )).toList
547
+ .map(d => UnusedSymbol (d.sourcePos , d.namePos , WarnTypes .ImplicitParams )).toList
547
548
else
548
549
Nil
549
550
// Partition to extract unset private variables from usedPrivates
@@ -552,15 +553,15 @@ object CheckUnused:
552
553
privateDefInScope.partition(d => d.symbol.usedDefContains)
553
554
else
554
555
(Nil , Nil )
555
- val sortedPrivateDefs = unusedPrivates.filterNot(d => containsSyntheticSuffix(d.symbol)).map(d => UnusedSymbol (d.namePos , d.name , WarnTypes .PrivateMembers )).toList
556
- val unsetPrivateDefs = usedPrivates.filter(isUnsetVarDef).map(d => UnusedSymbol (d.namePos , d.name , WarnTypes .UnsetPrivates )).toList
556
+ val sortedPrivateDefs = unusedPrivates.filterNot(d => containsSyntheticSuffix(d.symbol)).map(d => UnusedSymbol (d.sourcePos , d.namePos , WarnTypes .PrivateMembers )).toList
557
+ val unsetPrivateDefs = usedPrivates.filter(isUnsetVarDef).map(d => UnusedSymbol (d.sourcePos , d.namePos , WarnTypes .UnsetPrivates )).toList
557
558
val sortedPatVars =
558
559
if ctx.settings.WunusedHas .patvars then
559
560
patVarsInScope
560
561
.filterNot(d => d.symbol.usedDefContains)
561
562
.filterNot(d => containsSyntheticSuffix(d.symbol))
562
563
.filterNot(d => usedInPosition.exists { case (pos, name) => d.span.contains(pos.span) && name == d.symbol.name})
563
- .map(d => UnusedSymbol (d.namePos , d.name , WarnTypes .PatVars )).toList
564
+ .map(d => UnusedSymbol (d.sourcePos , d.namePos , WarnTypes .PatVars )).toList
564
565
else
565
566
Nil
566
567
val warnings =
@@ -574,7 +575,7 @@ object CheckUnused:
574
575
unsetLocalDefs :::
575
576
unsetPrivateDefs
576
577
unsorted.sortBy { s =>
577
- val pos = s.pos .sourcePos
578
+ val pos = s.namePos .sourcePos
578
579
(pos.line, pos.column)
579
580
}
580
581
UnusedResult (warnings.toSet)
@@ -807,7 +808,8 @@ object CheckUnused:
807
808
case _:tpd.Block => Local
808
809
case _ => Other
809
810
810
- case class UnusedSymbol (pos : SrcPos , name : Name , warnType : WarnTypes )
811
+ case class UnusedSymbol (defnTree : SrcPos , namePos : SrcPos , warnType : WarnTypes )
812
+
811
813
/** A container for the results of the used elements analysis */
812
814
case class UnusedResult (warnings : Set [UnusedSymbol ])
813
815
object UnusedResult :
0 commit comments