From 415dc2ac78697d648297084b638e37cbd7bac716 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Tue, 2 Jan 2024 06:41:20 -0800 Subject: [PATCH] RedundantParens: exclude case of trailing commas --- .../scalafmt/rewrite/RedundantParens.scala | 4 ++- .../resources/rewrite/RedundantParens.stat | 27 ++++++++++++------- 2 files changed, 21 insertions(+), 10 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 99713d6b47..9445cfaca8 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 @@ -86,7 +86,9 @@ class RedundantParens(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { style: ScalafmtConfig ): Option[(Replacement, Replacement)] = ft.right match { - case _: Token.RightParen if left.how eq ReplacementType.Remove => + case _: Token.RightParen + if (left.how eq ReplacementType.Remove) && // check trailing comma + !ftoks.prevNonComment(ft).left.is[Token.Comma] => Some((left, removeToken)) case _ => None } diff --git a/scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat b/scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat index 843c478061..15c75c64d2 100644 --- a/scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat +++ b/scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat @@ -1334,12 +1334,15 @@ object A { Option(""), ).map(identity)} >>> -test does not parse object A { def test = - foo, + ( + foo, + ) def test = - Option(""), .map(identity) + ( + Option(""), + ).map(identity) } <<< #3743 trailingCommas = never rewrite.trailingCommas.style = never @@ -1354,12 +1357,15 @@ object A { Option(""), ).map(identity)} >>> -test does not parse object A { def test = - foo, + ( + foo, + ) def test = - Option(""), .map(identity) + ( + Option(""), + ).map(identity) } <<< #3743 trailingCommas = keep rewrite.trailingCommas.style = keep @@ -1374,10 +1380,13 @@ object A { Option(""), ).map(identity)} >>> -test does not parse object A { def test = - foo, + ( + foo, + ) def test = - Option(""), .map(identity) + ( + Option(""), + ).map(identity) }