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

if/else closures with return type removes curly brackets #4577

Closed
bombsimon opened this issue Dec 5, 2020 · 1 comment · Fixed by #4580
Closed

if/else closures with return type removes curly brackets #4577

bombsimon opened this issue Dec 5, 2020 · 1 comment · Fixed by #4580
Labels
1x-backport:completed a-closures bug Panic, non-idempotency, invalid code, etc.

Comments

@bombsimon
Copy link

bombsimon commented Dec 5, 2020

Describe the bug

if/else in closure with return type removes required curly brackets. This doesn't seem to be too uncommon to me but I didn't find any issues for this bug. Sorry if this is already known, if so please close this issue and refer to the original.

To Reproduce

fn main() {
    let s: String = "ABAABBAA".chars().map(|c| -> char {
        if c == 'A' {
            '0'
        } else {
            '1'
        }
    }).collect();
    
    println!("{}", s);
}

After running rustfmt on this code, it becomes

fn main() {
    let s: String = "ABAABBAA"
        .chars()
        .map(|c| -> char if c == 'A' { '0' } else { '1' })
        .collect();

    println!("{}", s);
}

This is no longer possible to compile and gives the following error:

error: expected `{`, found keyword `if`
 --> src/main.rs:4:26
  |
4 |         .map(|c| -> char if c == 'A' { '0' } else { '1' })
  |                          ^^------------------------------
  |                          |
  |                          expected `{`
  |                          help: try placing this code inside a block: `{ if c == 'A' { '0' } else { '1' } }`

error: aborting due to previous error

The following example will not break the code:

fn main() {
    let s: String = "ABAABBAA"
        .chars()
        .map(|c| -> char {
            if c == 'A' {
                return '0';
            }

            '1'
        })
        .collect();

    println!("{}", s);
}

Expected behavior

The code to be formatted, but still working

Meta

  • rustfmt version: rustfmt 1.4.24-stable (eb894d5 2020-11-05)
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: rustftm <filename> or via Rust vim plugin
@bombsimon bombsimon added the bug Panic, non-idempotency, invalid code, etc. label Dec 5, 2020
@calebcartwright calebcartwright added the 1x-backport:pending Fixed/resolved in source but not yet backported to a 1x branch and release label Dec 12, 2020
@calebcartwright
Copy link
Member

Backported in v1.4.30

@calebcartwright calebcartwright added 1x-backport:completed and removed 1x-backport:pending Fixed/resolved in source but not yet backported to a 1x branch and release labels Dec 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1x-backport:completed a-closures bug Panic, non-idempotency, invalid code, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants