Skip to content

Commit ee1bdba

Browse files
committed
Add copyToBuffer rule
1 parent 589712c commit ee1bdba

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
rule = "scala:fix.Collectionstrawman_v0"
3+
*/
4+
package fix
5+
6+
import scala.collection.mutable
7+
8+
class Collectionstrawman_v0_copyToBuffer(xs: List[Int], b: mutable.Buffer[Int]) {
9+
10+
xs.copyToBuffer(b)
11+
(xs ++ xs).copyToBuffer(b)
12+
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package fix
2+
3+
import scala.collection.mutable
4+
5+
class Collectionstrawman_v0_copyToBuffer(xs: List[Int], b: mutable.Buffer[Int]) {
6+
7+
b ++= xs
8+
b ++= xs ++ xs
9+
10+
}

2.13/rules/src/main/scala/fix/Collectionstrawman_v0.scala

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,20 @@ case class Collectionstrawman_v0(index: SemanticdbIndex)
8686
removeTokensPatch + replaceCommasPatch
8787
}.asPatch
8888

89+
val copyToBuffer = SymbolMatcher.normalized(
90+
Symbol("_root_.scala.collection.TraversableOnce.copyToBuffer.")
91+
)
92+
93+
def replaceCopyToBuffer(ctx: RuleCtx): Patch =
94+
ctx.tree.collect {
95+
case t @ q"${copyToBuffer(Term.Select(collection, _))}($buffer)" =>
96+
ctx.replaceTree(t, q"$buffer ++= $collection".syntax)
97+
}.asPatch
98+
8999
override def fix(ctx: RuleCtx): Patch = {
90100
replaceToList(ctx) +
91101
replaceSymbols(ctx) +
92-
replaceTupleZipped(ctx)
102+
replaceTupleZipped(ctx) +
103+
replaceCopyToBuffer(ctx)
93104
}
94105
}

2.13/tests/src/test/scala/fix/Collectionstrawman_Tests.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class Collectionstrawman_Tests
1111
Seq(AbsolutePath(BuildInfo.outputSourceroot))
1212
) {
1313
override def assertNoDiff(a: String, b: String, c: String) = {
14-
println(a)
1514
super.assertNoDiff(a, b, c)
1615
}
1716
runAllTests()

0 commit comments

Comments
 (0)