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

Clarify style of empty match arms #146

Open
varkor opened this issue Jan 7, 2020 · 4 comments
Open

Clarify style of empty match arms #146

varkor opened this issue Jan 7, 2020 · 4 comments

Comments

@varkor
Copy link
Member

varkor commented Jan 7, 2020

There are two styles of empty match arms prevalent in Rust code:

match x {
	a => {} // Option A
	b => () // Option B
}

The preferred option should be specified by the style guide (and correspondingly enforced by rustfmt). For reference, rustc currently uses a mixture, but with a bias towards Option A.

https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/expressions.md#match doesn't mention this issue, though one of the examples does show an arm using Option A.

@joshtriplett
Copy link
Member

I've never seen option B before, and I wouldn't consider it a formatting option so much as a special case of "you can omit the braces" with a unit value.

Nonetheless, if you feel that this should be documented in the style guide, we could add an explicit note that "An empty match arm should be written as pattern => {}."

@varkor
Copy link
Member Author

varkor commented Jan 8, 2020

Option B is also relatively common. I'll make a pull request.

@pickfire
Copy link
Contributor

pickfire commented Jun 2, 2020

Option A is implemented in rustfmt rust-lang/rustfmt#4226 see rust-lang/rustfmt#4065 for the tracking issue.

@camsteffen
Copy link

camsteffen commented May 2, 2021

Just to add some rationale, I like {} because to me it reads "do nothing" and () reads "a unit". I would rather not think about a unit. Similar to not writing a return type -> ().

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 a pull request may close this issue.

4 participants