Skip to content

Commit 424da9f

Browse files
authored
WUnused: Fix unused warnining in synthetic symbols (#17020)
Resolves #16925 #16926
2 parents ce65296 + fa5df05 commit 424da9f

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import dotty.tools.dotc.ast.untpd.ImportSelector
77
import dotty.tools.dotc.config.ScalaSettings
88
import dotty.tools.dotc.core.Contexts.*
99
import dotty.tools.dotc.core.Decorators.{em, i}
10-
import dotty.tools.dotc.core.Flags._
10+
import dotty.tools.dotc.core.Flags.*
1111
import dotty.tools.dotc.core.Phases.Phase
1212
import dotty.tools.dotc.core.StdNames
1313
import dotty.tools.dotc.report
1414
import dotty.tools.dotc.reporting.Message
1515
import dotty.tools.dotc.typer.ImportInfo
16-
import dotty.tools.dotc.util.Property
16+
import dotty.tools.dotc.util.{Property, SrcPos}
1717
import dotty.tools.dotc.core.Mode
1818
import dotty.tools.dotc.core.Types.TypeTraverser
1919
import dotty.tools.dotc.core.Types.Type
@@ -302,6 +302,7 @@ object CheckUnused:
302302
* See the `isAccessibleAsIdent` extension method below in the file
303303
*/
304304
private val usedInScope = MutStack(MutSet[(Symbol,Boolean, Option[Name])]())
305+
private val usedInPosition = MutSet[(SrcPos, Name)]()
305306
/* unused import collected during traversal */
306307
private val unusedImport = MutSet[ImportSelector]()
307308

@@ -351,6 +352,7 @@ object CheckUnused:
351352
usedInScope.top += ((sym, sym.isAccessibleAsIdent, name))
352353
usedInScope.top += ((sym.companionModule, sym.isAccessibleAsIdent, name))
353354
usedInScope.top += ((sym.companionClass, sym.isAccessibleAsIdent, name))
355+
name.map(n => usedInPosition += ((sym.sourcePos, n)))
354356

355357
/** Register a symbol that should be ignored */
356358
def addIgnoredUsage(sym: Symbol)(using Context): Unit =
@@ -455,6 +457,7 @@ object CheckUnused:
455457
if ctx.settings.WunusedHas.locals then
456458
localDefInScope
457459
.filterNot(d => d.symbol.usedDefContains)
460+
.filterNot(d => usedInPosition.exists { case (pos, name) => d.span.contains(pos.span) && name == d.symbol.name})
458461
.map(d => d.namePos -> WarnTypes.LocalDefs).toList
459462
else
460463
Nil
@@ -483,6 +486,7 @@ object CheckUnused:
483486
if ctx.settings.WunusedHas.patvars then
484487
patVarsInScope
485488
.filterNot(d => d.symbol.usedDefContains)
489+
.filterNot(d => usedInPosition.exists { case (pos, name) => d.span.contains(pos.span) && name == d.symbol.name})
486490
.map(d => d.namePos -> WarnTypes.PatVars).toList
487491
else
488492
Nil

tests/neg-custom-args/fatal-warnings/i15503i.scala

+14
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,17 @@ package foo.test.i16877:
194194

195195
@ExampleAnnotation(new HashMap()) // OK
196196
class Test //OK
197+
198+
package foo.test.i16926:
199+
def hello(): Unit =
200+
for {
201+
i <- (0 to 10).toList
202+
(a, b) = "hello" -> "world" // OK
203+
} yield println(s"$a $b")
204+
205+
package foo.test.i16925:
206+
def hello =
207+
for {
208+
i <- 1 to 2 if true
209+
_ = println(i) // OK
210+
} yield ()

0 commit comments

Comments
 (0)