Skip to content

Commit a39298e

Browse files
Rewrite mutable.Set/Map no longer have a + operation
1 parent 3688ecf commit a39298e

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
rule = "scala:fix.Scalacollectioncompat_newcollections"
3+
*/
4+
package fix
5+
6+
import scala.collection.mutable
7+
8+
class MutSetMapSrc(map: mutable.Map[Int, Int], set: mutable.Set[Int]) {
9+
set + 2
10+
map + (2 -> 3)
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
3+
4+
package fix
5+
6+
import scala.collection.mutable
7+
8+
class MutSetMapSrc(map: mutable.Map[Int, Int], set: mutable.Set[Int]) {
9+
set.clone() += 2
10+
map.clone() += (2 -> 3)
11+
}

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

+23-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
2929
Symbol("_root_.scala.runtime.Tuple2Zipped.Ops.zipped."),
3030
Symbol("_root_.scala.runtime.Tuple3Zipped.Ops.zipped.")
3131
)
32+
val mutSetPlus = SymbolMatcher.exact(
33+
Symbol("_root_.scala.collection.mutable.SetLike#`+`(Ljava/lang/Object;)Lscala/collection/mutable/Set;.")
34+
)
35+
val mutMapPlus = SymbolMatcher.exact(
36+
Symbol("_root_.scala.collection.mutable.MapLike#`+`(Lscala/Tuple2;)Lscala/collection/mutable/Map;.")
37+
)
3238
def foldSymbol(isLeft: Boolean): SymbolMatcher = {
3339
val op =
3440
if (isLeft) "/:"
@@ -156,6 +162,21 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
156162
ctx.replaceTree(t, "lazyAppendedAll")
157163
}.asPatch
158164

165+
def replaceMutSetMapPlus(ctx: RuleCtx): Patch = {
166+
def rewriteMutPlus(lhs: Term, op: Term.Name): Patch = {
167+
ctx.addRight(lhs, ".clone()") +
168+
ctx.addRight(op, "=")
169+
}
170+
171+
ctx.tree.collect {
172+
case Term.ApplyInfix(lhs, op @ mutSetPlus(_), _, List(_)) =>
173+
rewriteMutPlus(lhs, op)
174+
175+
case Term.ApplyInfix(lhs, op @ mutMapPlus(_), _, List(_)) =>
176+
rewriteMutPlus(lhs, op)
177+
}.asPatch
178+
}
179+
159180
override def fix(ctx: RuleCtx): Patch = {
160181
// println(ctx.index.database)
161182

@@ -166,6 +187,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
166187
replaceStreamAppend(ctx) +
167188
replaceMutableMap(ctx) +
168189
replaceMutableSet(ctx) +
169-
replaceSymbolicFold(ctx)
190+
replaceSymbolicFold(ctx) +
191+
replaceMutSetMapPlus(ctx)
170192
}
171193
}

0 commit comments

Comments
 (0)