Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RedundantBraces: cosmetic, minor refactor #4447

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
style: ScalafmtConfig,
): (Replacement, Replacement) = left.how match {
case ReplacementType.Remove =>
val resOpt = getRightBraceBeforeRightParen(false).map { rb =>
getRightBraceBeforeRightParen(shouldBeRemoved = false).map { rb =>
ft.meta.rightOwner match {
case ac: Term.ArgClause => ftoks.matchingOpt(rb.left).map(ftoks.prev)
.foreach { lb =>
Expand All @@ -178,15 +178,14 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
case _ =>
}
(left, removeToken)
}
resOpt.orNull
}.orNull

case ReplacementType.Replace if {
val lft = left.ft
val ro = ft.meta.rightOwner
(lft.meta.rightOwner eq ro) && lft.right.is[Token.LeftBrace]
} =>
val pftOpt = getRightBraceBeforeRightParen(true)
val pftOpt = getRightBraceBeforeRightParen(shouldBeRemoved = true)
def replaceIfAfterRightBrace = pftOpt.map { pft =>
val rb = pft.left
// move right to the end of the function
Expand Down Expand Up @@ -456,7 +455,7 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
checkBlockAsBody(b, d.body, noParams = d.paramClauseGroup.isEmpty)

case p: Term.FunctionTerm if isFunctionWithBraces(p) =>
okToRemoveAroundFunctionBody(b, true)
okToRemoveAroundFunctionBody(b, okIfMultipleStats = true)

case _: Term.If => settings.ifElseExpressions &&
shouldRemoveSingleStatBlock(b)
Expand Down Expand Up @@ -513,9 +512,9 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
session: Session,
): Boolean = getSingleStatIfLineSpanOk(b).exists { stat =>
@tailrec
def checkParent(tree: Tree): Boolean = tree match {
def keepForParent(tree: Tree): Boolean = tree match {
case t: Term.ArgClause => t.parent match {
case Some(p) => checkParent(p)
case Some(p) => keepForParent(p)
case _ => true
}
case _: Term.TryClause =>
Expand Down Expand Up @@ -586,7 +585,7 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
)
}

innerOk(b)(stat) && !b.parent.exists(checkParent)
innerOk(b)(stat) && !b.parent.exists(keepForParent)
}

@inline
Expand All @@ -607,20 +606,12 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
case _ => okIfMultipleStats
})

@tailrec
private def getBlockNestedPartialFunction(
tree: Tree,
): Option[Term.PartialFunction] = tree match {
case x: Term.PartialFunction => Some(x)
case x: Term.Block => getBlockNestedPartialFunction(x)
case _ => None
}

@tailrec
private def getBlockNestedPartialFunction(
tree: Term.Block,
): Option[Term.PartialFunction] = getBlockSingleStat(tree) match {
case Some(x: Term.PartialFunction) => Some(x)
case Some(x: Term.Block) => getBlockNestedPartialFunction(x)
case Term.Block(x :: Nil) => getBlockNestedPartialFunction(x)
case _ => None
}

Expand Down
Loading