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 12946b11b3..5a6585a176 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 @@ -300,11 +300,19 @@ class RedundantBraces(implicit val ftoks: FormatTokens) session: Session, style: ScalafmtConfig, ): (Replacement, Replacement) = { + @tailrec + def okComment(xft: FormatToken): Boolean = + ftoks.prevNotTrailingComment(xft) match { + case Right(x) => + if ((x eq xft) && x.hasBreak) + !ftoks.isAttachedCommentThenBreak(ftoks.next(ft)) + else !session.isRemovedOnLeft(x, true) || okComment(ftoks.prev(x)) + case _ => false + } val ok = ft.meta.rightOwner match { - case _: Term.Block => ftoks.prevNotTrailingComment(ft).isRight && - !braceSeparatesTwoXmlTokens && + case _: Term.Block => !braceSeparatesTwoXmlTokens && (style.dialect.allowSignificantIndentation || - !elseAfterRightBraceThenpOnLeft) + okComment(ft) && !elseAfterRightBraceThenpOnLeft) case _ => true } if (ok) (left, removeToken) else null diff --git a/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces-if.stat b/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces-if.stat index 9ec4c789de..bf96ec37ce 100644 --- a/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces-if.stat +++ b/scalafmt-tests/shared/src/test/resources/rewrite/RedundantBraces-if.stat @@ -424,9 +424,11 @@ object a { } } >>> -Idempotency violated -=> Diff (- obtained, + expected) - searchIndices() -- // else swallow the termination and wait for pull -+ // else swallow the termination and wait for pull - } +object a { + override def onUpstreamFinish(): Unit = + if (buffer.isEmpty) + completeStage() + else if (isAvailable(out)) { + searchIndices() + } // else swallow the termination and wait for pull +}