Skip to content

Improve formatting of pattern alternatives #4005

Open
@RalfJung

Description

@RalfJung

I found no mention in the spec of how to format pattern alternatives like A | B | C => expr, so I have to assume that what rustfmt does there is some ad-hoc decision made during implementation. I think that formatting should be given some thought. I am not sure where to start with that, so I figured I'd open an issue. ;)

Some of the problems that we currently have:

  • With long lists of patterns (that do not fit on a single line), we end up with code like this:
Pat1
| Pat2
| Pat3
| Pat4 => ...

Notice how the patterns start at different vertical positions, destroying symmetry. This is unlike how every other "list-like thing" is formatted, which all are properly vertically aligned. Also see #3973.

  • With long matches, rustfmt can end up generating code like this:
        match ty.kind {
            // Types without identity.
            ty::Bool
            | ty::Char
            | ty::Int(_)
            | ty::Uint(_)
            | ty::Float(_)
            | ty::Str
            | ty::Array(_, _)
            | ty::Slice(_)
            | ty::RawPtr(_)
            | ty::Ref(_, _, _)
            | ty::FnPtr(_)
            | ty::Never
            | ty::Tuple(_)
            | ty::Dynamic(_, _) => self.pretty_print_type(ty),

            // Placeholders (all printed as `_` to uniformize them).
            ty::Param(_) | ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) | ty::Error => {
                write!(self, "_")?;
                Ok(self)
            }

Notice how the two lists of alternatives are formatted very differently. This should be done in a more consistent way. Just like #3995 is about formatting the match arms themselves consistent across a match, I think rustfmt should also format lists of pattern alternatives consistent across a match.

Things get even more complicated when considering match guards... but one step after the other, I'd say. ;)

Metadata

Metadata

Assignees

No one assigned

    Labels

    a-matchesmatch arms, patterns, blocks, etc

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions