Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ class CheckUnused private (phaseMode: PhaseMode, suffix: String) extends MiniPha
if fun.symbol.is(Module) && defn.isTupleClass(fun.symbol.companionClass) then
args.foreach(_.withAttachment(ForArtifact, ()))
case _ =>
else if !tree.tpe.isInstanceOf[MethodOrPoly] then
val f = funPart(tree)
if f.symbol.isConstructor then
f match
case Select(_: New, nme.CONSTRUCTOR) if ctx.outersIterator.exists(_.owner eq f.symbol.owner) =>
ignoreArgsOfSelfConstruction(tree, f.symbol)
case _ =>
ctx
override def transformApply(tree: Apply)(using Context): tree.type =
// check for multiversal equals
Expand Down Expand Up @@ -1011,6 +1018,13 @@ object CheckUnused:
traverseChildren(tree)
relaxer.traverse(tree)

def ignoreArgsOfSelfConstruction(tree: Apply, ctor: Symbol)(using Context): Unit =
val pars = ctor.denot.paramSymss.flatten.iterator.filter(_.isTerm)
val args = allTermArguments(tree)
for (par, arg) <- pars.zip(args) do
if arg.symbol.is(ParamAccessor) && arg.symbol.name == par.name && arg.symbol.owner == ctor.owner then
arg.putAttachment(Ignore, ())

extension (nm: Name)
inline def exists(p: Name => Boolean): Boolean = nm.ne(nme.NO_NAME) && p(nm)
inline def isWildcard: Boolean = nm == nme.WILDCARD || nm.is(WildcardParamName)
Expand Down
22 changes: 22 additions & 0 deletions tests/warn/i24698.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//> using options -Wunused:all

trait TC[X] {
def notTrivial: Int
}

class C[T: TC]() { // warn implicit TC used only for new instance of owner
def mod: C[T] = new C
}

class D(val i: Int):
def this(s: String) = this(s.toInt) // not new

class E[T](tc: TC[T]): // warn
def mod: E[T] = new E(tc)

class F[T: TC](i: Int): // warn // warn
def mod: F[T] = new F(i)

class G(i: Int): // warn
class Inner(i: Int):
def g = new G(i)
Loading