Skip to content

Commit ad984d8

Browse files
szymon-rdKordyjan
authored andcommitted
WUnused: Fix unused warnining in synthetic symbols
1 parent aecbfa7 commit ad984d8

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
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, SourcePosition, SrcPos}
1717
import dotty.tools.dotc.core.Mode
1818
import dotty.tools.dotc.core.Types.TypeTraverser
1919
import dotty.tools.dotc.core.Types.Type
@@ -289,6 +289,7 @@ object CheckUnused:
289289
* See the `isAccessibleAsIdent` extension method below in the file
290290
*/
291291
private val usedInScope = MutStack(MutSet[(Symbol,Boolean, Option[Name])]())
292+
private val usedInPosition = MutSet[(SrcPos, Name)]()
292293
/* unused import collected during traversal */
293294
private val unusedImport = MutSet[ImportSelector]()
294295

@@ -343,6 +344,7 @@ object CheckUnused:
343344
usedInScope.top += ((sym, sym.isAccessibleAsIdent, name))
344345
usedInScope.top += ((sym.companionModule, sym.isAccessibleAsIdent, name))
345346
usedInScope.top += ((sym.companionClass, sym.isAccessibleAsIdent, name))
347+
name.map(n => usedInPosition += ((sym.sourcePos, n)))
346348

347349
/** Register a symbol that should be ignored */
348350
def addIgnoredUsage(sym: Symbol)(using Context): Unit =
@@ -450,6 +452,7 @@ object CheckUnused:
450452
if ctx.settings.WunusedHas.locals then
451453
localDefInScope
452454
.filterNot(d => d.symbol.usedDefContains)
455+
.filterNot(d => usedInPosition.exists { case (pos, name) => d.span.contains(pos.span) && name == d.symbol.name})
453456
.map(d => d.namePos -> WarnTypes.LocalDefs).toList
454457
else
455458
Nil
@@ -478,6 +481,7 @@ object CheckUnused:
478481
if ctx.settings.WunusedHas.patvars then
479482
patVarsInScope
480483
.filterNot(d => d.symbol.usedDefContains)
484+
.filterNot(d => usedInPosition.exists { case (pos, name) => d.span.contains(pos.span) && name == d.symbol.name})
481485
.map(d => d.namePos -> WarnTypes.PatVars).toList
482486
else
483487
Nil
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// scalac: -Wunused:all
2+
3+
def hello =
4+
for {
5+
i <- 1 to 2 if true
6+
_ = println(i) // OK
7+
} yield ()
8+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// scalac: -Wunused:all
2+
3+
def hello(): Unit =
4+
for {
5+
i <- (0 to 10).toList
6+
(a, b) = "hello" -> "world" // OK
7+
} yield println(s"$a $b")

0 commit comments

Comments
 (0)