From ec29e08bbbb973219f698dcaed8443ae8f1c5392 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Wed, 29 Dec 2021 11:50:57 -0800 Subject: [PATCH] RedundantBraces: rewrite `val`/`var`/`macro`, too --- .../scalafmt/rewrite/RedundantBraces.scala | 13 ++++++++++- .../resources/newlines/source_classic.stat | 3 +-- .../test/resources/newlines/source_fold.stat | 22 +++++++++---------- .../test/resources/newlines/source_keep.stat | 3 +-- .../resources/newlines/source_unfold.stat | 3 +-- 5 files changed, 25 insertions(+), 19 deletions(-) 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 69d81de9b3..0d0fe8943f 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 @@ -310,10 +310,16 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { case _ => false } settings.methodBodies && - getSingleStatIfLineSpanOk(b).exists(innerOk(b)) && + checkBlockAsBody(b, d.body) && !isProcedureSyntax(d) && !disqualifiedByUnit + case d: Defn.Var => d.rhs.exists(checkBlockAsBody(b, _)) + case d: Defn.Val => checkBlockAsBody(b, d.rhs) + case d: Defn.Type => checkBlockAsBody(b, d.body) + case d: Defn.Macro => checkBlockAsBody(b, d.body) + case d: Defn.GivenAlias => checkBlockAsBody(b, d.body) + case p: Term.Function if isFunctionWithBraces(p) => okToRemoveAroundFunctionBody(b, true) @@ -329,6 +335,11 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { } } + private def checkBlockAsBody(b: Term.Block, rhs: Tree)(implicit + style: ScalafmtConfig + ): Boolean = + rhs.eq(b) && getSingleStatIfLineSpanOk(b).exists(innerOk(b)) + private def innerOk(b: Term.Block)(s: Stat): Boolean = s match { case t: Term.NewAnonymous => diff --git a/scalafmt-tests/src/test/resources/newlines/source_classic.stat b/scalafmt-tests/src/test/resources/newlines/source_classic.stat index 2d492b9d24..c2ee2faa45 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_classic.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_classic.stat @@ -433,7 +433,7 @@ val attributes = { } } >>> -val attributes = { +val attributes = for (_ ← 1 to count) yield { val tag = d.readUnsignedShort() @@ -447,7 +447,6 @@ val attributes = { Some(c(name)) } } -} <<< 2.13.3 val with for/yield, redundant parens maxColumn = 80 rewrite.rules = [RedundantParens] diff --git a/scalafmt-tests/src/test/resources/newlines/source_fold.stat b/scalafmt-tests/src/test/resources/newlines/source_fold.stat index 89167be7ad..b6779d4531 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_fold.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_fold.stat @@ -395,18 +395,16 @@ val attributes = { } } >>> -val attributes = { - for (_ ← 1 to count) yield { - val tag = d.readUnsignedShort() - val length = d.readInt() - if (debug) println(s"LNB: tag ${c(tag)} ($length bytes)") - if (tag != s) { - skip(d, length) - None - } else { - val name = d.readUnsignedShort() - Some(c(name)) - } +val attributes = for (_ ← 1 to count) yield { + val tag = d.readUnsignedShort() + val length = d.readInt() + if (debug) println(s"LNB: tag ${c(tag)} ($length bytes)") + if (tag != s) { + skip(d, length) + None + } else { + val name = d.readUnsignedShort() + Some(c(name)) } } <<< 2.13.3 val with for/yield, redundant parens diff --git a/scalafmt-tests/src/test/resources/newlines/source_keep.stat b/scalafmt-tests/src/test/resources/newlines/source_keep.stat index f44573f85a..cf040358ad 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_keep.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_keep.stat @@ -438,7 +438,7 @@ val attributes = { } } >>> -val attributes = { +val attributes = for (_ ← 1 to count) yield { val tag = d.readUnsignedShort() @@ -452,7 +452,6 @@ val attributes = { Some(c(name)) } } -} <<< 2.13.3 val with for/yield, redundant parens maxColumn = 80 rewrite.rules = [RedundantParens] diff --git a/scalafmt-tests/src/test/resources/newlines/source_unfold.stat b/scalafmt-tests/src/test/resources/newlines/source_unfold.stat index 533a26180e..68f5c700cc 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_unfold.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_unfold.stat @@ -498,7 +498,7 @@ val attributes = { } } >>> -val attributes = { +val attributes = for (_ ← 1 to count) yield { val tag = d.readUnsignedShort() @@ -513,7 +513,6 @@ val attributes = { Some(c(name)) } } -} <<< 2.13.3 val with for/yield, redundant parens maxColumn = 80 rewrite.rules = [RedundantParens]