Skip to content

Commit 29bc3db

Browse files
authored
Backport "Fix WUnused false positive in for" (#17278)
Backports #17176
2 parents a6eee4f + fd7b962 commit 29bc3db

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

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

+3-1
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

+1-1
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

+3-3
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
)
@@ -300,4 +300,4 @@ package foo.test.i17117:
300300

301301
val test = t1.test
302302
}
303-
}
303+
}

0 commit comments

Comments
 (0)