Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indentation in closures gets formatted twice and result is not nice™ #4616

Open
cdbrkfxrpt opened this issue Dec 30, 2020 · 1 comment
Open
Labels
a-closures bug Panic, non-idempotency, invalid code, etc. only-with-option requires a non-default option value to reproduce

Comments

@cdbrkfxrpt
Copy link

Describe the bug

On first invocation of rustfmt closure code gets formatted (nicely, arguably). On any subsequent iteration it gets formatted again (not so nicely, arguably).

To Reproduce

Throw the following in a test.rs file:

#[derive(Debug)]
struct Foo {
  some:   usize,
  member: String,
}

impl Foo {
  pub fn some_fn() {
    let edb_file = fs::read_dir(&event_path)?.find_map(|p| {
      let path = p.ok()?.path();
      (path.is_file() && Self::as_regex().is_match(&get_file_name(&path).ok()?) && path.extension()? == "edb").as_some(path)
    });
  }
}

and put the following in a rustfmt.toml file in the same directory:

# "Block" | "Visual"
indent_style = "Visual"

Now invoke rustfmt on file test.rs once. The following should happen:

#[derive(Debug)]
struct Foo {
    some: usize,
    member: String,
}

impl Foo {
    pub fn some_fn() {
        let edb_file = fs::read_dir(&event_path)?.find_map(|p| {
                           let path = p.ok()?.path();
                           (path.is_file()
                            && Self::as_regex().is_match(&get_file_name(&path).ok()?)
                            && path.extension()? == "edb")
                                                          .as_some(path)
                       });
    }
}

Now invoke rustfmt on file test.rs again, and the following should happen:

#[derive(Debug)]
struct Foo {
    some: usize,
    member: String,
}

impl Foo {
    pub fn some_fn() {
        let edb_file = fs::read_dir(&event_path)?.find_map(|p| {
                                                     let path = p.ok()?.path();
                                                     (path.is_file()
                            && Self::as_regex().is_match(&get_file_name(&path).ok()?)
                            && path.extension()? == "edb")
                                                          .as_some(path)
                                                 });
    }
}

Subsequent invocations should not make a difference. Differing settings for max_width (we use 79 characters) produce similar output.

Expected behavior

rustfmt should certainly not take two invocations to format a section of code. Personally I think the output after the first invocation is the intended one.

Meta

  • rustfmt version: rustfmt 1.4.30-nightly (acd9486 2020-12-20)
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: through vim but this is reproducible also on direct invocation
@cdbrkfxrpt cdbrkfxrpt added the bug Panic, non-idempotency, invalid code, etc. label Dec 30, 2020
@calebcartwright calebcartwright added a-closures only-with-option requires a non-default option value to reproduce labels Dec 30, 2020
@ytmimi
Copy link
Contributor

ytmimi commented Jul 26, 2022

Confirming I can reproduce this with rustfmt 1.5.1-nightly (9c8ccc32 2022-07-26)

Linking the tracking issues for indent_style (#3346)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-closures bug Panic, non-idempotency, invalid code, etc. only-with-option requires a non-default option value to reproduce
Projects
None yet
Development

No branches or pull requests

3 participants