Skip to content

Rewrite mutable.Set/Map no longer have a + operation #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions scalafix/input/src/main/scala/fix/MutSetMapSrc.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
rule = "scala:fix.Scalacollectioncompat_newcollections"
*/
package fix

import scala.collection.mutable

class MutSetMapSrc(map: mutable.Map[Int, Int], set: mutable.Set[Int]) {
set + 2
map + (2 -> 3)
(set + 2).size
}
12 changes: 12 additions & 0 deletions scalafix/output/src/main/scala/fix/MutSetMapSrc.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@



package fix

import scala.collection.mutable

class MutSetMapSrc(map: mutable.Map[Int, Int], set: mutable.Set[Int]) {
set.clone() += 2
map.clone() += (2 -> 3)
(set.clone() += 2).size
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
val mapPlus2 = SymbolMatcher.exact(
Symbol("_root_.scala.collection.immutable.MapLike#`+`(Lscala/Tuple2;Lscala/Tuple2;Lscala/collection/Seq;)Lscala/collection/immutable/Map;.")
)
val mutSetPlus = SymbolMatcher.exact(
Symbol("_root_.scala.collection.mutable.SetLike#`+`(Ljava/lang/Object;)Lscala/collection/mutable/Set;.")
)
val mutMapPlus = SymbolMatcher.exact(
Symbol("_root_.scala.collection.mutable.MapLike#`+`(Lscala/Tuple2;)Lscala/collection/mutable/Map;.")
)

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

def replaceSetMapPlus2(ctx: RuleCtx): Patch = {
def rewritePlus(ap: Term.ApplyInfix, lhs: Term, op: Term.Name, rhs1: Term, rhs2: Term): Patch = {

val tokensToReplace =
if(ap.tokens.headOption.map(_.is[Token.LeftParen]).getOrElse(false)) {
// don't drop surrounding parens
Expand All @@ -183,7 +188,6 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
ctx.removeTokens(tokensToReplace) +
tokensToReplace.headOption.map(x => ctx.addRight(x, newTree))
}

ctx.tree.collect {
case ap @ Term.ApplyInfix(lhs, op @ mapPlus2(_), _, List(a, b)) =>
rewritePlus(ap, lhs, op, a, b)
Expand All @@ -193,6 +197,21 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
}.asPatch
}

def replaceMutSetMapPlus(ctx: RuleCtx): Patch = {
def rewriteMutPlus(lhs: Term, op: Term.Name): Patch = {
ctx.addRight(lhs, ".clone()") +
ctx.addRight(op, "=")
}

ctx.tree.collect {
case Term.ApplyInfix(lhs, op @ mutSetPlus(_), _, List(_)) =>
rewriteMutPlus(lhs, op)

case Term.ApplyInfix(lhs, op @ mutMapPlus(_), _, List(_)) =>
rewriteMutPlus(lhs, op)
}.asPatch
}

override def fix(ctx: RuleCtx): Patch = {
// println(ctx.index.database)

Expand All @@ -204,6 +223,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
replaceMutableMap(ctx) +
replaceMutableSet(ctx) +
replaceSymbolicFold(ctx) +
replaceSetMapPlus2(ctx)
replaceSetMapPlus2(ctx) +
replaceMutSetMapPlus(ctx)
}
}