rust-lang / rust-lang/rustfmt

"if expression" in implicit return position (?) considered a statement

Open
#4,351 5 comments 3 reactions 1 assignee View on GitHub

@topecongiro is already working on this.

Since Jul 24, 2020.

A-matches I-poor-formatting
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Input

let x = {let a = true; if a { 1 } else { 2 }};
let y = match () {
    _ => if true { 1 } else { 2 },
};

Output

let x = {
    let a = true;
    if a {
        1
    } else {
        2
    }
};
let y = match () {
    _ => {
        if true {
            1
        } else {
            2
        }
    }
};

Expected output

let x = {
    let a = true;
    if a { 1 } else { 2 } 
};
let y = match () {
    _ => if true { 1 } else { 2 },
};

Meta

  • rustfmt version: rustfmt 1.4.18-nightly (c1e9b7b 2020-06-13)
  • From where did you install rustfmt?: rustup

The "block" case is also what happens in functions, for example:

fn min(perc: u8) -> u8 {
    if perc >= 30 {
        perc
    } else {
        0
    }
}
// "recovery" with an unnecessary `return`:
fn min(perc: u8) -> u8 {
    return if perc >= 30 {
        perc
    } else {
        0
    }
}
// *fmt*
fn min(perc: u8) -> u8 {
    return if perc >= 30 { perc } else { 0 };
}
// collapsed nicely

This kills the crab.

The expansion damage is also compounded by defaults of match_arm_blocks: true and overflow_delimited_expr: false.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.