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: don't remove empty blocks #4148

Merged
merged 3 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
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 @@ -246,6 +246,10 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
case Some(_: Term.Interpolate) => handleInterpolation
case Some(_: Term.Xml) => null
case Some(_: Term.Annotate) => null
case Some(p: Case) =>
val ok = settings.generalExpressions &&
((p.body eq t) || shouldRemoveSingleStatBlock(t))
if (ok) removeToken else null
case _ => if (processBlock(t)) removeToken else null
}
case _: Term.Interpolate => handleInterpolation
Expand Down Expand Up @@ -345,14 +349,13 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
ft: FormatToken,
session: Session,
style: ScalafmtConfig,
): Boolean =
(b.tokens.headOption.contains(ft.right) &&
b.tokens.last.is[Token.RightBrace] && okToRemoveBlock(b)) &&
(b.parent match {
case Some(p: Term.ArgClause) => p.parent.exists(checkValidInfixParent)
case Some(p) => checkValidInfixParent(p)
case _ => true
})
): Boolean = b.stats.nonEmpty && b.tokens.headOption.contains(ft.right) &&
b.tokens.last.is[Token.RightBrace] && okToRemoveBlock(b) &&
(b.parent match {
case Some(p: Term.ArgClause) => p.parent.exists(checkValidInfixParent)
case Some(p) => checkValidInfixParent(p)
case _ => true
})

private def checkValidInfixParent(
p: Tree,
Expand Down Expand Up @@ -380,11 +383,6 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
b: Term.Block,
)(implicit style: ScalafmtConfig, session: Session): Boolean = b.parent
.exists {

case p: Case => settings.generalExpressions && {
(p.body eq b) || shouldRemoveSingleStatBlock(b)
}

case t: Term.ArgClause if isParentAnApply(t) =>
// Example: as.map { _.toString }
// Leave this alone for now.
Expand Down
15 changes: 15 additions & 0 deletions scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1888,3 +1888,18 @@ object a {
()
finally {}
}
<<< don't remove inner-block braces if outer-block become optional
runner.dialect = scala3
rewrite.scala3 {
insertEndMarkerMinLines = 5
removeOptionalBraces = yes
}
===
object a:
override def didSave(params: DidSaveTextDocumentParams): Unit = {
/*thisServer.synchronized*/ {}
}
>>>
object a:
override def didSave(params: DidSaveTextDocumentParams): Unit =
/*thisServer.synchronized*/ {}
Loading