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

Make all source files visible to rustfmt #1398

Merged
merged 3 commits into from
Nov 26, 2024

Conversation

dtolnay
Copy link
Contributor

@dtolnay dtolnay commented Nov 26, 2024

The pervasive reliance on cfg_if!, which is not understandable by rustfmt, was leading to only 18/81 source files in this crate able to be covered by cargo fmt. This PR works around that and formats the other 63 files.

@@ -116,6 +116,16 @@ mod debug;
mod serde;
pub(crate) mod utils;

// Make formattable by rustfmt.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So just to confirm: this cfg is always evaluated to false but at least it allows rustfmt to see the file. That's a super clever trick. Thanks for that!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right.

In the longer term, hopefully rustfmt will get a built-in understanding of the syntax of cfg_if! (rust-lang/rustfmt#3253) or cfg_match! (rust-lang/rust#115585) and the workaround won't be needed.

Or alternatively, someone might make an alternative macro that doesn't rely on dedicated rustfmt support for its syntax, something like:

#[apply_cfg]
enum __ {
    r#if = #[cfg(feature = "unknown-ci")]
    {
        // This is used in CI to check that the build for unknown targets is compiling fine.
        mod unknown;
        use crate::unknown as sys;

        #[cfg(test)]
        pub(crate) const MIN_USERS: usize = 0;
    },

    r#else = #[cfg(any(
        target_os = "macos",
        target_os = "ios",
        target_os = "linux",
        target_os = "android",
        target_os = "freebsd"
    ))]
    {
        mod unix;
        use crate::unix::sys;

        #[cfg(feature = "network")]
        mod network;
        #[cfg(feature = "network")]
        use crate::unix::network_helper;

        #[cfg(test)]
        pub(crate) const MIN_USERS: usize = 1;
    },

    ...
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer the current solution. It duplicates code but at least it's "easy" to see what's going on. Gonna check with rustfmt team if they'd be open to add some special cases for macros like cfg_if!.

@GuillaumeGomez
Copy link
Owner

Makes me a bit sad to have all these manual imports but the code formatting is finally working, thanks a lot! Gonna open an issue on rustfmt to see if they can work with cfg_if!.

@GuillaumeGomez GuillaumeGomez merged commit 378cdc5 into GuillaumeGomez:master Nov 26, 2024
67 checks passed
@dtolnay dtolnay deleted the rustfmt branch November 26, 2024 18:35
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

Successfully merging this pull request may close these issues.

2 participants