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 7a9a25a730..00510f6962 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,13 +27,13 @@ object RedundantBraces extends Rewrite { case _ => false } - def canRewriteWithParens(b: Term.Block): Boolean = { + def canRewriteWithParens(b: Term.Block): Boolean = getBlockSingleStat(b).exists { - case f: Term.Function => RedundantBraces.canRewriteWithParens(f) + case f: Term.Function => canRewriteWithParens(f) case _: Term.Assign => false // disallowed in 2.13 + case _: Defn => false case _ => true } - } /* guard for statements requiring a wrapper block * "foo { x => y; z }" can't become "foo(x => y; z)" */ @@ -41,6 +41,7 @@ object RedundantBraces extends Rewrite { def canRewriteWithParens(f: Term.Function, nested: Boolean = false): Boolean = !needParensAroundParams(f) && (getTermSingleStat(f.body) match { case Some(t: Term.Function) => canRewriteWithParens(t, true) + case Some(_: Defn) => false case x => nested || x.isDefined }) diff --git a/scalafmt-tests/src/test/resources/rewrite/RedundantBraces-ParenLambdas.stat b/scalafmt-tests/src/test/resources/rewrite/RedundantBraces-ParenLambdas.stat index 5ba804e6d9..5246271bd2 100644 --- a/scalafmt-tests/src/test/resources/rewrite/RedundantBraces-ParenLambdas.stat +++ b/scalafmt-tests/src/test/resources/rewrite/RedundantBraces-ParenLambdas.stat @@ -192,9 +192,8 @@ object a { } } >>> -test does not parse object a { - childContext.onDone(_ => val _ = self.withChildren(_.remove(childContext))) + childContext.onDone { _ => val _ = self.withChildren(_.remove(childContext)) } } <<< #1756 def within lambda object a { @@ -203,9 +202,8 @@ object a { } } >>> -test does not parse object a { - childContext.onDone(_ => def a = self.withChildren(_.remove(childContext))) + childContext.onDone { _ => def a = self.withChildren(_.remove(childContext)) } } <<< #1756 type within lambda object a { @@ -214,34 +212,30 @@ object a { } } >>> -test does not parse object a { - childContext.onDone(_ => type a = Int) + childContext.onDone { _ => type a = Int } } <<< #1756 val without lambda object a { childContext.onDone { val _ = self.withChildren(_.remove(childContext)) } } >>> -test does not parse object a { - childContext.onDone(val _ = self.withChildren(_.remove(childContext))) + childContext.onDone { val _ = self.withChildren(_.remove(childContext)) } } <<< #1756 def without lambda object a { childContext.onDone { def a = self.withChildren(_.remove(childContext)) } } >>> -test does not parse object a { - childContext.onDone(def a = self.withChildren(_.remove(childContext))) + childContext.onDone { def a = self.withChildren(_.remove(childContext)) } } <<< #1756 type without lambda object a { childContext.onDone { type a = Int } } >>> -test does not parse object a { - childContext.onDone(type a = Int) + childContext.onDone { type a = Int } }