Skip to content

Commit

Permalink
RemoveUnused: support Term.Block RHS in unused Defn
Browse files Browse the repository at this point in the history
  • Loading branch information
bjaglin committed Apr 20, 2024
1 parent 982a7dd commit eacafa2
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ class RemoveUnused(config: RemoveUnusedConfig)
Patch.removeImportee(i).atomic
}.asPatch
case i: Defn if isUnusedTerm(i.pos) =>
defnTokensToRemove(i).map(Patch.removeTokens).asPatch.atomic
defnPatch(i).atomic
case i @ Defn.Def(_, name, _, _, _, _) if isUnusedTerm(name.pos) =>
defnTokensToRemove(i).map(Patch.removeTokens).asPatch.atomic
defnPatch(i).atomic
case i @ Defn.Val(_, List(pat), _, _)
if isUnusedTerm.exists(p => p.start == pat.pos.start) =>
defnTokensToRemove(i).map(Patch.removeTokens).asPatch.atomic
defnPatch(i).atomic
case i @ Defn.Var(_, List(pat), _, _)
if isUnusedTerm.exists(p => p.start == pat.pos.start) =>
defnTokensToRemove(i).map(Patch.removeTokens).asPatch.atomic
defnPatch(i).atomic
case Term.Match(_, cases) =>
patchPatVarsIn(cases)
case Term.PartialFunction(cases) =>
Expand Down Expand Up @@ -200,13 +200,46 @@ class RemoveUnused(config: RemoveUnusedConfig)
private def leftTokens(t: Tree, right: Tree): Tokens =
t.tokens.dropRightWhile(_.start >= right.pos.start)

private def defnTokensToRemove(defn: Defn): Option[Tokens] = defn match {
case i @ Defn.Val(_, _, _, Lit(_)) => Some(i.tokens)
case i @ Defn.Val(_, _, _, rhs) => Some(leftTokens(i, rhs))
case i @ Defn.Var(_, _, _, Some(Lit(_))) => Some(i.tokens)
case i @ Defn.Var(_, _, _, rhs) => rhs.map(leftTokens(i, _))
case i: Defn.Def => Some(i.tokens)
case _ => None
private def defnPatch(defn: Defn): Patch = {
val maybeTokensToRemove = defn match {
case i @ Defn.Val(_, _, _, Lit(_)) => Some(i.tokens)
case i @ Defn.Val(_, _, _, rhs) => Some(leftTokens(i, rhs))
case i @ Defn.Var(_, _, _, Some(Lit(_))) => Some(i.tokens)
case i @ Defn.Var(_, _, _, rhs) => rhs.map(leftTokens(i, _))
case i: Defn.Def => Some(i.tokens)
case _ => None
}

val maybePatch = maybeTokensToRemove.map { tokens =>
val maybeRHSBlock = defn match {
case Defn.Val(_, _, _, x @ Term.Block(_)) => Some(x)
case Defn.Var(_, _, _, Some(x @ Term.Block(_))) => Some(x)
case _ => None
}

val maybeLocally = maybeRHSBlock.map { block =>
if (Some(block.pos.start) == block.stats.headOption.map(_.pos.start))
// only significant indentation blocks have their first stat
// aligned with the block itself (otherwise there is a heading "{")
"locally:"
else "locally"
}

maybeLocally match {
case Some(locally) =>
// Preserve comments between the LHS and the RHS, as well as
// newlines for significant indentation
val tokensNoTrailingTrivia =
tokens.dropRightWhile(_.is[Trivia])

Patch.removeTokens(tokensNoTrailingTrivia) +
tokensNoTrailingTrivia.lastOption.map(Patch.addRight(_, locally))
case _ =>
Patch.removeTokens(tokens)
}
}

maybePatch.getOrElse(Patch.empty)
}

private def posExclParens(tree: Tree): Position = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
rules = RemoveUnused
*/
package test.removeUnused

object RemoveUnusedTermsSignificantIndentation:

private val a =
println(5)

private var b1 =
println("foo")
1

private val b2 =
{ println("foo") }
{ 1 }

private
var
b3:
Integer
= // preserved
// preserved
println("foo")
1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ object RemoveUnusedTerms {

def foo = {
val a = "unused"
val aa = println(5)
val aa = // lost
println(5)
var b = 0
var bb = println(0)
println(1)
Expand All @@ -19,4 +20,20 @@ object RemoveUnusedTerms {
val dd = 0
def f(x: Int) = "unused"
private def ff(x: Int) = "unused"

private val b1: Integer = { println("foo"); 1 }
private var b2: Integer = /* preserved */ {
println("foo")
1
}
private
var
b3:
Integer
= /* preserved */
// preserved
{
println("foo")
1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package test.removeUnused

object RemoveUnusedTermsSignificantIndentation:

println(5)

locally:
println("foo")
1

locally:
{ println("foo") }
{ 1 }

locally: // preserved
// preserved
println("foo")
1
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,16 @@ object RemoveUnusedTerms {
val dd = 0
def f(x: Int) = "unused"


locally { println("foo"); 1 }
locally /* preserved */ {
println("foo")
1
}
locally /* preserved */
// preserved
{
println("foo")
1
}
}

0 comments on commit eacafa2

Please sign in to comment.