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

const blocks are formatted differently between 1.80 and 1.81; presense of lint attributes changes block formatting #6341

Open
eric-seppanen opened this issue Sep 21, 2024 · 3 comments

Comments

@eric-seppanen
Copy link

If I ask cargo fmt from 1.81.0 to format this code, I get:

const COUNT: u8 = 5;

pub struct Thing;

impl Thing {
    pub fn set() {
        #[allow(clippy::assertions_on_constants)]
        const {
            assert!(COUNT == 5)
        };
    }
}

Under 1.80.0 I get:

const COUNT: u8 = 5;

pub struct Thing;

impl Thing {
    pub fn set() {
        #[allow(clippy::assertions_on_constants)]
        const { assert!(COUNT == 5) };
    }
}

Weirdly, if I remove the lint attribute, then both 1.81.0 and 1.80.0 format the const block on a single line.

I'm on Ubuntu 22.04, with no rustfmt.toml and the rustfmt versions are:

$ .rustup/toolchains/1.80.0-x86_64-unknown-linux-gnu/bin/rustfmt -V
rustfmt 1.7.0-stable (0514789 2024-07-21)
$ .rustup/toolchains/1.81.0-x86_64-unknown-linux-gnu/bin/rustfmt -V
rustfmt 1.7.1-stable (eeb90cd 2024-09-04)

@ytmimi
Copy link
Contributor

ytmimi commented Sep 22, 2024

const block formatting was updated in the 1.7.1 release to address a different bug: #6173. It's unfortunate that the formatting for const blocks with outer attributes was impacted because of it, and it's something that was overlooked.

The upside here is that const blocks now behave like other kinds of blocks when they're annotated with outer attributes. See #6106 for an example with unsafe blocks.

To prevent further churn, I don't think we can go back to the one line formatting without gating the change.

@eric-seppanen
Copy link
Author

eric-seppanen commented Sep 23, 2024

I don't have an opinion about matching the 1.80 behavior. Either format seems fine to me.

But formatting the block differently depending on whether there's an attribute present seems like a bug.

@eric-seppanen
Copy link
Author

Rust 1.82 has the same problem, where the attribute causes different block formatting.

Example, with attribute:

impl Thing {
    pub fn set() {
        #[allow(clippy::assertions_on_constants)]
        const {
            assert!(COUNT == 5)
        };
    }
}

Example, without attribute:

impl Thing {
    pub fn set() {
        const { assert!(COUNT == 5) };
    }
}

Latest nightly (2024-10-18) behaves the same.

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

No branches or pull requests

2 participants