Skip to content

Add a consistent group around if-stmts. #185

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

Merged
merged 1 commit into from
Apr 9, 2020

Conversation

dylansturg
Copy link
Contributor

The consistent group forces the bodies of if-stmts with else clauses to have breaks around all braces when the stmt spans multiple lines. Previously. the breaks around the braces only fired if the body didn't fit on the line. That behavior lead to hard to read if-stmts because if was unclear where conditions stopped and the body started.

}
if let foo = getmyfoo(), let bar = getmybar(),
foo.baz && bar.baz && someOtherCondition
{ foo() }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice to have these breaks fire too when the condition wraps, if possible—that way, the body is only condensed to one line if the whole statement fits on one line. I'm surprised that doesn't happen already, since I'd expect the reset break that did fire to do so consistently with the breaks inside the braces. What's going on there, and would it be a lot harder to make it do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intentionally only added the consistent group when there is an else clause, since the problem of inconsistent breaks around the clauses don't exist when there's only 1 clause. It's a simple change to unconditionally add the consistent group though.

The consistent group forces the bodies of if-stmts with else clauses to have breaks around all braces when the stmt spans multiple lines. Previously. the breaks around the braces only fired if the body didn't fit on the line. That behavior lead to hard to read if-stmts because if was unclear where conditions stopped and the body started.
@allevato allevato merged commit 194a375 into swiftlang:master Apr 9, 2020
@dylansturg dylansturg deleted the better_if_stmts branch April 15, 2020 20:33
aaditya-chandrasekhar pushed a commit to val-verde/swift-format that referenced this pull request May 20, 2021
* Use prebuilt Docker image

* Add Changelog entry for swiftlang#185
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants