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

RedundantParens: rewrite bodies of if/while/case #3225

Merged
merged 2 commits into from
May 19, 2022
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 @@ -90,20 +90,23 @@ class RedundantParens(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
.exists(TreeOps.isSeqSingle) =>
false
case TreeOps.SplitAssignIntoParts((body, _)) =>
body.eq(t) && (t match {
case InfixApp(ia) => !breaksBeforeOp(ia)
case _ => true
})
body.eq(t) && canRewriteBody(t)
case _: Enumerator.Guard => RewriteCtx.isPostfixExpr(t)
case p: Case =>
p.cond.contains(t) && RewriteCtx.isPostfixExpr(t)
case _: Term.Do => false
if (p.body eq t) canRewriteBody(t)
else p.cond.contains(t) && RewriteCtx.isPostfixExpr(t)
case p: Term.Do =>
p.body.eq(t) && canRewriteBody(t)
case p: Term.While =>
style.dialect.allowSignificantIndentation && p.expr == t &&
ftoks.tokenBefore(p.body).left.is[Token.KwDo]
if (p.expr eq t)
style.dialect.allowSignificantIndentation &&
ftoks.tokenBefore(p.body).left.is[Token.KwDo]
else canRewriteBody(t)
case p: Term.If =>
style.dialect.allowSignificantIndentation && p.cond == t &&
ftoks.tokenBefore(p.thenp).left.is[Token.KwThen]
if (p.cond eq t)
style.dialect.allowSignificantIndentation &&
ftoks.tokenBefore(p.thenp).left.is[Token.KwThen]
else canRewriteBody(t)
case InfixApp(pia) if !infixNeedsParens(pia, t) =>
t match {
case InfixApp(tia) =>
Expand Down Expand Up @@ -152,6 +155,12 @@ class RedundantParens(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
})
}

private def canRewriteBody(tree: Tree): Boolean =
tree match {
case InfixApp(ia) => !breaksBeforeOp(ia)
case _ => true
}

private def findEnclosed(implicit ft: FormatToken): Option[Enclosed] = {
// counts consecutive parent pairs starting with the given one as the innermost
// the parens could belong to tree they are enclosing, or its parent
Expand Down
74 changes: 74 additions & 0 deletions scalafmt-tests/src/test/resources/rewrite/RedundantParens.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1033,3 +1033,77 @@ object a {
object a {
do foo while (true)
}
<<< body: if
object a {
if (true) (foo)
if (true)
(
a
+ b
)
}
>>>
object a {
if (true) foo
if (true)
(
a
+ b
)
}
<<< body: while
object a {
while (true) (foo)
while (true)
(
a
+ b
)
}
>>>
object a {
while (true) foo
while (true)
(
a
+ b
)
}
<<< body: do-while
object a {
do (foo) while (true)
do
(
a
+ b
)
while (true)
}
>>>
object a {
do foo while (true)
do (
a
+ b
) while (true)
}
<<< body: case
object a {
foo match {
case bar => (baz)
case bar => (
a
+ b
)
}
}
>>>
object a {
foo match {
case bar => baz
case bar => (
a
+ b
)
}
}