Skip to content

Commit 345c16d

Browse files
committed
Warn unused member of anonymous class
1 parent 332fceb commit 345c16d

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

Diff for: 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

Diff for: tests/semanticdb/metac.expect

+2-1
Original file line numberDiff line numberDiff line change
@@ -3494,7 +3494,7 @@ Text => empty
34943494
Language => Scala
34953495
Symbols => 12 entries
34963496
Occurrences => 33 entries
3497-
Diagnostics => 1 entries
3497+
Diagnostics => 2 entries
34983498
Synthetics => 4 entries
34993499

35003500
Symbols:
@@ -3550,6 +3550,7 @@ Diagnostics:
35503550
[14:20..14:23): [warning] Alphanumeric method foo is not declared infix; it should not be used as infix operator.
35513551
Instead, use method syntax .foo(...) or backticked identifier `foo`.
35523552
The latter can be rewritten automatically under -rewrite -source 3.4-migration.
3553+
[19:8..19:17): [warning] unused private member
35533554

35543555
Synthetics:
35553556
[12:2..12:6):user => reflectiveSelectable(*)

Diff for: tests/warn/i22681.scala

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
//> using options -Wunused:all
3+
4+
trait T:
5+
def t: Int
6+
7+
class C:
8+
def f: Runnable { def u: Int } = 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
13+
def u = v // warn despite structural type

0 commit comments

Comments
 (0)