Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve override detection in CheckUnused, fixes #16865 #16965

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -574,12 +574,14 @@ object CheckUnused:
private def shouldNotReportParamOwner(using Context): Boolean =
if sym.exists then
val owner = sym.owner
trivialDefs(owner) ||
owner.is(Flags.Override) ||
trivialDefs(owner) || // is a trivial def
owner.isPrimaryConstructor ||
owner.annotations.exists (
owner.annotations.exists ( // @depreacated
_.symbol == ctx.definitions.DeprecatedAnnot
)
) ||
owner.isAllOf(Synthetic | PrivateLocal) ||
owner.is(Accessor) ||
owner.isOverriden
else
false

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

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

end extension

extension (defdef: tpd.DefDef)
Expand Down Expand Up @@ -620,8 +627,8 @@ object CheckUnused:
val sym = memDef.symbol
(sym.is(Param) || sym.isAllOf(PrivateParamAccessor | Local, butNot = CaseAccessor)) &&
!isSyntheticMainParam(sym) &&
!sym.shouldNotReportParamOwner &&
(!sym.exists || !(sym.owner.isAllOf(Synthetic | PrivateLocal) || sym.owner.is(Accessor)))
!sym.shouldNotReportParamOwner


private def shouldReportPrivateDef(using Context): Boolean =
currScopeType.top == ScopeType.Template && !memDef.symbol.isConstructor && memDef.symbol.is(Private, butNot = SelfName | Synthetic | CaseAccessor)
Expand Down
14 changes: 13 additions & 1 deletion tests/neg-custom-args/fatal-warnings/i15503e.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,16 @@ package foo.test.trivial:
object Y

package foo.test.i16955:
class S(var r: String) // OK
class S(var r: String) // OK

package foo.test.i16865:
trait Foo:
def fn(a: Int, b: Int): Int // OK
trait Bar extends Foo

object Ex extends Bar:
def fn(a: Int, b: Int): Int = b + 3 // OK

object Ex2 extends Bar:
override def fn(a: Int, b: Int): Int = b + 3 // OK