diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala index dc9fb75987..270a1312bc 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala @@ -568,7 +568,18 @@ class FormatOps( case t => t } def isOldTopLevel(child: Tree) = child.parent.exists { - case _: Term.Block | _: Term.If | _: Term.While | _: Source => true + case _: Term.If | _: Term.While | _: Source => true + case t: Term.Block => !isSingleStatBlock(t) || t.parent.forall { p => + t.tokens.head match { // check brace was not rewritten + case head: Token.LeftBrace => + (tokens.before(head).left eq head) || + (p match { + case p: Tree.WithCond => p.cond eq t + case _ => false + }) + case _ => true + } + } case fun: Term.FunctionTerm => isBlockFunction(fun) case t: Case => t.pat.eq(child) || t.body.eq(child) case SingleArgInBraces(_, arg, _) => child eq arg diff --git a/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat b/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat index 15742248ba..31d401a6b7 100644 --- a/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat +++ b/scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat @@ -1917,11 +1917,10 @@ object a { } } >>> -Idempotency violated -=> Diff (- obtained, + expected) - t.className != "scala/Console$" && -- // console accesses DynamicVariable, let's discard it too -- !t.className.startsWith("scala/util/DynamicVariable") -+ // console accesses DynamicVariable, let's discard it too -+ !t.className.startsWith("scala/util/DynamicVariable") - } +object a { + val standardFilter: MethodCallTrace => Boolean = t => + // ignore all calls to Console trigger by printing + t.className != "scala/Console$" && + // console accesses DynamicVariable, let's discard it too + !t.className.startsWith("scala/util/DynamicVariable") +}