Skip to content

Commit 0ff4441

Browse files
authored
Merge pull request #1978 from bjaglin/removeuunsed-block-rhs
RemoveUnused: guard Term.Block RHS in unused Defn
2 parents 76ec60b + 68fcbbc commit 0ff4441

File tree

5 files changed

+109
-10
lines changed

5 files changed

+109
-10
lines changed

scalafix-rules/src/main/scala/scalafix/internal/rule/RemoveUnused.scala

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,15 @@ class RemoveUnused(config: RemoveUnusedConfig)
156156
Patch.removeImportee(i).atomic
157157
}.asPatch
158158
case i: Defn if isUnusedTerm(i.pos) =>
159-
defnTokensToRemove(i).map(Patch.removeTokens).asPatch.atomic
159+
defnPatch(i).asPatch.atomic
160160
case i @ Defn.Def(_, name, _, _, _, _) if isUnusedTerm(name.pos) =>
161-
defnTokensToRemove(i).map(Patch.removeTokens).asPatch.atomic
161+
defnPatch(i).asPatch.atomic
162162
case i @ Defn.Val(_, List(pat), _, _)
163163
if isUnusedTerm.exists(p => p.start == pat.pos.start) =>
164-
defnTokensToRemove(i).map(Patch.removeTokens).asPatch.atomic
164+
defnPatch(i).asPatch.atomic
165165
case i @ Defn.Var(_, List(pat), _, _)
166166
if isUnusedTerm.exists(p => p.start == pat.pos.start) =>
167-
defnTokensToRemove(i).map(Patch.removeTokens).asPatch.atomic
167+
defnPatch(i).asPatch.atomic
168168
case Term.Match(_, cases) =>
169169
patchPatVarsIn(cases)
170170
case Term.PartialFunction(cases) =>
@@ -197,11 +197,8 @@ class RemoveUnused(config: RemoveUnusedConfig)
197197
}
198198

199199
// Given ("val x = 2", "2"), returns "val x = ".
200-
private def leftTokens(t: Tree, right: Tree): Tokens = {
201-
val startT = t.tokens.start
202-
val startRight = right.tokens.start
203-
t.tokens.take(startRight - startT)
204-
}
200+
private def leftTokens(t: Tree, right: Tree): Tokens =
201+
t.tokens.dropRightWhile(_.start >= right.pos.start)
205202

206203
private def defnTokensToRemove(defn: Defn): Option[Tokens] = defn match {
207204
case i @ Defn.Val(_, _, _, Lit(_)) => Some(i.tokens)
@@ -212,6 +209,35 @@ class RemoveUnused(config: RemoveUnusedConfig)
212209
case _ => None
213210
}
214211

212+
private def defnPatch(defn: Defn): Option[Patch] =
213+
defnTokensToRemove(defn).map { tokens =>
214+
val maybeRHSBlock = defn match {
215+
case Defn.Val(_, _, _, x @ Term.Block(_)) => Some(x)
216+
case Defn.Var(_, _, _, Some(x @ Term.Block(_))) => Some(x)
217+
case _ => None
218+
}
219+
220+
val maybeLocally = maybeRHSBlock.map { block =>
221+
if (Some(block.pos.start) == block.stats.headOption.map(_.pos.start))
222+
// only significant indentation blocks have their first stat
223+
// aligned with the block itself (otherwise there is a heading "{")
224+
"locally:"
225+
else "locally"
226+
}
227+
228+
maybeLocally match {
229+
case Some(locally) =>
230+
// Preserve comments between the LHS and the RHS, as well as
231+
// newlines & whitespaces for significant indentation
232+
val tokensNoTrailingTrivia = tokens.dropRightWhile(_.is[Trivia])
233+
234+
Patch.removeTokens(tokensNoTrailingTrivia) +
235+
tokensNoTrailingTrivia.lastOption.map(Patch.addRight(_, locally))
236+
case _ =>
237+
Patch.removeTokens(tokens)
238+
}
239+
}
240+
215241
private def posExclParens(tree: Tree): Position = {
216242
val leftTokenCount =
217243
tree.tokens.takeWhile(tk => tk.is[Token.LeftParen] || tk.is[Trivia]).size
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
rules = RemoveUnused
3+
*/
4+
package test.removeUnused
5+
6+
object RemoveUnusedTermsSignificantIndentation:
7+
8+
private val a = // lost
9+
println(5)
10+
11+
private var b1 =
12+
println("foo")
13+
1
14+
15+
private val b2 =
16+
{ println("foo") }
17+
{ 1 }
18+
19+
private
20+
var
21+
b3:
22+
Integer
23+
= // preserved
24+
// preserved
25+
println("foo")
26+
1

scalafix-tests/input/src/main/scala/test/removeUnused/RemoveUnusedTerms.scala

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ object RemoveUnusedTerms {
77

88
def foo = {
99
val a = "unused"
10-
val aa = println(5)
10+
val aa = // lost
11+
println(5)
1112
var b = 0
1213
var bb = println(0)
1314
println(1)
@@ -19,4 +20,20 @@ object RemoveUnusedTerms {
1920
val dd = 0
2021
def f(x: Int) = "unused"
2122
private def ff(x: Int) = "unused"
23+
24+
private val b1: Integer = { println("foo"); 1 }
25+
private var b2: Integer = /* preserved */ {
26+
println("foo")
27+
1
28+
}
29+
private
30+
var
31+
b3:
32+
Integer
33+
= /* preserved */
34+
// preserved
35+
{
36+
println("foo")
37+
1
38+
}
2239
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package test.removeUnused
2+
3+
object RemoveUnusedTermsSignificantIndentation:
4+
5+
println(5)
6+
7+
locally:
8+
println("foo")
9+
1
10+
11+
locally:
12+
{ println("foo") }
13+
{ 1 }
14+
15+
locally: // preserved
16+
// preserved
17+
println("foo")
18+
1

scalafix-tests/output/src/main/scala/test/removeUnused/RemoveUnusedTerms.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,16 @@ object RemoveUnusedTerms {
1616
val dd = 0
1717
def f(x: Int) = "unused"
1818

19+
20+
locally { println("foo"); 1 }
21+
locally /* preserved */ {
22+
println("foo")
23+
1
24+
}
25+
locally /* preserved */
26+
// preserved
27+
{
28+
println("foo")
29+
1
30+
}
1931
}

0 commit comments

Comments
 (0)