Skip to content

matches!(x, | A | B | ...) produces strange output #5176

Closed
@01mf02

Description

@01mf02

When using a leading | in the matches! macro, this leads to some weird formatting looking like closure syntax:

enum Bla {
    A,
    B,
}

impl Bla {
    // before processing by `rustfmt`
    pub fn weird_unformatted(self) -> bool {
        matches!(self, | Self::A | Self::B)
    }

    // output of `rustfmt` --- looks like closure syntax!
    pub fn weird_formatted(self) -> bool {
        matches!(self, |Self::A| Self::B)
    }

    // what if we use just plain `match`?
    pub fn match_unformatted(self) -> bool {
        match self {
            | Self::A | Self::B => true,
        }
    }

    // that looks better!
    pub fn match_formatted(self) -> bool {
        match self {
            Self::A | Self::B => true,
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions