@@ -102,7 +102,9 @@ class CheckUnused extends MiniPhase:
102
102
103
103
override def prepareForValDef (tree : tpd.ValDef )(using Context ): Context =
104
104
_key.unusedDataApply{ud =>
105
- ud.registerDef(tree)
105
+ // do not register the ValDef generated for `object`
106
+ if ! tree.symbol.is(Module ) then
107
+ ud.registerDef(tree)
106
108
ud.addIgnoredUsage(tree.symbol)
107
109
}
108
110
@@ -341,15 +343,11 @@ object CheckUnused:
341
343
342
344
/** Register a symbol that should be ignored */
343
345
def addIgnoredUsage (sym : Symbol )(using Context ): Unit =
344
- doNotRegister += sym
345
- if sym.is(Flags .Module ) then
346
- doNotRegister += sym.moduleClass
346
+ doNotRegister ++= sym.everySymbol
347
347
348
348
/** Remove a symbol that shouldn't be ignored anymore */
349
349
def removeIgnoredUsage (sym : Symbol )(using Context ): Unit =
350
- doNotRegister -= sym
351
- if sym.is(Flags .Module ) then
352
- doNotRegister -= sym.moduleClass
350
+ doNotRegister --= sym.everySymbol
353
351
354
352
355
353
/** Register an import */
@@ -447,27 +445,37 @@ object CheckUnused:
447
445
Nil
448
446
val sortedLocalDefs =
449
447
if ctx.settings.WunusedHas .locals then
450
- localDefInScope.filter(d => ! usedDef(d.symbol)).map(d => d.namePos -> WarnTypes .LocalDefs ).toList
448
+ localDefInScope
449
+ .filterNot(d => d.symbol.usedDefContains)
450
+ .map(d => d.namePos -> WarnTypes .LocalDefs ).toList
451
451
else
452
452
Nil
453
453
val sortedExplicitParams =
454
454
if ctx.settings.WunusedHas .explicits then
455
- explicitParamInScope.filter(d => ! usedDef(d.symbol)).map(d => d.namePos -> WarnTypes .ExplicitParams ).toList
455
+ explicitParamInScope
456
+ .filterNot(d => d.symbol.usedDefContains)
457
+ .map(d => d.namePos -> WarnTypes .ExplicitParams ).toList
456
458
else
457
459
Nil
458
460
val sortedImplicitParams =
459
461
if ctx.settings.WunusedHas .implicits then
460
- implicitParamInScope.filter(d => ! usedDef(d.symbol)).map(d => d.namePos -> WarnTypes .ImplicitParams ).toList
462
+ implicitParamInScope
463
+ .filterNot(d => d.symbol.usedDefContains)
464
+ .map(d => d.namePos -> WarnTypes .ImplicitParams ).toList
461
465
else
462
466
Nil
463
467
val sortedPrivateDefs =
464
468
if ctx.settings.WunusedHas .privates then
465
- privateDefInScope.filter(d => ! usedDef(d.symbol)).map(d => d.namePos -> WarnTypes .PrivateMembers ).toList
469
+ privateDefInScope
470
+ .filterNot(d => d.symbol.usedDefContains)
471
+ .map(d => d.namePos -> WarnTypes .PrivateMembers ).toList
466
472
else
467
473
Nil
468
474
val sortedPatVars =
469
475
if ctx.settings.WunusedHas .patvars then
470
- patVarsInScope.filter(d => ! usedDef(d.symbol)).map(d => d.namePos -> WarnTypes .PatVars ).toList
476
+ patVarsInScope
477
+ .filterNot(d => d.symbol.usedDefContains)
478
+ .map(d => d.namePos -> WarnTypes .PatVars ).toList
471
479
else
472
480
Nil
473
481
val warnings = List (sortedImp, sortedLocalDefs, sortedExplicitParams, sortedImplicitParams, sortedPrivateDefs, sortedPatVars).flatten.sortBy { s =>
@@ -567,6 +575,14 @@ object CheckUnused:
567
575
else
568
576
false
569
577
578
+ private def usedDefContains (using Context ): Boolean =
579
+ sym.everySymbol.exists(usedDef.apply)
580
+
581
+ private def everySymbol (using Context ): List [Symbol ] =
582
+ List (sym, sym.companionClass, sym.companionModule, sym.moduleClass).filter(_.exists)
583
+
584
+ end extension
585
+
570
586
extension (defdef : tpd.DefDef )
571
587
// so trivial that it never consumes params
572
588
private def isTrivial (using Context ): Boolean =
0 commit comments