Skip to content

Commit 78fd1fb

Browse files
Rewrite Map.mapValues -> Map.mapValues.toMap
1 parent 58a01af commit 78fd1fb

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
@@ -66,6 +66,11 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
6666
Symbol("_root_.scala.collection.mutable.MapLike.retain.")
6767
)
6868

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

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

241254

242255
override def fix(ctx: RuleCtx): Patch = {
@@ -253,6 +266,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
253266
replaceSetMapPlus2(ctx) +
254267
replaceMutSetMapPlus(ctx) +
255268
replaceMutMapUpdated(ctx) +
256-
replaceIterableSameElements(ctx)
269+
replaceIterableSameElements(ctx) +
270+
replaceMapMapValues(ctx)
257271
}
258272
}

0 commit comments

Comments
 (0)