fmt-rfcs icon indicating copy to clipboard operation
fmt-rfcs copied to clipboard

Clarify style of empty match arms

Open varkor opened this issue 6 years ago • 4 comments

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.

varkor avatar Jan 07 '20 23:01 varkor

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 => {}."

joshtriplett avatar Jan 08 '20 02:01 joshtriplett

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

varkor avatar Jan 08 '20 02:01 varkor

Option A is implemented in rustfmt https://github.com/rust-lang/rustfmt/pull/4226 see https://github.com/rust-lang/rustfmt/issues/4065 for the tracking issue.

pickfire avatar Jun 02 '20 11:06 pickfire

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 -> ().

camsteffen avatar May 02 '21 03:05 camsteffen