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 a60e5841f3..95e6c2bb56 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 @@ -104,11 +104,15 @@ case object RedundantBraces extends Rewrite { if f.tokens.last.is[Token.RightBrace] && getTermLineSpan(f) > 0 => val rbrace = f.tokens.last val lbrace = ctx.matchingParens(TokenOps.hash(rbrace)) - val lparen = ctx.matchingParens(TokenOps.hash(rparen)) - builder += TokenPatch.Replace(lparen, lbrace.text) - builder += TokenPatch.Remove(lbrace) - builder += TokenPatch.Remove(rparen) - removeTrailingLF(rbrace.pos, rparen) + // we really wanted the first token of body but Block usually + // points to the next non-whitespace token after opening brace + if (lbrace.start <= f.body.tokens.head.start) { + val lparen = ctx.matchingParens(TokenOps.hash(rparen)) + builder += TokenPatch.Replace(lparen, lbrace.text) + builder += TokenPatch.Remove(lbrace) + builder += TokenPatch.Remove(rparen) + removeTrailingLF(rbrace.pos, rparen) + } case _ => } @@ -124,12 +128,14 @@ case object RedundantBraces extends Rewrite { && exactlyOneStatement(body) && isLineSpanOk(body) => val rbrace = fun.tokens.last val lbrace = ctx.matchingParens(TokenOps.hash(rbrace)) - builder += TokenPatch.Remove(lbrace) - builder += TokenPatch.Remove(rbrace) - removeTrailingLF(lbrace.pos, rbrace) - ctx.tokenTraverser - .reverseFind(rbrace)(!Whitespace.unapply(_)) - .foreach(t => removeTrailingLF(t.pos, rbrace)) + if (lbrace.start <= body.tokens.head.start) { + builder += TokenPatch.Remove(lbrace) + builder += TokenPatch.Remove(rbrace) + removeTrailingLF(lbrace.pos, rbrace) + ctx.tokenTraverser + .reverseFind(rbrace)(!Whitespace.unapply(_)) + .foreach(t => removeTrailingLF(t.pos, rbrace)) + } case _ => } diff --git a/scalafmt-tests/src/test/resources/rewrite/RedundantBraces-case.stat b/scalafmt-tests/src/test/resources/rewrite/RedundantBraces-case.stat index 6b2a273029..801ddbc2cc 100644 --- a/scalafmt-tests/src/test/resources/rewrite/RedundantBraces-case.stat +++ b/scalafmt-tests/src/test/resources/rewrite/RedundantBraces-case.stat @@ -75,3 +75,17 @@ object a { } } +<<< #1583 1 shouldn't remove "redundant braces" +val a = b.c(d => + e match { case f => +}) +>>> +val a = b.c(d => + e match { + case f => + } +) +<<< #1583 2 shouldn't remove "redundant braces" +val a = b.c(d => {e}, f =>g match { case h =>}) +>>> +val a = b.c(d => e, f => g match { case h => })