Skip to content

Commit 57bf2f4

Browse files
szymon-rdKordyjan
authored andcommitted
WUnused: Fix unused warnining in synthetic symbols
1 parent 609bc59 commit 57bf2f4

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-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, 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
@@ -297,6 +297,7 @@ object CheckUnused:
297297
* See the `isAccessibleAsIdent` extension method below in the file
298298
*/
299299
private val usedInScope = MutStack(MutSet[(Symbol,Boolean, Option[Name])]())
300+
private val usedInPosition = MutSet[(SrcPos, Name)]()
300301
/* unused import collected during traversal */
301302
private val unusedImport = MutSet[ImportSelector]()
302303

@@ -346,6 +347,7 @@ object CheckUnused:
346347
usedInScope.top += ((sym, sym.isAccessibleAsIdent, name))
347348
usedInScope.top += ((sym.companionModule, sym.isAccessibleAsIdent, name))
348349
usedInScope.top += ((sym.companionClass, sym.isAccessibleAsIdent, name))
350+
name.map(n => usedInPosition += ((sym.sourcePos, n)))
349351

350352
/** Register a symbol that should be ignored */
351353
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
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+
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)