Skip to content

Commit ae843d4

Browse files
Rewrite collection.Set.+ restrict type of lhs
scalafix allow us to match on SetLike.+ but we cannot check the type of the expression on the lhs We can work arround this limitation if the lhs is a identifier (simple case). This allow us to document the expected type on the lhs and prepare the implementation once scalameta/scalameta#1212 is resolved
1 parent 62d982c commit ae843d4

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class SetMapSrc(iset: immutable.Set[Int],
1515
imap + (2 -> 3, 3 -> 4)
1616
(iset + (2, 3)).toString
1717
iset + (2, 3) - 4
18-
iset + 1 - 2
19-
cset + 1 - 2
18+
iset + 1
19+
iset - 2
20+
cset + 1
21+
cset - 2
2022
cmap + (2 -> 3) + ((4, 5))
2123
}

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ class SetMapSrc(iset: immutable.Set[Int],
1515
imap + (2 -> 3) + (3 -> 4)
1616
(iset + 2 + 3).toString
1717
iset + 2 + 3 - 4
18-
iset + 1 - 2
19-
cset ++ _root_.scala.collection.Set(1) -- _root_.scala.collection.Set(2)
18+
iset + 1
19+
iset - 2
20+
cset ++ _root_.scala.collection.Set(1)
21+
cset -- _root_.scala.collection.Set(2)
2022
cmap ++ _root_.scala.collection.Map(2 -> 3) ++ _root_.scala.collection.Map((4, 5))
2123
}

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,15 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
222222
}.asPatch
223223
}
224224

225+
// terms dont give us terms https://github.com/scalameta/scalameta/issues/1212
226+
// if we have a simple identifier, we can look at his definition at query it's type
227+
// this should be improved in future version of scalameta
228+
def isTpe(symbol: Symbol, tree: Tree, ctx: RuleCtx): Boolean =
229+
ctx.index.denotation(tree).map(_.names.headOption.exists(_.symbol == symbol)).getOrElse(false)
230+
231+
def isCollectionSet(tree: Tree, ctx: RuleCtx): Boolean =
232+
isTpe(Symbol("_root_.scala.collection.Set#"), tree, ctx)
233+
225234
def replaceSetMapPlusMinus(ctx: RuleCtx): Patch = {
226235
def rewriteOp(op: Tree, rhs: Tree, doubleOp: String, col0: String): Patch = {
227236
val col = "_root_.scala.collection." + col0
@@ -238,10 +247,10 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
238247
}
239248

240249
ctx.tree.collect {
241-
case Term.ApplyInfix(_, op @ setPlus(_), Nil, List(rhs)) =>
250+
case Term.ApplyInfix(lhs, op @ setPlus(_), Nil, List(rhs)) if isCollectionSet(lhs, ctx) =>
242251
rewriteOp(op, rhs, "+", "Set")
243252

244-
case Term.ApplyInfix(lhs, op @ setMinus(_), Nil, List(rhs)) =>
253+
case Term.ApplyInfix(lhs, op @ setMinus(_), Nil, List(rhs)) if isCollectionSet(lhs, ctx) =>
245254
rewriteOp(op, rhs, "-", "Set")
246255

247256
case Term.ApplyInfix(lhs, op @ mapPlus(_), Nil, List(rhs)) =>

0 commit comments

Comments
 (0)