Skip to content

Commit 6d0b0c9

Browse files
committed
Unused lint ignores args to ctor of enclosing class
1 parent 402be90 commit 6d0b0c9

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
115115
if fun.symbol.is(Module) && defn.isTupleClass(fun.symbol.companionClass) then
116116
args.foreach(_.withAttachment(ForArtifact, ()))
117117
case _ =>
118+
else if !tree.tpe.isInstanceOf[MethodOrPoly] then
119+
val f = funPart(tree)
120+
if f.symbol.isConstructor then
121+
f match
122+
case Select(_: New, nme.CONSTRUCTOR) if ctx.outersIterator.exists(_.owner eq f.symbol.owner) =>
123+
ignoreArgsOfSelfConstruction(tree, f.symbol)
124+
case _ =>
118125
ctx
119126
override def transformApply(tree: Apply)(using Context): tree.type =
120127
// check for multiversal equals
@@ -1011,6 +1018,13 @@ object CheckUnused:
10111018
traverseChildren(tree)
10121019
relaxer.traverse(tree)
10131020

1021+
def ignoreArgsOfSelfConstruction(tree: Apply, ctor: Symbol)(using Context): Unit =
1022+
val pars = ctor.denot.paramSymss.flatten.iterator.filter(_.isTerm)
1023+
val args = allTermArguments(tree)
1024+
for (par, arg) <- pars.zip(args) do
1025+
if arg.symbol.is(ParamAccessor) && arg.symbol.name == par.name && arg.symbol.owner == ctor.owner then
1026+
arg.putAttachment(Ignore, ())
1027+
10141028
extension (nm: Name)
10151029
inline def exists(p: Name => Boolean): Boolean = nm.ne(nme.NO_NAME) && p(nm)
10161030
inline def isWildcard: Boolean = nm == nme.WILDCARD || nm.is(WildcardParamName)

tests/warn/i24698.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//> using options -Wunused:all
2+
3+
trait TC[X] {
4+
def notTrivial: Int
5+
}
6+
7+
class C[T: TC]() { // warn implicit TC used only for new instance of owner
8+
def mod: C[T] = new C
9+
}
10+
11+
class D(val i: Int):
12+
def this(s: String) = this(s.toInt) // not new
13+
14+
class E[T](tc: TC[T]): // warn
15+
def mod: E[T] = new E(tc)
16+
17+
class F[T: TC](i: Int): // warn // warn
18+
def mod: F[T] = new F(i)
19+
20+
class G(i: Int): // warn
21+
class Inner(i: Int):
22+
def g = new G(i)

0 commit comments

Comments
 (0)