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: rewrite val/var/macro, too #3022

Merged
merged 1 commit into from
Dec 30, 2021
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 @@ -310,10 +310,16 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
case _ => false
}
settings.methodBodies &&
getSingleStatIfLineSpanOk(b).exists(innerOk(b)) &&
checkBlockAsBody(b, d.body) &&
!isProcedureSyntax(d) &&
!disqualifiedByUnit

case d: Defn.Var => d.rhs.exists(checkBlockAsBody(b, _))
case d: Defn.Val => checkBlockAsBody(b, d.rhs)
case d: Defn.Type => checkBlockAsBody(b, d.body)
case d: Defn.Macro => checkBlockAsBody(b, d.body)
case d: Defn.GivenAlias => checkBlockAsBody(b, d.body)

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

Expand All @@ -329,6 +335,11 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
}
}

private def checkBlockAsBody(b: Term.Block, rhs: Tree)(implicit
style: ScalafmtConfig
): Boolean =
rhs.eq(b) && getSingleStatIfLineSpanOk(b).exists(innerOk(b))

private def innerOk(b: Term.Block)(s: Stat): Boolean =
s match {
case t: Term.NewAnonymous =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ val attributes = {
}
}
>>>
val attributes = {
val attributes =
for (_ ← 1 to count)
yield {
val tag = d.readUnsignedShort()
Expand All @@ -447,7 +447,6 @@ val attributes = {
Some(c(name))
}
}
}
<<< 2.13.3 val with for/yield, redundant parens
maxColumn = 80
rewrite.rules = [RedundantParens]
Expand Down
22 changes: 10 additions & 12 deletions scalafmt-tests/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -395,18 +395,16 @@ val attributes = {
}
}
>>>
val attributes = {
for (_ ← 1 to count) yield {
val tag = d.readUnsignedShort()
val length = d.readInt()
if (debug) println(s"LNB: tag ${c(tag)} ($length bytes)")
if (tag != s) {
skip(d, length)
None
} else {
val name = d.readUnsignedShort()
Some(c(name))
}
val attributes = for (_ ← 1 to count) yield {
val tag = d.readUnsignedShort()
val length = d.readInt()
if (debug) println(s"LNB: tag ${c(tag)} ($length bytes)")
if (tag != s) {
skip(d, length)
None
} else {
val name = d.readUnsignedShort()
Some(c(name))
}
}
<<< 2.13.3 val with for/yield, redundant parens
Expand Down
3 changes: 1 addition & 2 deletions scalafmt-tests/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ val attributes = {
}
}
>>>
val attributes = {
val attributes =
for (_ ← 1 to count)
yield {
val tag = d.readUnsignedShort()
Expand All @@ -452,7 +452,6 @@ val attributes = {
Some(c(name))
}
}
}
<<< 2.13.3 val with for/yield, redundant parens
maxColumn = 80
rewrite.rules = [RedundantParens]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ val attributes = {
}
}
>>>
val attributes = {
val attributes =
for (_ ← 1 to count)
yield {
val tag = d.readUnsignedShort()
Expand All @@ -513,7 +513,6 @@ val attributes = {
Some(c(name))
}
}
}
<<< 2.13.3 val with for/yield, redundant parens
maxColumn = 80
rewrite.rules = [RedundantParens]
Expand Down