From 7eee1bdcd955ccb26a7328501e853b08cbb140d1 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Mon, 14 Sep 2020 07:54:48 -0700 Subject: [PATCH] RedundantParens: check if postfix only is allowed --- .../org/scalafmt/rewrite/RedundantParens.scala | 13 +++++++++++-- .../src/test/resources/rewrite/RedundantParens.stat | 7 +++++-- 2 files changed, 16 insertions(+), 4 deletions(-) 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 {