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

Adding an attribute to an unsafe block changes its formatting #6106

Open
tgross35 opened this issue Mar 5, 2024 · 4 comments
Open

Adding an attribute to an unsafe block changes its formatting #6106

tgross35 opened this issue Mar 5, 2024 · 4 comments

Comments

@tgross35
Copy link
Contributor

tgross35 commented Mar 5, 2024

This is the rustfmt result for an unsafe block:

fn main() {
    // #[cfg(not(fake_flag))]
    unsafe { println!() };
}

But uncomment the attribute, and it reformats it as:

fn main() {
    #[cfg(not(fake_flag))]
    unsafe {
        println!()
    };
}

Changing the attribute probably should not change the formatting.

rustfmt 1.7.0-nightly (5119208f 2024-03-02)

@ytmimi
Copy link
Contributor

ytmimi commented Mar 5, 2024

A similar issue has popped up in other cases too. #5901 and #5662 immediately come to mind, but there might be others in the backlog.

I haven't looked into this, but my hunch is that rustfmt considers the newline between the attribute and the block as an indication that the block can't be written on one line.

@ytmimi
Copy link
Contributor

ytmimi commented Mar 5, 2024

I'm assuming this affects all blocks, not just unsafe blocks? What about async blocks for example?

@tgross35
Copy link
Contributor Author

tgross35 commented Mar 5, 2024

Good call, looks like async and probably others are the same

fn main() {
    // #[cfg(not(fake_flag))]
    async { println!() };
}
fn main() {
    #[cfg(not(fake_flag))]
    async {
        println!()
    };
}

Probably indeed the same issue as #5662 so close this if you see fit. I just noticed because the diff for rust-lang/rust#121894 looked kind of funny :)

@ytmimi
Copy link
Contributor

ytmimi commented Mar 5, 2024

I think its the same underlying problem as #5662, but it's occurring in a different parts of the codebase. rustfmt rewrites attrs before rewriting the AST node those attrs are associated with so I think this is a distinct issue to the others I linked.

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

No branches or pull requests

2 participants