Skip to content

Commit a496917

Browse files
authored
Merge pull request #36 from MasseGuillaume/retain-set
Rewrite `retain` to `filterInPlace` for mutable.Set
2 parents 431d2fa + 53abcf3 commit a496917

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

scalafix/input/src/main/scala/fix/RetainSrc.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ rule = "scala:fix.Scalacollectioncompat_NewCollections"
33
*/
44
package fix
55

6-
import scala.collection.mutable.Map
6+
import scala.collection.mutable.{Map, Set}
77

8-
class MethodRenames(xs: Map[Int, Int]) {
8+
class MethodRenames(xs: Map[Int, Int], ys: Set[Int]) {
99
xs.retain((_, _) => true)
1010
xs.retain{case (x, y) => true}
11+
ys.retain(_ => true)
1112
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package fix
22

3-
import scala.collection.mutable.Map
3+
import scala.collection.mutable.{Map, Set}
44

5-
class MethodRenames(xs: Map[Int, Int]) {
5+
class MethodRenames(xs: Map[Int, Int], ys: Set[Int]) {
66
xs.filterInPlace{case (_, _) => true}
77
xs.filterInPlace{case (x, y) => true}
8+
ys.filterInPlace(_ => true)
89
}

scalafix/rules/src/main/scala/fix/Scalacollectioncompat_NewCollections.scala

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,28 @@ case class Scalacollectioncompat_NewCollections(index: SemanticdbIndex)
3030
Symbol("_root_.scala.runtime.Tuple3Zipped.Ops.zipped.")
3131
)
3232

33-
val retain =
33+
val retainMap =
3434
SymbolMatcher.normalized(
3535
Symbol("_root_.scala.collection.mutable.MapLike.retain.")
3636
)
3737

38+
val retainSet =
39+
SymbolMatcher.normalized(
40+
Symbol("_root_.scala.collection.mutable.SetLike.retain.")
41+
)
42+
43+
def replaceMutableSet(ctx: RuleCtx) =
44+
ctx.tree.collect {
45+
case retainSet(n: Name) =>
46+
ctx.replaceTree(n, "filterInPlace")
47+
}.asPatch
48+
3849
def replaceMutableMap(ctx: RuleCtx) =
3950
ctx.tree.collect {
40-
case Term.Apply(Term.Select(_, retain(n: Name)), List(_: Term.PartialFunction)) =>
51+
case Term.Apply(Term.Select(_, retainMap(n: Name)), List(_: Term.PartialFunction)) =>
4152
ctx.replaceTree(n, "filterInPlace")
4253

43-
case Term.Apply(Term.Select(_, retain(n: Name)), List(_: Term.Function)) =>
54+
case Term.Apply(Term.Select(_, retainMap(n: Name)), List(_: Term.Function)) =>
4455
(for {
4556
name <- n.tokens.lastOption
4657
open <- ctx.tokenList.find(name)(t => t.is[Token.LeftParen])
@@ -128,11 +139,14 @@ case class Scalacollectioncompat_NewCollections(index: SemanticdbIndex)
128139
}.asPatch
129140

130141
override def fix(ctx: RuleCtx): Patch = {
142+
// println(ctx.index.database)
143+
131144
replaceToList(ctx) +
132145
replaceSymbols(ctx) +
133146
replaceTupleZipped(ctx) +
134147
replaceCopyToBuffer(ctx) +
135148
replaceStreamAppend(ctx) +
136-
replaceMutableMap(ctx)
149+
replaceMutableMap(ctx) +
150+
replaceMutableSet(ctx)
137151
}
138152
}

0 commit comments

Comments
 (0)