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

Formating of match should consider consistency across match arms #196

Open
RalfJung opened this issue Oct 18, 2024 · 1 comment
Open

Formating of match should consider consistency across match arms #196

RalfJung opened this issue Oct 18, 2024 · 1 comment

Comments

@RalfJung
Copy link
Member

The current style guide for match has some rather unfortunate consequences. rustfmt will turn

pat =>
    very_long_expr,
pat =>
    short_expr

into

pat =>
    very_long_expr,
pat => short_expr

or turn

pat => {
  stmt;
  expr
}
pat => {
  expr
}

into

pat => {
  stmt;
  expr
}
pat => expr

This can make a match that is entirely symmetric (every pattern and arm is of the same shape) look non-symmetric, making code harder to read since the reader cannot make use of the symmetry. Putting expr on the same line as pat can also make it easy to miss that there is any expression there at all.

A practical example of this looks as follows:

            LocalValue::Dead => {
                // Lots of code here.
            }
            ref mut local @ LocalValue::Live(Operand::Immediate(_))
            | ref mut local @ LocalValue::Uninitialized => Ok(Ok(local)),
            LocalValue::SomeOtherVariant => {
                // Lots of code here.
            }

It is hard to even see the code for the final arm since it got put on the same line as the pattern, making it look like the two large code blocks are the only actual code here.

                        let kind = match rvalue {
                            Rvalue::Ref(_, borrow_kind, _)
                                if borrow_kind.allows_two_phase_borrow() =>
                            {
                                RetagKind::TwoPhase
                            }
                            Rvalue::AddressOf(..) => RetagKind::Raw,
                            _ => RetagKind::Default,
                        };

All match arms are just an enum constructor, but one of them gets curly braces forcibly added around it.

@nielsle
Copy link

nielsle commented Oct 18, 2024

Hehe. I asked about this in 2017 (But I don't have strong opinions)

#34 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants