diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantParens.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantParens.scala index bd6fc59e3d..5d12a3bc44 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantParens.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantParens.scala @@ -28,9 +28,9 @@ class RedundantParens(implicit ctx: RewriteCtx) extends RewriteSession { private[rewrite] val rewriteFunc: PartialFunction[Tree, Unit] = { case t @ (_: Term.Tuple | _: Type.Tuple | _: Lit.Unit) => remove(t, 2) - case g: Enumerator.Guard => remove(g.cond) + case g: Enumerator.Guard => maybeRemovePostfix(g.cond) - case t: Case => t.cond.foreach(remove(_)) + case t: Case => t.cond.foreach(maybeRemovePostfix) case t if usesAvoidInfix && t.parent.exists { case p: Term.ApplyInfix => p.lhs ne t @@ -75,6 +75,15 @@ class RedundantParens(implicit ctx: RewriteCtx) extends RewriteSession { case _ => remove(tree, minToKeep) } + // https://www.scala-lang.org/files/archive/spec/2.13/06-expressions.html#prefix-infix-and-postfix-operations + private def maybeRemovePostfix(tree: Tree): Unit = + tree match { + case _: Lit | _: Term.Name | _: Term.ApplyInfix | _: Term.Select | + _: Term.Apply | _: Term.ApplyUnary => + remove(tree) + case _ => + } + private def remove(tree: Tree, minToKeep: Int = 0): Unit = removeByTokens(tree.tokens, minToKeep) diff --git a/scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat b/scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat index 6654f33797..e283a7d1ca 100644 --- a/scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat +++ b/scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat @@ -315,11 +315,14 @@ object a { } } >>> -test does not parse object a { val b = c match { case true if (d match { - ^ + case true => true + case false => false + }) => + } +} <<< #2194 2 infix object a { val b = c match {