@@ -7,7 +7,7 @@ import dotty.tools.dotc.ast.untpd.ImportSelector
7
7
import dotty .tools .dotc .config .ScalaSettings
8
8
import dotty .tools .dotc .core .Contexts .*
9
9
import dotty .tools .dotc .core .Decorators .{em , i }
10
- import dotty .tools .dotc .core .Flags .{ Given , Implicit , GivenOrImplicit , Param , Private , SelfName , Synthetic }
10
+ import dotty .tools .dotc .core .Flags ._
11
11
import dotty .tools .dotc .core .Phases .Phase
12
12
import dotty .tools .dotc .core .StdNames
13
13
import dotty .tools .dotc .report
@@ -25,6 +25,7 @@ import dotty.tools.dotc.transform.MegaPhase.MiniPhase
25
25
import dotty .tools .dotc .core .Annotations
26
26
import dotty .tools .dotc .core .Definitions
27
27
import dotty .tools .dotc .core .Types .ConstantType
28
+ import dotty .tools .dotc .core .NameKinds .WildcardParamName
28
29
29
30
30
31
@@ -108,8 +109,9 @@ class CheckUnused extends MiniPhase:
108
109
109
110
override def prepareForTypeDef (tree : tpd.TypeDef )(using Context ): Context =
110
111
_key.unusedDataApply{ ud =>
111
- ud.registerDef(tree)
112
- ud.addIgnoredUsage(tree.symbol)
112
+ if ! tree.symbol.is(Param ) then // Ignore type parameter (as Scala 2)
113
+ ud.registerDef(tree)
114
+ ud.addIgnoredUsage(tree.symbol)
113
115
}
114
116
115
117
override def prepareForBind (tree : tpd.Bind )(using Context ): Context =
@@ -313,7 +315,7 @@ object CheckUnused:
313
315
/** Register all annotations of this symbol's denotation */
314
316
def registerUsedAnnotation (sym : Symbol )(using Context ): Unit =
315
317
val annotSym = sym.denot.annotations.map(_.symbol)
316
- registerUsed(annotSym )
318
+ annotSym.foreach(s => registerUsed(s, None ) )
317
319
318
320
/**
319
321
* Register a found (used) symbol along with its name
@@ -327,13 +329,6 @@ object CheckUnused:
327
329
if sym.isConstructor && sym.exists then
328
330
registerUsed(sym.owner, None ) // constructor are "implicitly" imported with the class
329
331
330
- /** Register a list of found (used) symbols */
331
- def registerUsed (syms : Seq [Symbol ])(using Context ): Unit =
332
- usedInScope.top ++=
333
- syms
334
- .filterNot(s => isConstructorOfSynth(s) || doNotRegister(s))
335
- .map(s => (s, s.isAccessibleAsIdent, None ))
336
-
337
332
/** Register a symbol that should be ignored */
338
333
def addIgnoredUsage (sym : Symbol )(using Context ): Unit =
339
334
doNotRegister += sym
@@ -359,8 +354,8 @@ object CheckUnused:
359
354
def registerDef (memDef : tpd.MemberDef )(using Context ): Unit =
360
355
// register the annotations for usage
361
356
registerUsedAnnotation(memDef.symbol)
362
- if ! memDef.symbol.isUnusedAnnot && ! memDef.symbol.isAllOf( Flags . AccessorCreationFlags ) then
363
- if memDef.symbol.is( Param ) && ! isSyntheticMainParam(memDef.symbol) && ! memDef.symbol.ownerIsTrivial then
357
+ if memDef.isValidMemberDef then
358
+ if memDef.isValidParam then
364
359
if memDef.symbol.isOneOf(GivenOrImplicit ) then
365
360
implicitParamInScope += memDef
366
361
else
@@ -526,7 +521,7 @@ object CheckUnused:
526
521
527
522
extension (sym : Symbol )
528
523
/** is accessible without import in current context */
529
- def isAccessibleAsIdent (using Context ): Boolean =
524
+ private def isAccessibleAsIdent (using Context ): Boolean =
530
525
sym.exists &&
531
526
ctx.outersIterator.exists{ c =>
532
527
c.owner == sym.owner
@@ -536,7 +531,7 @@ object CheckUnused:
536
531
}
537
532
538
533
/** Given an import and accessibility, return an option of selector that match import<->symbol */
539
- def isInImport (imp : tpd.Import , isAccessible : Boolean , symName : Option [Name ])(using Context ): Option [ImportSelector ] =
534
+ private def isInImport (imp : tpd.Import , isAccessible : Boolean , symName : Option [Name ])(using Context ): Option [ImportSelector ] =
540
535
val tpd .Import (qual, sels) = imp
541
536
val qualHasSymbol = qual.tpe.member(sym.name).alternatives.map(_.symbol).contains(sym)
542
537
def selector = sels.find(sel => (sel.name.toTermName == sym.name || sel.name.toTypeName == sym.name) && symName.map(n => n.toTermName == sel.rename).getOrElse(true ))
@@ -547,24 +542,55 @@ object CheckUnused:
547
542
None
548
543
549
544
/** Annotated with @unused */
550
- def isUnusedAnnot (using Context ): Boolean =
545
+ private def isUnusedAnnot (using Context ): Boolean =
551
546
sym.annotations.exists(a => a.symbol == ctx.definitions.UnusedAnnot )
552
547
553
- def ownerIsTrivial (using Context ): Boolean =
548
+ private def shouldNotReportParamOwner (using Context ): Boolean =
549
+ if sym.exists then
550
+ val owner = sym.owner
551
+ trivialDefs(owner) ||
552
+ owner.is(Flags .Override ) ||
553
+ owner.isPrimaryConstructor ||
554
+ owner.annotations.exists (
555
+ _.symbol == ctx.definitions.DeprecatedAnnot
556
+ )
557
+ else
558
+ false
559
+
560
+ private def ownerIsTrivial (using Context ): Boolean =
554
561
sym.exists && trivialDefs(sym.owner)
555
562
556
563
extension (defdef : tpd.DefDef )
557
564
// so trivial that it never consumes params
558
565
private def isTrivial (using Context ): Boolean =
559
566
val rhs = defdef.rhs
560
- rhs.symbol == ctx.definitions.Predef_undefined || rhs.tpe =:= ctx.definitions.NothingType || (rhs match {
567
+ rhs.symbol == ctx.definitions.Predef_undefined ||
568
+ rhs.tpe =:= ctx.definitions.NothingType ||
569
+ defdef.symbol.is(Deferred ) ||
570
+ (rhs match {
561
571
case _ : tpd.Literal => true
562
572
case _ => rhs.tpe.isInstanceOf [ConstantType ]
563
573
})
564
574
def registerTrivial (using Context ): Unit =
565
575
if defdef.isTrivial then
566
576
trivialDefs += defdef.symbol
567
577
578
+ extension (memDef : tpd.MemberDef )
579
+ private def isValidMemberDef (using Context ): Boolean =
580
+ ! memDef.symbol.isUnusedAnnot && ! memDef.symbol.isAllOf(Flags .AccessorCreationFlags ) && ! memDef.name.isWildcard
581
+
582
+ private def isValidParam (using Context ): Boolean =
583
+ val sym = memDef.symbol
584
+ (sym.is(Param ) || sym.isAllOf(PrivateParamAccessor )) &&
585
+ ! isSyntheticMainParam(sym) &&
586
+ ! sym.shouldNotReportParamOwner
587
+
588
+
589
+ extension (thisName : Name )
590
+ private def isWildcard : Boolean =
591
+ thisName == StdNames .nme.WILDCARD || thisName.is(WildcardParamName )
592
+
593
+
568
594
end UnusedData
569
595
570
596
private object UnusedData :
0 commit comments