Skip to content

Commit f0da45e

Browse files
authored
Merge pull request #55 from MasseGuillaume/mut-map-updated
Rewrite mutable.Map.updated
2 parents 5fe1aaa + 5617806 commit f0da45e

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ class MutSetMapSrc(map: mutable.Map[Int, Int], set: mutable.Set[Int]) {
99
set + 2
1010
map + (2 -> 3)
1111
(set + 2).size
12+
map.updated(1, 3)
1213
}

scalafix/output/src/main/scala/fix/MutSetMapSrc.scala

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ class MutSetMapSrc(map: mutable.Map[Int, Int], set: mutable.Set[Int]) {
99
set.clone() += 2
1010
map.clone() += (2 -> 3)
1111
(set.clone() += 2).size
12+
map.clone() += ((1, 3))
1213
}

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
4141
val mutMapPlus = SymbolMatcher.exact(
4242
Symbol("_root_.scala.collection.mutable.MapLike#`+`(Lscala/Tuple2;)Lscala/collection/mutable/Map;.")
4343
)
44+
val mutMapUpdate =
45+
SymbolMatcher.exact(
46+
Symbol("_root_.scala.collection.mutable.MapLike#updated(Ljava/lang/Object;Ljava/lang/Object;)Lscala/collection/mutable/Map;.")
47+
)
4448

4549
def foldSymbol(isLeft: Boolean): SymbolMatcher = {
4650
val op =
@@ -212,6 +216,16 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
212216
}.asPatch
213217
}
214218

219+
def replaceMutMapUpdated(ctx: RuleCtx): Patch = {
220+
ctx.tree.collect {
221+
case Term.Apply(Term.Select(a, up @ mutMapUpdate(_)), List(k, v)) => {
222+
ctx.addRight(up, "clone() += (") +
223+
ctx.removeTokens(up.tokens) +
224+
ctx.addRight(v, ")")
225+
}
226+
}.asPatch
227+
}
228+
215229
override def fix(ctx: RuleCtx): Patch = {
216230
// println(ctx.index.database)
217231

@@ -224,6 +238,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
224238
replaceMutableSet(ctx) +
225239
replaceSymbolicFold(ctx) +
226240
replaceSetMapPlus2(ctx) +
227-
replaceMutSetMapPlus(ctx)
241+
replaceMutSetMapPlus(ctx) +
242+
replaceMutMapUpdated(ctx)
228243
}
229244
}

0 commit comments

Comments
 (0)