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

PreferCurlyFors: don't rewrite semicolon only #4013

Merged
merged 2 commits into from
May 19, 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 @@ -52,6 +52,7 @@ private class PreferCurlyFors(implicit val ftoks: FormatTokens)

import FormatTokensRewrite._
import PreferCurlyFors._
import ftoks._

override def enabled(implicit style: ScalafmtConfig): Boolean =
PreferCurlyFors.enabled
Expand All @@ -62,8 +63,7 @@ private class PreferCurlyFors(implicit val ftoks: FormatTokens)
style: ScalafmtConfig,
): Option[Replacement] = Option {
ft.right match {
case x: Token.LeftParen
if ftoks.prevNonComment(ft).left.is[Token.KwFor] =>
case x: Token.LeftParen if prevNonComment(ft).left.is[Token.KwFor] =>
val ok = ft.meta.rightOwner match {
case t: Term.For => hasMultipleNonGuardEnums(t.enums)
case t: Term.ForYield => hasMultipleNonGuardEnums(t.enums)
Expand All @@ -75,9 +75,14 @@ private class PreferCurlyFors(implicit val ftoks: FormatTokens)

case _: Token.Semicolon
if !style.rewrite.preferCurlyFors.removeTrailingSemicolonsOnly ||
ftoks.hasBreakAfterRightBeforeNonComment(ft) =>
hasBreakAfterRightBeforeNonComment(ft) =>
ft.meta.rightOwner match {
case _: Term.For | _: Term.ForYield => removeToken
case t @ (_: Term.For | _: Term.ForYield)
if nextNonCommentAfter(ft).right.is[Token.KwIf] || {
val parenOrBrace = nextNonComment(getHead(t))
parenOrBrace.right.is[Token.LeftBrace] ||
session.claimedRule(parenOrBrace).exists(_.rule eq this)
} => removeToken
case _ => null
}

Expand Down
13 changes: 12 additions & 1 deletion scalafmt-tests/src/test/resources/rewrite/PreferCurlyFors.stat
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,18 @@ object O {
c = pos.split("->")
} yield ()
}
<<< #2695 generator and multiple guards
<<< #2695 generator and multiple guards, braces but semicolons
object O {
for { a <- b; if c; if d } yield ()
}
>>>
object O {
for {
a <- b if c
if d
} yield ()
}
<<< #2695 generator and multiple guards, parens and semicolons
object O {
for (a <- b; if c; if d) yield ()
}
Expand Down
Loading