From 7fad633b686fea1dfcc372265f9871e31647a79b Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Thu, 7 Mar 2024 22:11:36 -0800 Subject: [PATCH 1/2] Test redundant parens and braces, one-line func --- .../src/test/resources/rewrite/RedundantParens.stat | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat b/scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat index 47b44540a2..51bbf6ae55 100644 --- a/scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat +++ b/scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat @@ -1404,3 +1404,12 @@ object A { Option(""), ).map(identity) } +<<< func in parens and braces, single line +rewrite.rules = [RedundantBraces, RedundantParens] +=== +foo.mtd({ x => x + 1 }) +>>> +Idempotency violated +=> Diff (- obtained, + expected) +-foo.mtd(x => x + 1) ++foo.mtd { x => x + 1 } From 44e0c737513f5f4c231a718b6bf02f9b84956079 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Thu, 7 Mar 2024 16:59:27 -0800 Subject: [PATCH 2/2] FormatWriter: mind rewritten tokens for {} -> () --- .../main/scala/org/scalafmt/internal/FormatWriter.scala | 2 +- .../scala/org/scalafmt/rewrite/RedundantBraces.scala | 9 +++++++-- .../src/test/resources/rewrite/RedundantParens.stat | 5 +---- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala index f4cdd8b515..6fa02b7a2b 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala @@ -162,7 +162,7 @@ class FormatWriter(formatOps: FormatOps) { val ok = tok.meta.leftOwner match { case b: Term.Block => checkApply(b) && RedundantBraces.canRewriteBlockWithParens(b) && - b.parent.exists(_.tokens.last.start == rb.start) + b.parent.exists(tokens.getLast(_) eq tok) case f: Term.FunctionTerm => checkApply(f) && RedundantBraces.canRewriteFuncWithParens(f) case t @ TreeOps.SingleArgInBraces(arg) => diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala index 5d0b275ff0..1acf0716cb 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RedundantBraces.scala @@ -27,14 +27,19 @@ object RedundantBraces extends Rewrite with FormatTokensRewrite.RuleFactory { case _ => false } - def canRewriteBlockWithParens(b: Term.Block): Boolean = + def canRewriteBlockWithParens(b: Term.Block)(implicit + ftoks: FormatTokens + ): Boolean = getBlockSingleStat(b).exists(canRewriteStatWithParens) - def canRewriteStatWithParens(t: Stat): Boolean = + def canRewriteStatWithParens(t: Stat)(implicit ftoks: FormatTokens): Boolean = t match { case f: Term.FunctionTerm => canRewriteFuncWithParens(f) case _: Term.Assign => false // disallowed in 2.13 case _: Defn => false + case _: Term.PartialFunction => false + case b @ Term.Block(s :: Nil) if !ftoks.isEnclosedInMatching(b) => + canRewriteStatWithParens(s) case _ => true } diff --git a/scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat b/scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat index 51bbf6ae55..74ee3f3542 100644 --- a/scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat +++ b/scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat @@ -1409,7 +1409,4 @@ rewrite.rules = [RedundantBraces, RedundantParens] === foo.mtd({ x => x + 1 }) >>> -Idempotency violated -=> Diff (- obtained, + expected) --foo.mtd(x => x + 1) -+foo.mtd { x => x + 1 } +foo.mtd(x => x + 1)