Skip to content

Commit 0ac6c4e

Browse files
committed
Warn unused member of anonymous class
1 parent 332fceb commit 0ac6c4e

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ object CheckUnused:
517517
def checkPrivate(sym: Symbol, pos: SrcPos) =
518518
if ctx.settings.WunusedHas.privates
519519
&& !sym.isPrimaryConstructor
520-
&& sym.is(Private, butNot = SelfName | Synthetic | CaseAccessor)
520+
&& !sym.isOneOf(SelfName | Synthetic | CaseAccessor)
521521
&& !sym.name.is(BodyRetainerName)
522522
&& !sym.isSerializationSupport
523523
&& !(sym.is(Mutable) && sym.isSetter && sym.owner.is(Trait)) // tracks sym.underlyingSymbol sibling getter
@@ -758,7 +758,7 @@ object CheckUnused:
758758
for (sym, pos) <- infos.defs.iterator if !sym.hasAnnotation(defn.UnusedAnnot) do
759759
if infos.refs(sym) then
760760
checkUnassigned(sym, pos)
761-
else if sym.is(Private, butNot = ParamAccessor) then
761+
else if sym.isEffectivelyPrivate then
762762
checkPrivate(sym, pos)
763763
else if sym.is(Param, butNot = Given | Implicit) then
764764
checkParam(sym, pos)
@@ -879,6 +879,9 @@ object CheckUnused:
879879
sym.isClass && sym.info.allMembers.forall: d =>
880880
val m = d.symbol
881881
!m.isTerm || m.isSelfSym || m.is(Method) && (m.owner == defn.AnyClass || m.owner == defn.ObjectClass)
882+
def isEffectivelyPrivate(using Context): Boolean =
883+
sym.is(Private, butNot = ParamAccessor)
884+
|| sym.owner.isAnonymousClass && !sym.nextOverriddenSymbol.exists
882885

883886
extension (sel: ImportSelector)
884887
def boundTpe: Type = sel.bound match

tests/warn/i22681.scala

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
//> using options -Wunused:all
3+
4+
trait T:
5+
def t: Int
6+
7+
class C:
8+
def f: Runnable = new Runnable with T:
9+
private def v = 42 // avoid g judged too trivial to warn
10+
def run() = ()
11+
def g = v // warn effectively private member is unused
12+
def t = v // nowarn

0 commit comments

Comments
 (0)