Skip to content

Commit 5fe1aaa

Browse files
authored
Merge pull request #54 from MasseGuillaume/set-map-mut-plus
Rewrite `mutable.Set/Map` no longer have a `+` operation
2 parents 0ebd48d + 3eea437 commit 5fe1aaa

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
(set + 2).size
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
(set.clone() += 2).size
12+
}

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

+23-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
3535
val mapPlus2 = SymbolMatcher.exact(
3636
Symbol("_root_.scala.collection.immutable.MapLike#`+`(Lscala/Tuple2;Lscala/Tuple2;Lscala/collection/Seq;)Lscala/collection/immutable/Map;.")
3737
)
38+
val mutSetPlus = SymbolMatcher.exact(
39+
Symbol("_root_.scala.collection.mutable.SetLike#`+`(Ljava/lang/Object;)Lscala/collection/mutable/Set;.")
40+
)
41+
val mutMapPlus = SymbolMatcher.exact(
42+
Symbol("_root_.scala.collection.mutable.MapLike#`+`(Lscala/Tuple2;)Lscala/collection/mutable/Map;.")
43+
)
3844

3945
def foldSymbol(isLeft: Boolean): SymbolMatcher = {
4046
val op =
@@ -165,7 +171,6 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
165171

166172
def replaceSetMapPlus2(ctx: RuleCtx): Patch = {
167173
def rewritePlus(ap: Term.ApplyInfix, lhs: Term, op: Term.Name, rhs1: Term, rhs2: Term): Patch = {
168-
169174
val tokensToReplace =
170175
if(ap.tokens.headOption.map(_.is[Token.LeftParen]).getOrElse(false)) {
171176
// don't drop surrounding parens
@@ -183,7 +188,6 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
183188
ctx.removeTokens(tokensToReplace) +
184189
tokensToReplace.headOption.map(x => ctx.addRight(x, newTree))
185190
}
186-
187191
ctx.tree.collect {
188192
case ap @ Term.ApplyInfix(lhs, op @ mapPlus2(_), _, List(a, b)) =>
189193
rewritePlus(ap, lhs, op, a, b)
@@ -193,6 +197,21 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
193197
}.asPatch
194198
}
195199

200+
def replaceMutSetMapPlus(ctx: RuleCtx): Patch = {
201+
def rewriteMutPlus(lhs: Term, op: Term.Name): Patch = {
202+
ctx.addRight(lhs, ".clone()") +
203+
ctx.addRight(op, "=")
204+
}
205+
206+
ctx.tree.collect {
207+
case Term.ApplyInfix(lhs, op @ mutSetPlus(_), _, List(_)) =>
208+
rewriteMutPlus(lhs, op)
209+
210+
case Term.ApplyInfix(lhs, op @ mutMapPlus(_), _, List(_)) =>
211+
rewriteMutPlus(lhs, op)
212+
}.asPatch
213+
}
214+
196215
override def fix(ctx: RuleCtx): Patch = {
197216
// println(ctx.index.database)
198217

@@ -204,6 +223,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
204223
replaceMutableMap(ctx) +
205224
replaceMutableSet(ctx) +
206225
replaceSymbolicFold(ctx) +
207-
replaceSetMapPlus2(ctx)
226+
replaceSetMapPlus2(ctx) +
227+
replaceMutSetMapPlus(ctx)
208228
}
209229
}

0 commit comments

Comments
 (0)