Skip to content

Commit b821cbe

Browse files
committed
Collect nowarn symbols instead of skipping them
1 parent 0bbd04f commit b821cbe

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

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

+7-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: ValDef if tree.hasAttachment(PatternVar) =>
468471
if !tree.name.isInstanceOf[DerivedName] then
469472
pats.addOne((tree.symbol, tree.namePos))
470473
case tree: NamedDefTree =>
471474
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
@@ -634,7 +638,7 @@ object CheckUnused:
634638
val byPos = infos.pats.groupMap(uniformPos(_, _))((sym, pos) => sym)
635639
for (pos, syms) <- byPos if pos.span.exists && !syms.exists(_.hasAnnotation(defn.UnusedAnnot)) do
636640
if !syms.exists(infos.refs(_)) then
637-
if !syms.exists(v => !v.isLocal && !v.is(Private)) then
641+
if !syms.exists(v => !v.isLocal && !v.is(Private) || infos.nowarn(v)) then
638642
warnAt(pos)(UnusedSymbol.patVars)
639643
else if syms.exists(_.is(Mutable)) then // check unassigned var
640644
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)