@@ -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
@@ -281,24 +282,27 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
281
282
282
283
/** Do the actual reporting given the result of the anaylsis */
283
284
private def reportUnused (res : UnusedData .UnusedResult )(using Context ): Unit =
284
- res.warnings.toList.sortBy(_.pos.line)(using Ordering [Int ]).foreach { s =>
285
+ res.warnings.toList.sortBy{
286
+ case us : UnusedSymbol .ImportSelector => us.pos.line
287
+ case us : UnusedSymbol .Symbol => us.tree.sourcePos.line
288
+ }(using Ordering [Int ]).foreach { s =>
285
289
s match
286
- case UnusedSymbol (t , _, WarnTypes . Imports ) =>
287
- report.warning(s " unused import " , t )
288
- case UnusedSymbol (t, _ , WarnTypes .LocalDefs ) =>
289
- report.warning(s " unused local definition " , t)
290
- case UnusedSymbol (t, _ , WarnTypes .ExplicitParams ) =>
291
- report.warning(s " unused explicit parameter " , t)
292
- case UnusedSymbol (t, _ , WarnTypes .ImplicitParams ) =>
293
- report.warning(s " unused implicit parameter " , t)
294
- case UnusedSymbol (t, _ , WarnTypes .PrivateMembers ) =>
295
- report.warning(s " unused private member " , t)
296
- case UnusedSymbol (t, _ , WarnTypes .PatVars ) =>
297
- report.warning(s " unused pattern variable " , t)
298
- case UnusedSymbol (t, _ , WarnTypes .UnsetLocals ) =>
299
- report.warning(s " unset local variable, consider using an immutable val instead " , t)
300
- case UnusedSymbol (t, _ , WarnTypes .UnsetPrivates ) =>
301
- report.warning(s " unset private variable, consider using an immutable val instead " , t)
290
+ case UnusedSymbol . ImportSelector (srcPos , _) =>
291
+ report.warning(UnusedSymbolMessage .imports(srcPos.sourcePos) )
292
+ case UnusedSymbol . Symbol (t , WarnTypes .LocalDefs ) =>
293
+ report.warning(UnusedSymbolMessage .localDefs(t) , t)
294
+ case UnusedSymbol . Symbol (t , WarnTypes .ExplicitParams ) =>
295
+ report.warning(UnusedSymbolMessage .explicitParams(t) , t)
296
+ case UnusedSymbol . Symbol (t , WarnTypes .ImplicitParams ) =>
297
+ report.warning(UnusedSymbolMessage .implicitParams(t) , t)
298
+ case UnusedSymbol . Symbol (t , WarnTypes .PrivateMembers ) =>
299
+ report.warning(UnusedSymbolMessage .privateMembers(t) , t)
300
+ case UnusedSymbol . Symbol (t , WarnTypes .PatVars ) =>
301
+ report.warning(UnusedSymbolMessage .patVars(t) , t)
302
+ case UnusedSymbol . Symbol (t , WarnTypes .UnsetLocals ) =>
303
+ report.warning(" unset local variable, consider using an immutable val instead" , t)
304
+ case UnusedSymbol . Symbol (t , WarnTypes .UnsetPrivates ) =>
305
+ report.warning(" unset private variable, consider using an immutable val instead" , t)
302
306
}
303
307
304
308
end CheckUnused
@@ -311,8 +315,7 @@ object CheckUnused:
311
315
case Aggregate
312
316
case Report
313
317
314
- private enum WarnTypes :
315
- case Imports
318
+ enum WarnTypes :
316
319
case LocalDefs
317
320
case ExplicitParams
318
321
case ImplicitParams
@@ -510,7 +513,7 @@ object CheckUnused:
510
513
popScope()
511
514
val sortedImp =
512
515
if ctx.settings.WunusedHas .imports || ctx.settings.WunusedHas .strictNoImplicitWarn then
513
- unusedImport.map(d => UnusedSymbol (d.srcPos, d.name, WarnTypes . Imports )).toList
516
+ unusedImport.map(d => UnusedSymbol . ImportSelector (d.srcPos, d.name)).toList
514
517
else
515
518
Nil
516
519
// Partition to extract unset local variables from usedLocalDefs
@@ -523,24 +526,24 @@ object CheckUnused:
523
526
unusedLocalDefs
524
527
.filterNot(d => usedInPosition.exists { case (pos, name) => d.span.contains(pos.span) && name == d.symbol.name})
525
528
.filterNot(d => containsSyntheticSuffix(d.symbol))
526
- .map(d => UnusedSymbol (d.namePos, d.name , WarnTypes .LocalDefs )).toList
527
- val unsetLocalDefs = usedLocalDefs.filter(isUnsetVarDef).map(d => UnusedSymbol (d.namePos, d.name , WarnTypes .UnsetLocals )).toList
529
+ .map(d => UnusedSymbol . Symbol (d , WarnTypes .LocalDefs )).toList
530
+ val unsetLocalDefs = usedLocalDefs.filter(isUnsetVarDef).map(d => UnusedSymbol . Symbol (d , WarnTypes .UnsetLocals )).toList
528
531
529
532
val sortedExplicitParams =
530
533
if ctx.settings.WunusedHas .explicits then
531
534
explicitParamInScope
532
535
.filterNot(d => d.symbol.usedDefContains)
533
536
.filterNot(d => usedInPosition.exists { case (pos, name) => d.span.contains(pos.span) && name == d.symbol.name})
534
537
.filterNot(d => containsSyntheticSuffix(d.symbol))
535
- .map(d => UnusedSymbol (d.namePos, d.name , WarnTypes .ExplicitParams )).toList
538
+ .map(d => UnusedSymbol . Symbol (d , WarnTypes .ExplicitParams )).toList
536
539
else
537
540
Nil
538
541
val sortedImplicitParams =
539
542
if ctx.settings.WunusedHas .implicits then
540
543
implicitParamInScope
541
544
.filterNot(d => d.symbol.usedDefContains)
542
545
.filterNot(d => containsSyntheticSuffix(d.symbol))
543
- .map(d => UnusedSymbol (d.namePos, d.name , WarnTypes .ImplicitParams )).toList
546
+ .map(d => UnusedSymbol . Symbol (d , WarnTypes .ImplicitParams )).toList
544
547
else
545
548
Nil
546
549
// Partition to extract unset private variables from usedPrivates
@@ -549,15 +552,15 @@ object CheckUnused:
549
552
privateDefInScope.partition(d => d.symbol.usedDefContains)
550
553
else
551
554
(Nil , Nil )
552
- val sortedPrivateDefs = unusedPrivates.filterNot(d => containsSyntheticSuffix(d.symbol)).map(d => UnusedSymbol (d.namePos, d.name , WarnTypes .PrivateMembers )).toList
553
- val unsetPrivateDefs = usedPrivates.filter(isUnsetVarDef).map(d => UnusedSymbol (d.namePos, d.name , WarnTypes .UnsetPrivates )).toList
555
+ val sortedPrivateDefs = unusedPrivates.filterNot(d => containsSyntheticSuffix(d.symbol)).map(d => UnusedSymbol . Symbol (d , WarnTypes .PrivateMembers )).toList
556
+ val unsetPrivateDefs = usedPrivates.filter(isUnsetVarDef).map(d => UnusedSymbol . Symbol (d , WarnTypes .UnsetPrivates )).toList
554
557
val sortedPatVars =
555
558
if ctx.settings.WunusedHas .patvars then
556
559
patVarsInScope
557
560
.filterNot(d => d.symbol.usedDefContains)
558
561
.filterNot(d => containsSyntheticSuffix(d.symbol))
559
562
.filterNot(d => usedInPosition.exists { case (pos, name) => d.span.contains(pos.span) && name == d.symbol.name})
560
- .map(d => UnusedSymbol (d.namePos, d.name , WarnTypes .PatVars )).toList
563
+ .map(d => UnusedSymbol . Symbol (d , WarnTypes .PatVars )).toList
561
564
else
562
565
Nil
563
566
val warnings =
@@ -570,9 +573,13 @@ object CheckUnused:
570
573
sortedPatVars :::
571
574
unsetLocalDefs :::
572
575
unsetPrivateDefs
573
- unsorted.sortBy { s =>
574
- val pos = s.pos.sourcePos
575
- (pos.line, pos.column)
576
+ unsorted.sortBy {
577
+ case s : UnusedSymbol .Symbol =>
578
+ val pos = s.tree.sourcePos
579
+ (pos.line, pos.column)
580
+ case s : UnusedSymbol .ImportSelector =>
581
+ val pos = s.pos.sourcePos
582
+ (pos.line, pos.column)
576
583
}
577
584
UnusedResult (warnings.toSet)
578
585
end getUnused
@@ -803,7 +810,9 @@ object CheckUnused:
803
810
case _:tpd.Block => Local
804
811
case _ => Other
805
812
806
- case class UnusedSymbol (pos : SrcPos , name : Name , warnType : WarnTypes )
813
+ enum UnusedSymbol :
814
+ case Symbol (tree : tpd.NamedDefTree , warnType : WarnTypes )
815
+ case ImportSelector (pos : SrcPos , name : Name )
807
816
/** A container for the results of the used elements analysis */
808
817
case class UnusedResult (warnings : Set [UnusedSymbol ])
809
818
object UnusedResult :
0 commit comments