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 3958e66a5c..46dd89e98e 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 @@ -147,7 +147,7 @@ class FormatWriter(formatOps: FormatOps) { def checkApply(t: Tree): Boolean = t.parent match { case Some(p @ Term.ArgClause(`t` :: Nil, _)) => - p.parent.exists(_.is[Term.Apply]) + TreeOps.isParentAnApply(p) case _ => false } 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 ada3783c55..8dde68a845 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 @@ -262,8 +262,7 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { case ta @ Term.ArgClause((func: Term.Function) :: Nil, _) if { val body = func.body (body.is[Term.Block] || func.tokens.last.ne(body.tokens.last)) && - ta.parent.exists(_.is[Term.Apply]) && - okToRemoveAroundFunctionBody(body, true) + isParentAnApply(ta) && okToRemoveAroundFunctionBody(body, true) } => getOpeningParen(ta).map((_, func)) case _ => None @@ -341,7 +340,7 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule { (p.body eq b) || shouldRemoveSingleStatBlock(b) } - case t: Term.ArgClause if t.parent.exists(_.is[Term.Apply]) => + case t: Term.ArgClause if isParentAnApply(t) => // Example: as.map { _.toString } // Leave this alone for now. // In future there should be an option to surround such expressions with parens instead of braces diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala index eaee3b60b4..d8e6bc8091 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala @@ -158,9 +158,7 @@ object TreeOps { // handle rewritten apply { { x => b } } to a { x => b } val parentApply = findTreeWithParent(t) { case Term.Block(_) => None - case p @ Term.ArgClause(_ :: Nil, _) - if p.parent.exists(_.is[Term.Apply]) => - Some(true) + case p @ Term.ArgClause(_ :: Nil, _) => Some(isParentAnApply(p)) case _ => Some(false) } if (parentApply.isDefined) addOne(arg) @@ -1041,4 +1039,7 @@ object TreeOps { case _ => false } + def isParentAnApply(t: Tree): Boolean = + t.parent.exists(_.is[Term.Apply]) + }