Skip to content

Formating of match should consider consistency across match arms #196

Closed as not planned
@RalfJung

Description

@RalfJung

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions