Skip to content
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
15 changes: 12 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import dotty.tools.dotc.config.ScalaSettings
import dotty.tools.dotc.core.Contexts.*
import dotty.tools.dotc.core.Flags.*
import dotty.tools.dotc.core.Names.{Name, SimpleName, DerivedName, TermName, termName}
import dotty.tools.dotc.core.NameOps.{isAnonymousFunctionName, isReplWrapperName}
import dotty.tools.dotc.core.NameOps.{isAnonymousFunctionName, isReplWrapperName, setterName}
import dotty.tools.dotc.core.NameKinds.{
BodyRetainerName, ContextBoundParamName, ContextFunctionParamName, DefaultGetterName, WildcardParamName}
import dotty.tools.dotc.core.StdNames.nme
Expand Down Expand Up @@ -505,7 +505,13 @@ object CheckUnused:
if sym.isLocalToBlock then
if ctx.settings.WunusedHas.locals && sym.is(Mutable) && !infos.asss(sym) then
warnAt(pos)(UnusedSymbol.unsetLocals)
else if ctx.settings.WunusedHas.privates && sym.isAllOf(Private | Mutable) && !infos.asss(sym) then
else if ctx.settings.WunusedHas.privates
&& sym.is(Mutable)
&& (sym.is(Private) || sym.isEffectivelyPrivate)
&& !sym.isSetter // tracks sym.underlyingSymbol sibling getter, check setter below
&& !infos.asss(sym)
&& !infos.refs(sym.owner.info.member(sym.name.asTermName.setterName).symbol)
then
warnAt(pos)(UnusedSymbol.unsetPrivates)

def checkPrivate(sym: Symbol, pos: SrcPos) =
Expand All @@ -514,7 +520,10 @@ object CheckUnused:
&& !sym.isOneOf(SelfName | Synthetic | CaseAccessor)
&& !sym.name.is(BodyRetainerName)
&& !sym.isSerializationSupport
&& !(sym.is(Mutable) && sym.isSetter && sym.owner.is(Trait)) // tracks sym.underlyingSymbol sibling getter
&& !( sym.is(Mutable)
&& sym.isSetter // tracks sym.underlyingSymbol sibling getter
&& (sym.owner.is(Trait) || sym.owner.isAnonymousClass)
)
&& !infos.nowarn(sym)
then
warnAt(pos)(UnusedSymbol.privateMembers)
Expand Down
8 changes: 8 additions & 0 deletions tests/warn/i23200.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- [E198] Unused Symbol Warning: tests/warn/i23200.scala:8:8 -----------------------------------------------------------
8 | var x: Int = 42 // warn
| ^
| unset private variable, consider using an immutable val instead
-- [E198] Unused Symbol Warning: tests/warn/i23200.scala:40:6 ----------------------------------------------------------
40 | var x: Int = 42 // warn local var
| ^
| unset local variable, consider using an immutable val instead
41 changes: 41 additions & 0 deletions tests/warn/i23200.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//> using options -Wunused:all

trait Foo
trait Bar

def `anon not updated` =
new Foo {
var x: Int = 42 // warn
val _ = new Bar:
println(x)
//x = 27
//x_=(27)
}
def `anon yes updated` =
new Foo {
var x: Int = 42 // nowarn
val _ = new Bar:
println(x)
x = 27
//x_=(27)
}
def `anon yes updated from nested context` =
new Foo {
var x: Int = 42 // nowarn
val _ = new Bar:
println(x)
x = 27
//x_=(27)
}
def `anon yes updated in daring use of setter` =
new Foo {
var x: Int = 42 // nowarn
val _ = new Bar:
println(x)
//x = 27
x_=(27)
}

def f: Unit =
var x: Int = 42 // warn local var
println(x)
Loading