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