rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
6k stars 883 forks source link

Cargo fmt breaks very odd loop break code #5484

Closed xd009642 closed 2 years ago

xd009642 commented 2 years ago

Disclaimer: This code is very odd and deliberately so as part of stressing some syn based source analysis and expression reachability stuff. I don't write code like this as part of an application.

Given the code which compiles and passes

#[test]
fn test_loop_breaks_no_label_but_has_value() {
    let x = 123;
    let _x: u32 = loop {
        break loop {
            break loop {
                loop {
                    break x; // does nothing really
                };
                break x;
            }
        }
    };
}

Cargo fmt turns it to:

#[test]
fn test_loop_breaks_no_label_but_has_value() {
    let x = 123;
    let _x: u32 = loop {
        break loop {
            break loop {
                loop {
                    break x; // does nothing really
                }
                break x;
            };
        };
    };
}

Causing this error:

error[E0308]: mismatched types
  --> src/lib.rs:38:27
   |
38 |                     break x; // does nothing really
   |                           ^ expected `()`, found integer

Of course there's a chance the formatting rules that cause this may break other projects using more normal code, hence the issue.

calebcartwright commented 2 years ago

This looks like another example, but same fundamental issue as #5377 to me. Will leave it open for a bit in case you disagree, but am otherwise inclined to close as a duplicate (though we'll include this as a test case in the eventual fix)

PS also wanted to say thank you for tarpaulin :grin: :pray:

xd009642 commented 2 years ago

Ah yeah this is definitely looks like a duplicate to me, close away :pray:

PS thank you, thanks for rustfmt :heart: