Skip to content

Commit

Permalink
RedundantBraces: claim left brace inside parens
Browse files Browse the repository at this point in the history
Since the code aims to remove the parens assuming the next brace is not
mutated by another rule, let's explicitly stake claim to that brace.
  • Loading branch information
kitbellew committed Mar 27, 2024
1 parent 22ff977 commit 869681b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Expand Up @@ -233,7 +233,13 @@ object FormatTokensRewrite {
ft: FormatToken,
style: ScalafmtConfig
): Replacement =
Replacement(this, ft, ReplacementType.Remove, style)
removeToken(Nil)

protected final def removeToken(claim: Iterable[Int] = Nil)(implicit
ft: FormatToken,
style: ScalafmtConfig
): Replacement =
Replacement(this, ft, ReplacementType.Remove, style, claim)

protected final def replaceToken(
text: String,
Expand Down
Expand Up @@ -128,8 +128,13 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
case ta @ Term.ArgClause(arg :: Nil, _)
if !ta.parent.exists(_.is[Init]) =>
getOpeningParen(ta).map { lp =>
val ko = lp.ne(rt) || getBlockNestedPartialFunction(arg).isEmpty
if (ko) null else removeToken
if (lp.ne(rt) || getBlockNestedPartialFunction(arg).isEmpty) null
else
ftoks.nextNonCommentAfter(ft) match {
case FormatToken(_, _: Token.LeftBrace, lbm) =>
removeToken(claim = lbm.idx :: Nil)
case _ => null
}
}
case _ => None
}
Expand Down

0 comments on commit 869681b

Please sign in to comment.