Skip to content

Invalid code when formatting matches!() expressions with leading pipe also for the first alternative #5860

Open
@tsdh

Description

@tsdh

When I format the following code using rustfmt src/cmds.rs (with rustfmt 1.5.2-stable (8ede3aa 2023-07-12)) I get invalid code.

Original valid code:

impl SwayrCommand {
    fn is_scripting_command(&self) -> bool {
        matches!(
            self,
            | SwayrCommand::GetWindowsAsJson { .. }
            | SwayrCommand::ForEachWindow { .. }
        )
    }
}

Invalid code after formatting:

impl SwayrCommand {
    fn is_scripting_command(&self) -> bool {
        matches!(self, |SwayrCommand::GetWindowsAsJson { .. }| {
            SwayrCommand::ForEachWindow { .. }
        })
    }
}

The issue seems to be caused by the pipe | before the first alternative which is probably uncommon but legal.


I hoped I could fix the subjectively strange indentation of matches!() where the 2nd to last alternative are indented much more than the first that way. I.e., that's what I get now

impl SwayrCommand {
    fn is_scripting_command(&self) -> bool {
        matches!(
            self,
            SwayrCommand::GetWindowsAsJson { .. }
                | SwayrCommand::ForEachWindow { .. }
                | SwayrCommand::ForEachWindow2 { .. }
                | SwayrCommand::ForEachWindow4 { .. }
        )
    }
}

and what would please me more would be

impl SwayrCommand {
    fn is_scripting_command(&self) -> bool {
        matches!(
            self,
            SwayrCommand::GetWindowsAsJson { .. }
            | SwayrCommand::ForEachWindow { .. }
            | SwayrCommand::ForEachWindow2 { .. }
            | SwayrCommand::ForEachWindow4 { .. }
        )
    }
}

and by prefixing even the first alternative with a pipe, I would get

impl SwayrCommand {
    fn is_scripting_command(&self) -> bool {
        matches!(
            self,
            | SwayrCommand::GetWindowsAsJson { .. }
            | SwayrCommand::ForEachWindow { .. }
            | SwayrCommand::ForEachWindow2 { .. }
            | SwayrCommand::ForEachWindow4 { .. }
        )
    }
}

which is as aesthetically pleasing as the wished-for version.

Metadata

Metadata

Assignees

No one assigned

    Labels

    a-macrosbugPanic, non-idempotency, invalid code, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions