Skip to content

Commit

Permalink
RedundantBraces: keep unrelated thenp/elsep apart
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Sep 20, 2024
1 parent c9ed923 commit 2d13bda
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,12 @@ class RedundantBraces(implicit val ftoks: FormatTokens)

private def onRightBrace(left: Replacement)(implicit
ft: FormatToken,
session: Session,
style: ScalafmtConfig,
): (Replacement, Replacement) = {
val ok = ft.meta.rightOwner match {
case _: Term.Block => ftoks.prevNotTrailingComment(ft).isRight &&
!braceSeparatesTwoXmlTokens
!braceSeparatesTwoXmlTokens && !elseAfterRightBraceThenpOnLeft
case _ => true
}
if (ok) (left, removeToken) else null
Expand Down Expand Up @@ -618,4 +619,27 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
private def braceSeparatesTwoXmlTokens(implicit ft: FormatToken): Boolean = ft
.left.is[Token.Xml.End] && ftoks.next(ft).right.is[Token.Xml.Start]

private def elseAfterRightBraceThenpOnLeft(implicit
ft: FormatToken,
ftoks: FormatTokens,
session: Session,
): Boolean = ftoks.nextNonCommentAfter(ft).right.is[Token.KwElse] && {
val pft = ftoks.findToken(ft, ftoks.prev) { xft =>
xft.left match {
case _: Token.Comment => false
case _: Token.RightBrace => !session.isRemovedOnLeft(xft, ok = true)
case _ => true
}
}
val rbOwner = ft.rightOwner
findTreeWithParent(pft.leftOwner) { p =>
if (p eq rbOwner) Some(false)
else p.parent match {
case None => Some(false)
case Some(pp: Term.If) if pp.thenp eq p => Some(true)
case _ => None
}
}.isDefined
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,14 @@ object a {
f(imported)
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
f(sym)
- else if (isQualifyingImplicit(name, imported, pre, imported = true))
- f(imported)
+ else if (isQualifyingImplicit(name, imported, pre, imported = true))
+ f(imported)
}
object a {
if (imported.isOverloaded) {
for (sym <- imported.alternatives)
if (isQualifyingImplicit(name, sym, pre, imported = true))
f(sym)
} else if (isQualifyingImplicit(name, imported, pre, imported = true))
f(imported)
}
<<< #4133 nested braces around expression ending with `if`-without-`else`
object a {
if (imported.isOverloaded) {
Expand All @@ -320,11 +320,11 @@ object a {
f(imported)
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
f(sym)
- else if (isQualifyingImplicit(name, imported, pre, imported = true))
- f(imported)
+ else if (isQualifyingImplicit(name, imported, pre, imported = true))
+ f(imported)
}
object a {
if (imported.isOverloaded) {
for (sym <- imported.alternatives)
if (isQualifyingImplicit(name, sym, pre, imported = true))
f(sym)
} else if (isQualifyingImplicit(name, imported, pre, imported = true))
f(imported)
}

0 comments on commit 2d13bda

Please sign in to comment.