Skip to content

Commit af8790c

Browse files
authored
Fix WUnused false positive in for (#17176)
Fix #17175
2 parents 73af81e + 408ff2f commit af8790c

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,8 @@ object CheckUnused:
358358
usedInScope.top += ((sym, sym.isAccessibleAsIdent, name, isDerived))
359359
usedInScope.top += ((sym.companionModule, sym.isAccessibleAsIdent, name, isDerived))
360360
usedInScope.top += ((sym.companionClass, sym.isAccessibleAsIdent, name, isDerived))
361-
name.map(n => usedInPosition += ((sym.sourcePos, n)))
361+
if sym.sourcePos.exists then
362+
name.map(n => usedInPosition += ((sym.sourcePos, n)))
362363

363364
/** Register a symbol that should be ignored */
364365
def addIgnoredUsage(sym: Symbol)(using Context): Unit =
@@ -473,6 +474,7 @@ object CheckUnused:
473474
if ctx.settings.WunusedHas.explicits then
474475
explicitParamInScope
475476
.filterNot(d => d.symbol.usedDefContains)
477+
.filterNot(d => usedInPosition.exists { case (pos, name) => d.span.contains(pos.span) && name == d.symbol.name})
476478
.filterNot(d => containsSyntheticSuffix(d.symbol))
477479
.map(d => d.namePos -> WarnTypes.ExplicitParams).toList
478480
else

tests/neg-custom-args/fatal-warnings/i15503-scala2/scala2-t11681.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class Revaluing(u: Int) { def f = u } // OK
6060

6161
case class CaseyKasem(k: Int) // OK
6262

63-
case class CaseyAtTheBat(k: Int)(s: String) // error
63+
case class CaseyAtTheBat(k: Int)(s: String) // ok
6464

6565
trait Ignorance {
6666
def f(readResolve: Int) = answer // error

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ package foo.test.possibleclasses:
9090
k: Int, // OK
9191
private val y: Int // OK /* Kept as it can be taken from pattern */
9292
)(
93-
s: Int, // error /* But not these */
93+
s: Int,
9494
val t: Int, // OK
9595
private val z: Int // error
9696
)
@@ -131,7 +131,7 @@ package foo.test.possibleclasses.withvar:
131131
k: Int, // OK
132132
private var y: Int // OK /* Kept as it can be taken from pattern */
133133
)(
134-
s: Int, // error /* But not these */
134+
s: Int,
135135
var t: Int, // OK
136136
private var z: Int // error
137137
)
@@ -288,6 +288,17 @@ package foo.test.i17156:
288288
import b.Xd
289289
trait Z derives Xd
290290

291+
292+
package foo.test.i17175:
293+
val continue = true
294+
def foo =
295+
for {
296+
i <- 1.until(10) // OK
297+
if continue
298+
} {
299+
println(i)
300+
}
301+
291302
package foo.test.i17117:
292303
package example {
293304
object test1 {
@@ -300,4 +311,4 @@ package foo.test.i17117:
300311

301312
val test = t1.test
302313
}
303-
}
314+
}

0 commit comments

Comments
 (0)