Skip to content

rustfmt is not idempotent over some blocks that consist of a series of empty statements finished by an expression #4748

Open
@Maxpxt

Description

@Maxpxt

Describe the bug

rustfmt is not idempotent over some blocks that consist of a series of empty statements finished by an expression. The first pass strips the empty statements and the second pass applies any applicable transformation to the block such as unwrapping the expression in it or rearranging it into a single line.

To Reproduce

Example input:

fn foo(x: Option<i32>) -> i32 {
    let y = {
        ;
        x
    };
    match y {
        Some(y) => {
            ;
            ;
            y
        }
        None => 0,
    }
}

Calling rustfmt once results in

fn foo(x: Option<i32>) -> i32 {
    let y = {
        x
    };
    match y {
        Some(y) => {
            y
        }
        None => 0,
    }
}

Calling rustfmt on the previous output then results in

fn foo(x: Option<i32>) -> i32 {
    let y = { x };
    match y {
        Some(y) => y,
        None => 0,
    }
}

Expected behavior

rustfmt should return the output of the second pass in the first pass.

Meta

  • rustfmt version: rustfmt 1.4.36-nightly (7de6968 2021-02-07)
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt?: via rust-analyzer in vscode, but this was tested with rustfmt too

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugPanic, non-idempotency, invalid code, etc.p-medium

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions