Skip to content

Commit 5bac6af

Browse files
Backport "Mention extension in unused param warning" to 3.7.1 (#23229)
Backports #23132 to the 3.7.1-RC2. PR submitted by the release tooling.
2 parents b9452c5 + f139047 commit 5bac6af

File tree

4 files changed

+55
-12
lines changed

4 files changed

+55
-12
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3310,23 +3310,29 @@ extends TypeMsg(ConstructorProxyNotValueID):
33103310
|are not values themselves, they can only be referred to in selections."""
33113311

33123312
class UnusedSymbol(errorText: String, val actions: List[CodeAction] = Nil)(using Context)
3313-
extends Message(UnusedSymbolID) {
3313+
extends Message(UnusedSymbolID):
33143314
def kind = MessageKind.UnusedSymbol
33153315

33163316
override def msg(using Context) = errorText
33173317
override def explain(using Context) = ""
33183318
override def actions(using Context) = this.actions
3319-
}
33203319

33213320
object UnusedSymbol:
33223321
def imports(actions: List[CodeAction])(using Context): UnusedSymbol = UnusedSymbol(i"unused import", actions)
33233322
def localDefs(using Context): UnusedSymbol = UnusedSymbol(i"unused local definition")
3324-
def explicitParams(using Context): UnusedSymbol = UnusedSymbol(i"unused explicit parameter")
3325-
def implicitParams(using Context): UnusedSymbol = UnusedSymbol(i"unused implicit parameter")
3323+
def explicitParams(sym: Symbol)(using Context): UnusedSymbol =
3324+
UnusedSymbol(i"unused explicit parameter${paramAddendum(sym)}")
3325+
def implicitParams(sym: Symbol)(using Context): UnusedSymbol =
3326+
UnusedSymbol(i"unused implicit parameter${paramAddendum(sym)}")
33263327
def privateMembers(using Context): UnusedSymbol = UnusedSymbol(i"unused private member")
33273328
def patVars(using Context): UnusedSymbol = UnusedSymbol(i"unused pattern variable")
3328-
def unsetLocals(using Context): UnusedSymbol = UnusedSymbol(i"unset local variable, consider using an immutable val instead")
3329-
def unsetPrivates(using Context): UnusedSymbol = UnusedSymbol(i"unset private variable, consider using an immutable val instead")
3329+
def unsetLocals(using Context): UnusedSymbol =
3330+
UnusedSymbol(i"unset local variable, consider using an immutable val instead")
3331+
def unsetPrivates(using Context): UnusedSymbol =
3332+
UnusedSymbol(i"unset private variable, consider using an immutable val instead")
3333+
private def paramAddendum(sym: Symbol)(using Context): String =
3334+
if sym.denot.owner.is(ExtensionMethod) then i" in extension ${sym.denot.owner}"
3335+
else ""
33303336

33313337
class NonNamedArgumentInJavaAnnotation(using Context) extends SyntaxMsg(NonNamedArgumentInJavaAnnotationID):
33323338

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,8 @@ object CheckUnused:
488488
val warnings = ArrayBuilder.make[MessageInfo]
489489
def warnAt(pos: SrcPos)(msg: UnusedSymbol, origin: String = ""): Unit = warnings.addOne((msg, pos, origin))
490490
val infos = refInfos
491+
//println(infos.defs.mkString("DEFS\n", "\n", "\n---"))
492+
//println(infos.refs.mkString("REFS\n", "\n", "\n---"))
491493

492494
def checkUnassigned(sym: Symbol, pos: SrcPos) =
493495
if sym.isLocalToBlock then
@@ -528,7 +530,7 @@ object CheckUnused:
528530
if aliasSym.isAllOf(PrivateParamAccessor, butNot = CaseAccessor) && !infos.refs(alias.symbol) then
529531
if aliasSym.is(Local) then
530532
if ctx.settings.WunusedHas.explicits then
531-
warnAt(pos)(UnusedSymbol.explicitParams)
533+
warnAt(pos)(UnusedSymbol.explicitParams(aliasSym))
532534
else
533535
if ctx.settings.WunusedHas.privates then
534536
warnAt(pos)(UnusedSymbol.privateMembers)
@@ -542,7 +544,7 @@ object CheckUnused:
542544
&& !sym.name.isInstanceOf[DerivedName]
543545
&& !ctx.platform.isMainMethod(m)
544546
then
545-
warnAt(pos)(UnusedSymbol.explicitParams)
547+
warnAt(pos)(UnusedSymbol.explicitParams(sym))
546548
end checkExplicit
547549
// begin
548550
if !infos.skip(m)
@@ -582,9 +584,9 @@ object CheckUnused:
582584
aliasSym.isAllOf(PrivateParamAccessor, butNot = CaseAccessor)
583585
|| aliasSym.isAllOf(Protected | ParamAccessor, butNot = CaseAccessor) && m.owner.is(Given)
584586
if checking && !infos.refs(alias.symbol) then
585-
warnAt(pos)(UnusedSymbol.implicitParams)
587+
warnAt(pos)(UnusedSymbol.implicitParams(aliasSym))
586588
else
587-
warnAt(pos)(UnusedSymbol.implicitParams)
589+
warnAt(pos)(UnusedSymbol.implicitParams(sym))
588590

589591
def checkLocal(sym: Symbol, pos: SrcPos) =
590592
if ctx.settings.WunusedHas.locals

tests/warn/i15503g.check

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-- [E198] Unused Symbol Warning: tests/warn/i15503g.scala:8:17 ---------------------------------------------------------
2+
8 | private def f2(a: Int) = default_int // warn
3+
| ^
4+
| unused explicit parameter
5+
-- [E198] Unused Symbol Warning: tests/warn/i15503g.scala:9:31 ---------------------------------------------------------
6+
9 | private def f3(a: Int)(using Int) = a // warn
7+
| ^
8+
| unused implicit parameter
9+
-- [E198] Unused Symbol Warning: tests/warn/i15503g.scala:10:17 --------------------------------------------------------
10+
10 | private def f4(a: Int)(using Int) = default_int // warn // warn
11+
| ^
12+
| unused explicit parameter
13+
-- [E198] Unused Symbol Warning: tests/warn/i15503g.scala:10:31 --------------------------------------------------------
14+
10 | private def f4(a: Int)(using Int) = default_int // warn // warn
15+
| ^
16+
| unused implicit parameter
17+
-- [E198] Unused Symbol Warning: tests/warn/i15503g.scala:11:17 --------------------------------------------------------
18+
11 | private def f6(a: Int)(using Int) = summon[Int] // warn
19+
| ^
20+
| unused explicit parameter
21+
-- [E198] Unused Symbol Warning: tests/warn/i15503g.scala:23:18 --------------------------------------------------------
22+
23 | def isAnIssue(y: A): Boolean = x == x // warn
23+
| ^
24+
| unused explicit parameter in extension method isAnIssue
25+
-- [E198] Unused Symbol Warning: tests/warn/i15503g.scala:29:30 --------------------------------------------------------
26+
29 | extension (s: String)(using Show) // warn not used in repeat
27+
| ^
28+
| unused implicit parameter in extension method repeat

tests/warn/i15503g.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//> using options -Wunused:params
1+
//> using options -Wunused:params
22

33
/* This goes around the "trivial method" detection */
44
object Foo {
@@ -15,10 +15,17 @@ object Foo {
1515
private def g2(x: Int) = ??? // OK
1616
}
1717

18-
package foo.test.i17101:
18+
object i17101:
1919
type Test[A] = A
2020
extension[A] (x: Test[A]) { // OK
2121
def value: A = x
2222
def causesIssue: Unit = println("oh no")
2323
def isAnIssue(y: A): Boolean = x == x // warn
2424
}
25+
26+
object i23125:
27+
trait Show:
28+
def show(s: String) = s
29+
extension (s: String)(using Show) // warn not used in repeat
30+
def echo = println(summon[Show].show(s))
31+
def repeat = s * 2

0 commit comments

Comments
 (0)