Skip to content

Commit d0a11e0

Browse files
committed
Collect nowarn symbols instead of skipping them
1 parent ddc2b33 commit d0a11e0

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ object CheckUnused:
450450
val refs = mutable.Set.empty[Symbol] // references
451451
val asss = mutable.Set.empty[Symbol] // targets of assignment
452452
val skip = mutable.Set.empty[Symbol] // methods to skip (don't warn about their params)
453+
val nowarn = mutable.Set.empty[Symbol] // marked @nowarn
453454
val imps = new IdentityHashMap[Import, Unit] // imports
454455
val sels = new IdentityHashMap[ImportSelector, Unit] // matched selectors
455456
def register(tree: Tree)(using Context): Unit = if inlined.isEmpty then
@@ -462,17 +463,20 @@ object CheckUnused:
462463
then
463464
imps.put(imp, ())
464465
case tree: Bind =>
465-
if !tree.name.isInstanceOf[DerivedName] && !tree.name.is(WildcardParamName) && !tree.hasAttachment(NoWarn) then
466+
if !tree.name.isInstanceOf[DerivedName] && !tree.name.is(WildcardParamName) then
467+
if tree.hasAttachment(NoWarn) then
468+
nowarn.addOne(tree.symbol)
466469
pats.addOne((tree.symbol, tree.namePos))
467470
case tree: NamedDefTree =>
468471
if tree.hasAttachment(PatternVar) then
469472
if !tree.name.isInstanceOf[DerivedName] then
470473
pats.addOne((tree.symbol, tree.namePos))
471474
else if (tree.symbol ne NoSymbol)
472475
&& !tree.name.isWildcard
473-
&& !tree.hasAttachment(NoWarn)
474476
&& !tree.symbol.is(ModuleVal) // track only the ModuleClass using the object symbol, with correct namePos
475477
then
478+
if tree.hasAttachment(NoWarn) then
479+
nowarn.addOne(tree.symbol)
476480
defs.addOne((tree.symbol.userSymbol, tree.namePos))
477481
case _ =>
478482
if tree.symbol ne NoSymbol then
@@ -540,6 +544,7 @@ object CheckUnused:
540544
&& !sym.name.is(BodyRetainerName)
541545
&& !sym.isSerializationSupport
542546
&& !(sym.is(Mutable) && sym.isSetter && sym.owner.is(Trait)) // tracks sym.underlyingSymbol sibling getter
547+
&& !infos.nowarn(sym)
543548
then
544549
warnAt(pos)(UnusedSymbol.privateMembers)
545550

@@ -635,7 +640,7 @@ object CheckUnused:
635640
val byPos = infos.pats.groupMap(uniformPos(_, _))((sym, pos) => sym)
636641
for (pos, syms) <- byPos if pos.span.exists && !syms.exists(_.hasAnnotation(defn.UnusedAnnot)) do
637642
if !syms.exists(infos.refs(_)) then
638-
if !syms.exists(v => !v.isLocal && !v.is(Private)) then
643+
if !syms.exists(v => !v.isLocal && !v.is(Private) || infos.nowarn(v)) then
639644
warnAt(pos)(UnusedSymbol.patVars)
640645
else if syms.exists(_.is(Mutable)) then // check unassigned var
641646
val sym = // recover the original

tests/warn/i15503d.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ case class K(i: Int, j: Int)
2222

2323
class C(c0: Option[Int], k0: K):
2424
private val Some(c) = c0: @unchecked // warn valdef from pattern
25-
private val K(i, j) = k0 // warn // warn valdefs from pattern (RHS patvars are NoWarn)
25+
private val K(i, j) = k0 // nowarn (name of case class element is nowarn)
2626
val K(v, w) = k0 // nowarn nonprivate
2727
private val K(r, s) = k0 // warn // warn valdefs from pattern
2828
def f(x: Option[Int]) = x match

tests/warn/t13095.scala

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//> using options -Wunused:patvars -Werror
2+
3+
case class A(x: Int, y: Int)
4+
5+
object Main {
6+
for {
7+
a <- List.empty[A]
8+
A(x, y) = a
9+
} yield x + y
10+
}

0 commit comments

Comments
 (0)