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