From 20dcbb17a33feb36b00f9982381ab9daad9cd007 Mon Sep 17 00:00:00 2001 From: gontard Date: Sat, 11 Jun 2022 10:46:28 +0200 Subject: [PATCH] Fix RedundantSyntax raw interpolator handling RedundantSyntax raised an exception for some characters were escaped in a raw interpolator. scalafix.internal.v1.FileException: unexpected error processing file /workspace/A.scala Caused by: scala.StringContext$InvalidEscapeException: invalid escape '\+' not one of [\b, \t, \n, \f, \r, \\, \", \', \uxxxx] at index 4 in "(.*)\+.*@(.*)". Use \\ for literal \. at scala.StringContext$.loop$2(StringContext.scala:417) at scala.StringContext$.replace(StringContext.scala:429) at scala.StringContext$.processEscapes(StringContext.scala:387) at scalafix.internal.rule.RedundantSyntax$$anonfun$fix$1.applyOrElse(RedundantSyntax.scala:39) --- .../main/scala/scalafix/internal/rule/RedundantSyntax.scala | 3 +-- .../main/scala/test/redundantSyntax/StringInterpolator.scala | 1 + .../main/scala/test/redundantSyntax/StringInterpolator.scala | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/scalafix-rules/src/main/scala/scalafix/internal/rule/RedundantSyntax.scala b/scalafix-rules/src/main/scala/scalafix/internal/rule/RedundantSyntax.scala index 807223a62..4e07d04c5 100644 --- a/scalafix-rules/src/main/scala/scalafix/internal/rule/RedundantSyntax.scala +++ b/scalafix-rules/src/main/scala/scalafix/internal/rule/RedundantSyntax.scala @@ -35,8 +35,7 @@ class RedundantSyntax(config: RedundantSyntaxConfig) Nil ) if config.stringInterpolator - && (p == "s" || p == "f" || (p == "raw" && StringContext - .processEscapes(v) == v)) => + && (p == "s" || p == "f" || (p == "raw" && !v.contains('\\'))) => Patch.removeTokens(interpolator.prefix.tokens) }.asPatch } diff --git a/scalafix-tests/input/src/main/scala/test/redundantSyntax/StringInterpolator.scala b/scalafix-tests/input/src/main/scala/test/redundantSyntax/StringInterpolator.scala index 6b36a3542..6a06f5cd1 100644 --- a/scalafix-tests/input/src/main/scala/test/redundantSyntax/StringInterpolator.scala +++ b/scalafix-tests/input/src/main/scala/test/redundantSyntax/StringInterpolator.scala @@ -27,6 +27,7 @@ class StringInterpolator { b = raw"foo $a \nbar" b = raw"foo\nbar\\" b = raw"foo bar" + b = raw"a\*b\+" b = my"foo" b = my"foo $a bar" diff --git a/scalafix-tests/output/src/main/scala/test/redundantSyntax/StringInterpolator.scala b/scalafix-tests/output/src/main/scala/test/redundantSyntax/StringInterpolator.scala index 997b921a2..a9a998550 100644 --- a/scalafix-tests/output/src/main/scala/test/redundantSyntax/StringInterpolator.scala +++ b/scalafix-tests/output/src/main/scala/test/redundantSyntax/StringInterpolator.scala @@ -22,6 +22,7 @@ class StringInterpolator { b = raw"foo $a \nbar" b = raw"foo\nbar\\" b = "foo bar" + b = raw"a\*b\+" b = my"foo" b = my"foo $a bar" @@ -29,4 +30,4 @@ class StringInterpolator { implicit class MyInterpolator(sc: StringContext) { def my(subs: Any*): String = sc.toString + subs.mkString("") } -} \ No newline at end of file +}