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

rustfmt always inserts newline before else in let-else statement with attribute #5901

Closed
rhysd opened this issue Sep 5, 2023 · 5 comments · Fixed by #5902
Closed

rustfmt always inserts newline before else in let-else statement with attribute #5901

rhysd opened this issue Sep 5, 2023 · 5 comments · Fixed by #5902
Assignees

Comments

@rhysd
Copy link
Contributor

rhysd commented Sep 5, 2023

Repro

This is a code rustfmt formatted.

fn main() {
    #[cfg(target_os = "linux")]
    let x = 42
    else {
        todo!()
    };

    // #[cfg(target_os = "linux")]
    let x = 42 else { todo!() };
}

With some attribute on a let-else statement, rustfmt always inserts newline before else. When removing the attribute, this doesn't happen and rustfmt works as intended.

Expected behavior

My expectation is that rustfmt does not change formatting looking at the existence of attribute:

fn main() {
    #[cfg(target_os = "linux")]
    let x = 42 else { todo!() };

    // #[cfg(target_os = "linux")]
    let x = 42 else { todo!() };
}

Environment

  • rustfmt: 1.6.0-stable (5680fa18 2023-08-23)
    • Installed via rustup's stable toolchain
  • Rust: 1.72.0
  • OS: Windows 10
@ytmimi
Copy link
Contributor

ytmimi commented Sep 5, 2023

Thanks for the report. Confirming I can reproduce this using rustfmt 1.6.0-nightly (b6367235 2023-08-30).

I wish I would have caught this when implementing let-else. I tried to be thorough with test cases but it didn't occur to me to test a let-else with an attribute 🤦

@calebcartwright I'm guessing we'll need to version gate this fix

@rhysd
Copy link
Contributor Author

rhysd commented Sep 6, 2023

@ytmimi Thank you for triaging quickly. I'm looking at the implementation and trying to fix this issue. I'll make a PR.

@calebcartwright
Copy link
Member

@calebcartwright I'm guessing we'll need to version gate this fix

Yeah we'll have to, can't rock formatting on stable again unfortunately. Mercifully the 2024 edition won't be too terribly far out so in practice this will get cleaned up by default before too long

@ytmimi
Copy link
Contributor

ytmimi commented Sep 6, 2023

Yup, that's what I thought

@rhysd
Copy link
Contributor Author

rhysd commented Sep 7, 2023

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants