Skip to content

Commit be7c9bb

Browse files
som-snytttgodzik
authored andcommitted
Collect nowarn symbols instead of skipping them
[Cherry-picked d0a11e0]
1 parent 8f7101f commit be7c9bb

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
@@ -441,6 +441,7 @@ object CheckUnused:
441441
val refs = mutable.Set.empty[Symbol] // references
442442
val asss = mutable.Set.empty[Symbol] // targets of assignment
443443
val skip = mutable.Set.empty[Symbol] // methods to skip (don't warn about their params)
444+
val nowarn = mutable.Set.empty[Symbol] // marked @nowarn
444445
val imps = new IdentityHashMap[Import, Unit] // imports
445446
val sels = new IdentityHashMap[ImportSelector, Unit] // matched selectors
446447
def register(tree: Tree)(using Context): Unit = if inlined.isEmpty then
@@ -453,17 +454,20 @@ object CheckUnused:
453454
then
454455
imps.put(imp, ())
455456
case tree: Bind =>
456-
if !tree.name.isInstanceOf[DerivedName] && !tree.name.is(WildcardParamName) && !tree.hasAttachment(NoWarn) then
457+
if !tree.name.isInstanceOf[DerivedName] && !tree.name.is(WildcardParamName) then
458+
if tree.hasAttachment(NoWarn) then
459+
nowarn.addOne(tree.symbol)
457460
pats.addOne((tree.symbol, tree.namePos))
458461
case tree: NamedDefTree =>
459462
if tree.hasAttachment(PatternVar) then
460463
if !tree.name.isInstanceOf[DerivedName] then
461464
pats.addOne((tree.symbol, tree.namePos))
462465
else if (tree.symbol ne NoSymbol)
463466
&& !tree.name.isWildcard
464-
&& !tree.hasAttachment(NoWarn)
465467
&& !tree.symbol.is(ModuleVal) // track only the ModuleClass using the object symbol, with correct namePos
466468
then
469+
if tree.hasAttachment(NoWarn) then
470+
nowarn.addOne(tree.symbol)
467471
defs.addOne((tree.symbol.userSymbol, tree.namePos))
468472
case _ =>
469473
if tree.symbol ne NoSymbol then
@@ -531,6 +535,7 @@ object CheckUnused:
531535
&& !sym.name.is(BodyRetainerName)
532536
&& !sym.isSerializationSupport
533537
&& !(sym.is(Mutable) && sym.isSetter && sym.owner.is(Trait)) // tracks sym.underlyingSymbol sibling getter
538+
&& !infos.nowarn(sym)
534539
then
535540
warnAt(pos)(UnusedSymbol.privateMembers)
536541

@@ -627,7 +632,7 @@ object CheckUnused:
627632
val byPos = infos.pats.groupMap(uniformPos(_, _))((sym, pos) => sym)
628633
for (pos, syms) <- byPos if pos.span.exists && !syms.exists(_.hasAnnotation(defn.UnusedAnnot)) do
629634
if !syms.exists(infos.refs(_)) then
630-
if !syms.exists(v => !v.isLocal && !v.is(Private)) then
635+
if !syms.exists(v => !v.isLocal && !v.is(Private) || infos.nowarn(v)) then
631636
warnAt(pos)(UnusedSymbol.patVars)
632637
else if syms.exists(_.is(Mutable)) then // check unassigned var
633638
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)