Skip to content

Commit 84bac1b

Browse files
Rewrite Map.mapValues -> Map.mapValues.toMap
1 parent 408c54d commit 84bac1b

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ class SetMapSrc(set: Set[Int], map: Map[Int, Int]) {
88
map + (2 -> 3, 3 -> 4)
99
(set + (2, 3)).map(x => x)
1010
set + (2, 3) - 4
11+
map.mapValues(_ + 1)
1112
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ class SetMapSrc(set: Set[Int], map: Map[Int, Int]) {
88
map + (2 -> 3) + (3 -> 4)
99
(set + 2 + 3).map(x => x)
1010
set + 2 + 3 - 4
11+
map.mapValues(_ + 1).toMap
1112
}

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
6767
Symbol("_root_.scala.collection.mutable.MapLike.retain.")
6868
)
6969

70+
val mapMapValues =
71+
SymbolMatcher.exact(
72+
Symbol("_root_.scala.collection.immutable.MapLike#mapValues(Lscala/Function1;)Lscala/collection/immutable/Map;.")
73+
)
74+
7075
val retainSet =
7176
SymbolMatcher.normalized(
7277
Symbol("_root_.scala.collection.mutable.SetLike.retain.")
@@ -232,12 +237,20 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
232237
}.asPatch
233238
}
234239

240+
235241
def replaceIterableSameElements(ctx: RuleCtx): Patch = {
236242
ctx.tree.collect {
237243
case Term.Apply(Term.Select(lhs, iterableSameElement(_)), List(_)) =>
238244
ctx.addRight(lhs, ".iterator")
239245
}.asPatch
240246
}
247+
248+
def replaceMapMapValues(ctx: RuleCtx): Patch = {
249+
ctx.tree.collect {
250+
case ap @ Term.Apply(Term.Select(_, mapMapValues(_)), List(_)) =>
251+
ctx.addRight(ap, ".toMap")
252+
}.asPatch
253+
}
241254

242255

243256
override def fix(ctx: RuleCtx): Patch = {
@@ -254,6 +267,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
254267
replaceSetMapPlus2(ctx) +
255268
replaceMutSetMapPlus(ctx) +
256269
replaceMutMapUpdated(ctx) +
257-
replaceIterableSameElements(ctx)
270+
replaceIterableSameElements(ctx) +
271+
replaceMapMapValues(ctx)
258272
}
259273
}

0 commit comments

Comments
 (0)