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 rewrite try { (a, b) } #4111

Merged
merged 2 commits into from
Jul 22, 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 @@ -473,13 +473,17 @@ class RedundantBraces(implicit val ftoks: FormatTokens)
}
case _: Term.Try | _: Term.TryWithHandler =>
// "try (x).y" or "try { x }.y" isn't supported until scala 2.13
// same is true with "try (a, b)" and "try ()"
// return true if rewrite is not OK
// inside exists, return true if rewrite is OK
!stat.tokens.headOption.exists {
case x: Token.LeftParen => ftoks.matchingOpt(x) match {
case Some(y) if y ne stat.tokens.last =>
session.rule[RedundantParens].exists {
_.onToken(ftoks(x, -1), session, style).exists(_.isRemove)
}
case Some(_) if !style.dialect.allowTryWithAnyExpr =>
!stat.isAny[Term.Tuple, Lit.Unit]
case _ => true
}
case x: Token.LeftBrace => ftoks.matchingOpt(x) match {
Expand Down
84 changes: 84 additions & 0 deletions scalafmt-tests/src/test/resources/rewrite/RedundantBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1804,3 +1804,87 @@ object a {
x.checkNest(nest) && x.pattern.forall(_.matcher(prefix).find())
}.flatMap(_.blanks)
}
<<< #4108 try with redundant parens in block, scala212
runner.dialect = scala212
===
object a {
try {
(a)
} finally {}
}
>>>
object a {
try
(a)
finally {}
}
<<< #4108 try with non-redundant parens (tuple) in block, scala212
runner.dialect = scala212
===
object a {
try {
(a, b)
} finally {}
}
>>>
object a {
try {
(a, b)
} finally {}
}
<<< #4108 try with non-redundant parens (unit) in block, scala212
runner.dialect = scala212
===
object a {
try {
()
} finally {}
}
>>>
object a {
try {
()
} finally {}
}
<<< #4108 try with redundant parens in block, scala213
runner.dialect = scala213
===
object a {
try {
(a)
} finally {}
}
>>>
object a {
try
(a)
finally {}
}
<<< #4108 try with non-redundant parens (tuple) in block, scala213
runner.dialect = scala213
===
object a {
try {
(a, b)
} finally {}
}
>>>
object a {
try
(a, b)
finally {}
}
<<< #4108 try with non-redundant parens (unit) in block, scala213
runner.dialect = scala213
===
object a {
try {
()
} finally {}
}
>>>
object a {
try
()
finally {}
}
Loading