Skip to content

Commit

Permalink
RedundantBraces: rewrite val/var/macro, too
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Dec 30, 2021
1 parent bd78d8b commit ec29e08
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
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

0 comments on commit ec29e08

Please sign in to comment.