Skip to content

Commit 7809f9b

Browse files
authored
Backport "Disable WUnused for params of non-private defs" (#17280)
Backports #17223
2 parents c043299 + 84458c7 commit 7809f9b

File tree

7 files changed

+85
-60
lines changed

7 files changed

+85
-60
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
118118

119119
override def prepareForDefDef(tree: tpd.DefDef)(using Context): Context =
120120
unusedDataApply{ ud =>
121+
if !tree.symbol.is(Private) then
122+
tree.termParamss.flatten.foreach { p =>
123+
ud.addIgnoredParam(p.symbol)
124+
}
121125
import ud.registerTrivial
122126
tree.registerTrivial
123127
traverseAnnotations(tree.symbol)
@@ -350,6 +354,8 @@ object CheckUnused:
350354
/** Trivial definitions, avoid registering params */
351355
private val trivialDefs = MutSet[Symbol]()
352356

357+
private val paramsToSkip = MutSet[Symbol]()
358+
353359
/**
354360
* Push a new Scope of the given type, executes the given Unit and
355361
* pop it back to the original type.
@@ -396,6 +402,8 @@ object CheckUnused:
396402
def removeIgnoredUsage(sym: Symbol)(using Context): Unit =
397403
doNotRegister --= sym.everySymbol
398404

405+
def addIgnoredParam(sym: Symbol)(using Context): Unit =
406+
paramsToSkip += sym
399407

400408
/** Register an import */
401409
def registerImport(imp: tpd.Import)(using Context): Unit =
@@ -410,8 +418,9 @@ object CheckUnused:
410418
if memDef.isValidMemberDef then
411419
if memDef.isValidParam then
412420
if memDef.symbol.isOneOf(GivenOrImplicit) then
413-
implicitParamInScope += memDef
414-
else
421+
if !paramsToSkip.contains(memDef.symbol) then
422+
implicitParamInScope += memDef
423+
else if !paramsToSkip.contains(memDef.symbol) then
415424
explicitParamInScope += memDef
416425
else if currScopeType.top == ScopeType.Local then
417426
localDefInScope += memDef

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ trait InterFace {
99
}
1010

1111
trait BadAPI extends InterFace {
12-
def f(a: Int,
12+
private def f(a: Int,
1313
b: String, // error
1414
c: Double): Int = {
1515
println(c)
@@ -33,7 +33,7 @@ trait BadAPI extends InterFace {
3333

3434
override def equals(other: Any): Boolean = true // OK
3535

36-
def i(implicit s: String) = answer // error
36+
def i(implicit s: String) = answer // ok
3737

3838
/*
3939
def future(x: Int): Int = {
@@ -63,7 +63,7 @@ case class CaseyKasem(k: Int) // OK
6363
case class CaseyAtTheBat(k: Int)(s: String) // ok
6464

6565
trait Ignorance {
66-
def f(readResolve: Int) = answer // error
66+
def f(readResolve: Int) = answer // ok
6767
}
6868

6969
class Reusing(u: Int) extends Unusing(u) // OK
@@ -78,28 +78,28 @@ trait Unimplementation {
7878
}
7979

8080
trait DumbStuff {
81-
def f(implicit dummy: DummyImplicit) = answer // todo // error
82-
def g(dummy: DummyImplicit) = answer // error
81+
def f(implicit dummy: DummyImplicit) = answer // ok
82+
def g(dummy: DummyImplicit) = answer // ok
8383
}
8484
trait Proofs {
85-
def f[A, B](implicit ev: A =:= B) = answer // todo // error
86-
def g[A, B](implicit ev: A <:< B) = answer // todo // error
87-
def f2[A, B](ev: A =:= B) = answer // error
88-
def g2[A, B](ev: A <:< B) = answer // error
85+
def f[A, B](implicit ev: A =:= B) = answer // ok
86+
def g[A, B](implicit ev: A <:< B) = answer // ok
87+
def f2[A, B](ev: A =:= B) = answer // ok
88+
def g2[A, B](ev: A <:< B) = answer // ok
8989
}
9090

9191
trait Anonymous {
92-
def f = (i: Int) => answer // error
92+
def f = (i: Int) => answer // ok
9393

9494
def f1 = (_: Int) => answer // OK
9595

9696
def f2: Int => Int = _ + 1 // OK
9797

98-
def g = for (i <- List(1)) yield answer // error
98+
def g = for (i <- List(1)) yield answer // ok
9999
}
100100
trait Context[A]
101101
trait Implicits {
102-
def f[A](implicit ctx: Context[A]) = answer // error
102+
def f[A](implicit ctx: Context[A]) = answer // ok
103103
def g[A: Context] = answer // OK
104104
}
105105
class Bound[A: Context] // OK

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

+25-23
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
// scalac: -Wunused:explicits
22

3-
/* This goes around the "trivial method" detection */
4-
val default_val = 1
3+
object Foo {
4+
/* This goes around the "trivial method" detection */
5+
val default_val = 1
56

6-
def f1(a: Int) = a // OK
7-
def f2(a: Int) = default_val // error
8-
def f3(a: Int)(using Int) = a // OK
9-
def f4(a: Int)(using Int) = default_val // error
10-
def f6(a: Int)(using Int) = summon[Int] // error
11-
def f7(a: Int)(using Int) = summon[Int] + a // OK
7+
private def f1(a: Int) = a // OK
8+
private def f2(a: Int) = default_val // error
9+
private def f3(a: Int)(using Int) = a // OK
10+
private def f4(a: Int)(using Int) = default_val // error
11+
private def f6(a: Int)(using Int) = summon[Int] // error
12+
private def f7(a: Int)(using Int) = summon[Int] + a // OK
13+
}
1214

1315
package scala2main.unused.args:
1416
object happyBirthday {
15-
def main(args: Array[String]): Unit = println("Hello World") // error
17+
def main(args: Array[String]): Unit = println("Hello World") // ok
1618
}
1719

1820
package scala2main:
@@ -29,27 +31,27 @@ package scala3main:
2931
package foo.test.lambda.param:
3032
val default_val = 1
3133
val a = (i: Int) => i // OK
32-
val b = (i: Int) => default_val // error
34+
val b = (i: Int) => default_val // OK
3335
val c = (_: Int) => default_val // OK
3436

3537
package foo.test.trivial:
3638
/* A twisted test from Scala 2 */
3739
class C {
3840
def answer: 42 = 42
3941
object X
40-
def g0(x: Int) = ??? // OK
41-
def f0(x: Int) = () // OK
42-
def f1(x: Int) = throw new RuntimeException // OK
43-
def f2(x: Int) = 42 // OK
44-
def f3(x: Int): Option[Int] = None // OK
45-
def f4(x: Int) = classOf[Int] // OK
46-
def f5(x: Int) = answer + 27 // OK
47-
def f6(x: Int) = X // OK
48-
def f7(x: Int) = Y // OK
49-
def f8(x: Int): List[C] = Nil // OK
50-
def f9(x: Int): List[Int] = List(1,2,3,4) // error
51-
def foo:Int = 32 // OK
52-
def f77(x: Int) = foo // error
42+
private def g0(x: Int) = ??? // OK
43+
private def f0(x: Int) = () // OK
44+
private def f1(x: Int) = throw new RuntimeException // OK
45+
private def f2(x: Int) = 42 // OK
46+
private def f3(x: Int): Option[Int] = None // OK
47+
private def f4(x: Int) = classOf[Int] // OK
48+
private def f5(x: Int) = answer + 27 // OK
49+
private def f6(x: Int) = X // OK
50+
private def f7(x: Int) = Y // OK
51+
private def f8(x: Int): List[C] = Nil // OK
52+
private def f9(x: Int): List[Int] = List(1,2,3,4) // error
53+
private def foo:Int = 32 // OK
54+
private def f77(x: Int) = foo // error
5355
}
5456
object Y
5557

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

+9-8
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
/* This goes around the "trivial method" detection */
44
val default_int = 1
55

6-
def f1(a: Int) = a // OK
7-
def f2(a: Int) = 1 // OK
8-
def f3(a: Int)(using Int) = a // OK
9-
def f4(a: Int)(using Int) = default_int // OK
10-
def f6(a: Int)(using Int) = summon[Int] // OK
11-
def f7(a: Int)(using Int) = summon[Int] + a // OK
12-
def f8(a: Int)(using foo: Int) = a // error
13-
6+
object Xd {
7+
private def f1(a: Int) = a // OK
8+
private def f2(a: Int) = 1 // OK
9+
private def f3(a: Int)(using Int) = a // OK
10+
private def f4(a: Int)(using Int) = default_int // OK
11+
private def f6(a: Int)(using Int) = summon[Int] // OK
12+
private def f7(a: Int)(using Int) = summon[Int] + a // OK
13+
private def f8(a: Int)(using foo: Int) = a // error
14+
}

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

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
// scalac: -Wunused:params
22

33
/* This goes around the "trivial method" detection */
4-
val default_int = 1
4+
object Foo {
5+
val default_int = 1
56

6-
def f1(a: Int) = a // OK
7-
def f2(a: Int) = default_int // error
8-
def f3(a: Int)(using Int) = a // OK
9-
def f4(a: Int)(using Int) = default_int // error
10-
def f6(a: Int)(using Int) = summon[Int] // error
11-
def f7(a: Int)(using Int) = summon[Int] + a // OK
12-
13-
/* --- Trivial method check --- */
14-
def g1(x: Int) = 1 // OK
15-
def g2(x: Int) = ??? // OK
7+
private def f1(a: Int) = a // OK
8+
private def f2(a: Int) = default_int // error
9+
private def f3(a: Int)(using Int) = a // OK
10+
private def f4(a: Int)(using Int) = default_int // error
11+
private def f6(a: Int)(using Int) = summon[Int] // error
12+
private def f7(a: Int)(using Int) = summon[Int] + a // OK
13+
/* --- Trivial method check --- */
14+
private def g1(x: Int) = 1 // OK
15+
private def g2(x: Int) = ??? // OK
16+
}
1617

1718
package foo.test.i17101:
1819
type Test[A] = A

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class A {
77
val b = 2 // OK
88

99
private def c = 2 // error
10-
def d(using x:Int): Int = b // error
10+
def d(using x:Int): Int = b // ok
1111
def e(x: Int) = 1 // OK
1212
def f =
1313
val x = 1 // error

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ class A {
1717
private def c2 = 2 // OK
1818
def c3 = c2
1919

20-
def d1(using x:Int): Int = default_int // error
20+
def d1(using x:Int): Int = default_int // ok
2121
def d2(using x:Int): Int = x // OK
2222

23-
def e1(x: Int) = default_int // error
23+
def e1(x: Int) = default_int // ok
2424
def e2(x: Int) = x // OK
2525
def f =
2626
val x = 1 // error
@@ -44,7 +44,8 @@ package foo.test.scala.annotation:
4444
val default_int = 12
4545

4646
def a1(a: Int) = a // OK
47-
def a2(a: Int) = default_int // error
47+
def a2(a: Int) = default_int // ok
48+
4849
def a3(@unused a: Int) = default_int //OK
4950

5051
def b1 =
@@ -288,6 +289,17 @@ package foo.test.i17156:
288289
import b.Xd
289290
trait Z derives Xd
290291

292+
293+
package foo.test.i17175:
294+
val continue = true
295+
def foo =
296+
for {
297+
i <- 1.until(10) // OK
298+
if continue
299+
} {
300+
println(i)
301+
}
302+
291303
package foo.test.i17117:
292304
package example {
293305
object test1 {

0 commit comments

Comments
 (0)