Skip to content

Commit 58a01af

Browse files
authored
Merge pull request #59 from MasseGuillaume/same-elements
Rewrite Iterable.sameElements -> Iterable.iterator.sameElements
2 parents f0da45e + ea2fa66 commit 58a01af

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
rule = "scala:fix.Scalacollectioncompat_newcollections"
3+
*/
4+
package fix
5+
6+
class IterableSrc(it: Iterable[Int]) {
7+
it.sameElements(it)
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
3+
4+
package fix
5+
6+
class IterableSrc(it: Iterable[Int]) {
7+
it.iterator.sameElements(it)
8+
}

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
4646
Symbol("_root_.scala.collection.mutable.MapLike#updated(Ljava/lang/Object;Ljava/lang/Object;)Lscala/collection/mutable/Map;.")
4747
)
4848

49+
val iterableSameElement =
50+
SymbolMatcher.exact(
51+
Symbol("_root_.scala.collection.IterableLike#sameElements(Lscala/collection/GenIterable;)Z.")
52+
)
53+
4954
def foldSymbol(isLeft: Boolean): SymbolMatcher = {
5055
val op =
5156
if (isLeft) "/:"
@@ -226,6 +231,14 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
226231
}.asPatch
227232
}
228233

234+
def replaceIterableSameElements(ctx: RuleCtx): Patch = {
235+
ctx.tree.collect {
236+
case Term.Apply(Term.Select(lhs, iterableSameElement(_)), List(_)) =>
237+
ctx.addRight(lhs, ".iterator")
238+
}.asPatch
239+
}
240+
241+
229242
override def fix(ctx: RuleCtx): Patch = {
230243
// println(ctx.index.database)
231244

@@ -239,6 +252,7 @@ case class Scalacollectioncompat_newcollections(index: SemanticdbIndex)
239252
replaceSymbolicFold(ctx) +
240253
replaceSetMapPlus2(ctx) +
241254
replaceMutSetMapPlus(ctx) +
242-
replaceMutMapUpdated(ctx)
255+
replaceMutMapUpdated(ctx) +
256+
replaceIterableSameElements(ctx)
243257
}
244258
}

0 commit comments

Comments
 (0)