From e5d4be3fcd349896a491c9685a45f903d43ed19a Mon Sep 17 00:00:00 2001 From: Szymon Rodziewicz Date: Wed, 29 Mar 2023 18:08:33 +0200 Subject: [PATCH 1/2] Fix WUnused false positive in for --- .../src/dotty/tools/dotc/transform/CheckUnused.scala | 1 + tests/neg-custom-args/fatal-warnings/i15503i.scala | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/compiler/src/dotty/tools/dotc/transform/CheckUnused.scala b/compiler/src/dotty/tools/dotc/transform/CheckUnused.scala index 5a178ff2ec1f..6c6bfea476d8 100644 --- a/compiler/src/dotty/tools/dotc/transform/CheckUnused.scala +++ b/compiler/src/dotty/tools/dotc/transform/CheckUnused.scala @@ -473,6 +473,7 @@ object CheckUnused: if ctx.settings.WunusedHas.explicits then explicitParamInScope .filterNot(d => d.symbol.usedDefContains) + .filterNot(d => usedInPosition.exists { case (pos, name) => d.span.contains(pos.span) && name == d.symbol.name}) .filterNot(d => containsSyntheticSuffix(d.symbol)) .map(d => d.namePos -> WarnTypes.ExplicitParams).toList else diff --git a/tests/neg-custom-args/fatal-warnings/i15503i.scala b/tests/neg-custom-args/fatal-warnings/i15503i.scala index 737e5f0739ca..76a982df3d67 100644 --- a/tests/neg-custom-args/fatal-warnings/i15503i.scala +++ b/tests/neg-custom-args/fatal-warnings/i15503i.scala @@ -287,3 +287,13 @@ package foo.test.i17156: package c: import b.Xd trait Z derives Xd + +package foo.test.i17175: + val continue = true + def foo = + for { + i <- 1.until(10) // OK + if continue + } { + println(i) + } \ No newline at end of file From fb0efe5a85529a42d38eff2c003cca09e3dd8a54 Mon Sep 17 00:00:00 2001 From: Szymon Rodziewicz Date: Thu, 13 Apr 2023 13:32:30 +0200 Subject: [PATCH 2/2] Do not register used symbol when position doesnt exist in wunused --- compiler/src/dotty/tools/dotc/transform/CheckUnused.scala | 3 ++- .../fatal-warnings/i15503-scala2/scala2-t11681.scala | 2 +- tests/neg-custom-args/fatal-warnings/i15503i.scala | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/transform/CheckUnused.scala b/compiler/src/dotty/tools/dotc/transform/CheckUnused.scala index 6c6bfea476d8..b4db279a522c 100644 --- a/compiler/src/dotty/tools/dotc/transform/CheckUnused.scala +++ b/compiler/src/dotty/tools/dotc/transform/CheckUnused.scala @@ -358,7 +358,8 @@ object CheckUnused: usedInScope.top += ((sym, sym.isAccessibleAsIdent, name, isDerived)) usedInScope.top += ((sym.companionModule, sym.isAccessibleAsIdent, name, isDerived)) usedInScope.top += ((sym.companionClass, sym.isAccessibleAsIdent, name, isDerived)) - name.map(n => usedInPosition += ((sym.sourcePos, n))) + if sym.sourcePos.exists then + name.map(n => usedInPosition += ((sym.sourcePos, n))) /** Register a symbol that should be ignored */ def addIgnoredUsage(sym: Symbol)(using Context): Unit = diff --git a/tests/neg-custom-args/fatal-warnings/i15503-scala2/scala2-t11681.scala b/tests/neg-custom-args/fatal-warnings/i15503-scala2/scala2-t11681.scala index 18aa6879eeba..912dbb456f3b 100644 --- a/tests/neg-custom-args/fatal-warnings/i15503-scala2/scala2-t11681.scala +++ b/tests/neg-custom-args/fatal-warnings/i15503-scala2/scala2-t11681.scala @@ -60,7 +60,7 @@ class Revaluing(u: Int) { def f = u } // OK case class CaseyKasem(k: Int) // OK -case class CaseyAtTheBat(k: Int)(s: String) // error +case class CaseyAtTheBat(k: Int)(s: String) // ok trait Ignorance { def f(readResolve: Int) = answer // error diff --git a/tests/neg-custom-args/fatal-warnings/i15503i.scala b/tests/neg-custom-args/fatal-warnings/i15503i.scala index 76a982df3d67..f67e273ca6f5 100644 --- a/tests/neg-custom-args/fatal-warnings/i15503i.scala +++ b/tests/neg-custom-args/fatal-warnings/i15503i.scala @@ -90,7 +90,7 @@ package foo.test.possibleclasses: k: Int, // OK private val y: Int // OK /* Kept as it can be taken from pattern */ )( - s: Int, // error /* But not these */ + s: Int, val t: Int, // OK private val z: Int // error ) @@ -131,7 +131,7 @@ package foo.test.possibleclasses.withvar: k: Int, // OK private var y: Int // OK /* Kept as it can be taken from pattern */ )( - s: Int, // error /* But not these */ + s: Int, var t: Int, // OK private var z: Int // error )