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: keep block w/ standalone comment #4262

Merged
merged 2 commits into from
Sep 14, 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 @@ -293,7 +293,13 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
private def onRightBrace(left: Replacement)(implicit
ft: FormatToken,
style: ScalafmtConfig,
): (Replacement, Replacement) = (left, removeToken)
): (Replacement, Replacement) = {
val ok = ft.meta.rightOwner match {
case _: Term.Block => ftoks.prevNotTrailingComment(ft).isRight
case _ => true
}
if (ok) (left, removeToken) else null
}

private def settings(implicit
style: ScalafmtConfig,
Expand Down Expand Up @@ -374,7 +380,7 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
session: Session,
style: ScalafmtConfig,
): Boolean = b.stats.nonEmpty && b.tokens.headOption.contains(ft.right) &&
b.tokens.last.is[Token.RightBrace] && okToRemoveBlock(b) &&
okToRemoveBlock(b) &&
(b.parent match {
case Some(p: Term.ArgClause) => p.parent.exists(checkValidInfixParent)
case Some(p) => checkValidInfixParent(p)
Expand Down
16 changes: 16 additions & 0 deletions scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1924,3 +1924,19 @@ object a {
// console accesses DynamicVariable, let's discard it too
!t.className.startsWith("scala/util/DynamicVariable")
}
<<< #4133 standalone comment at end of block
rewrite.rules = [RedundantBraces]
===
object a {
for (jinner <- jclazz.getDeclaredClasses) {
jclassAsScala(jinner) // inner class is entered as a side-effect
// no need to call enter explicitly
}
}
>>>
object a {
for (jinner <- jclazz.getDeclaredClasses) {
jclassAsScala(jinner) // inner class is entered as a side-effect
// no need to call enter explicitly
}
}
Loading