Skip to content

Commit b289cd1

Browse files
authored
Improve override detection in CheckUnused, fixes #16865 (#16965)
@szymon-rd, fixes #16865 - CheckUnused detects override from base type in addition of `override` flag - Update test suit
2 parents 7c9c72a + b509104 commit b289cd1

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

compiler/src/dotty/tools/dotc/transform/CheckUnused.scala

+13-6
Original file line numberDiff line numberDiff line change
@@ -574,12 +574,14 @@ object CheckUnused:
574574
private def shouldNotReportParamOwner(using Context): Boolean =
575575
if sym.exists then
576576
val owner = sym.owner
577-
trivialDefs(owner) ||
578-
owner.is(Flags.Override) ||
577+
trivialDefs(owner) || // is a trivial def
579578
owner.isPrimaryConstructor ||
580-
owner.annotations.exists (
579+
owner.annotations.exists ( // @depreacated
581580
_.symbol == ctx.definitions.DeprecatedAnnot
582-
)
581+
) ||
582+
owner.isAllOf(Synthetic | PrivateLocal) ||
583+
owner.is(Accessor) ||
584+
owner.isOverriden
583585
else
584586
false
585587

@@ -589,6 +591,11 @@ object CheckUnused:
589591
private def everySymbol(using Context): List[Symbol] =
590592
List(sym, sym.companionClass, sym.companionModule, sym.moduleClass).filter(_.exists)
591593

594+
/** A function is overriden. Either has `override flags` or parent has a matching member (type and name) */
595+
private def isOverriden(using Context): Boolean =
596+
sym.is(Flags.Override) ||
597+
(if sym.exists then sym.owner.thisType.parents.exists(p => sym.matchingMember(p).exists) else false)
598+
592599
end extension
593600

594601
extension (defdef: tpd.DefDef)
@@ -620,8 +627,8 @@ object CheckUnused:
620627
val sym = memDef.symbol
621628
(sym.is(Param) || sym.isAllOf(PrivateParamAccessor | Local, butNot = CaseAccessor)) &&
622629
!isSyntheticMainParam(sym) &&
623-
!sym.shouldNotReportParamOwner &&
624-
(!sym.exists || !(sym.owner.isAllOf(Synthetic | PrivateLocal) || sym.owner.is(Accessor)))
630+
!sym.shouldNotReportParamOwner
631+
625632

626633
private def shouldReportPrivateDef(using Context): Boolean =
627634
currScopeType.top == ScopeType.Template && !memDef.symbol.isConstructor && memDef.symbol.is(Private, butNot = SelfName | Synthetic | CaseAccessor)

tests/neg-custom-args/fatal-warnings/i15503e.scala

+13-1
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,16 @@ package foo.test.trivial:
5454
object Y
5555

5656
package foo.test.i16955:
57-
class S(var r: String) // OK
57+
class S(var r: String) // OK
58+
59+
package foo.test.i16865:
60+
trait Foo:
61+
def fn(a: Int, b: Int): Int // OK
62+
trait Bar extends Foo
63+
64+
object Ex extends Bar:
65+
def fn(a: Int, b: Int): Int = b + 3 // OK
66+
67+
object Ex2 extends Bar:
68+
override def fn(a: Int, b: Int): Int = b + 3 // OK
69+

0 commit comments

Comments
 (0)