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 formats a macro when it should not #6418

Open
jaskij opened this issue Dec 13, 2024 · 3 comments
Open

rustfmt formats a macro when it should not #6418

jaskij opened this issue Dec 13, 2024 · 3 comments

Comments

@jaskij
Copy link

jaskij commented Dec 13, 2024

rustfmt is formatting - changing indentation - inside a macro, in what is not valid Rust code.

In the code below, if you change the indentation inside the proptest! macro, rustfmt will change it back to the way it is currently, when it should not. Repo with the repro.

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Item {
    pub first_long_name_a: u16,
    pub item_count: u16,
    pub area: u16,
    pub address: u16,
}

#[cfg(test)]
mod tests {
    use proptest::prelude::*;

    use super::Item;

    proptest! {
               #[test]
               fn read_item_chars(s in "\\PC*") {
                   let pi = Item{
    first_long_name_a: 0,item_count: 0,area: 0,address: 0,};
               }
           }
}

Tested on both 1.82 and nightly.

$ cargo fmt --version
rustfmt 1.7.1-stable (f6e511e 2024-10-15)
$ cargo +nightly fmt --version
rustfmt 1.8.0-nightly (33c245b9e9 2024-12-10)
@jaskij
Copy link
Author

jaskij commented Dec 13, 2024

cc @ytmimi

@ytmimi
Copy link
Contributor

ytmimi commented Dec 16, 2024

@jaskij any chance you've got a global rustfmt.toml on your system that might be setting some no default configurations? If you run rustfmt with the -v flag you should see something like Using rustfmt config file ...

I created a local file issue_6481.rs with the following content:

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Item {
    pub first_long_name_a: u16,
    pub item_count: u16,
    pub area: u16,
    pub address: u16,
}

#[cfg(test)]
mod tests {
    use proptest::prelude::*;

    use super::Item;

    proptest! {
        #[test]
        fn read_item_chars(s in "\\PC*") {
            let pi = Item {
                 first_long_name_a: 0,item_count: 0,area: 0,address: 0
            };
        }
    }
}

And when I build and run rustfmt locally (from commit 8a2c073 and executing the command cargo run --bin rustfmt -- --check issue_6481.rs) the input is left unchanged

I'm also unable to see any changes in the file content when running with rustfmt +1.82 (rustfmt 1.7.1-stable (f6e511ee 2024-10-15)) and using the default rustfmt config. Specifically I ran rustfmt +1.82 --check issue_6481.rs.

@ytmimi ytmimi added a-macros needs-mcve needs a Minimal Complete and Verifiable Example labels Dec 16, 2024
@jaskij
Copy link
Author

jaskij commented Dec 17, 2024

Having poked it around - this may look like a bug but probably isn't. I'm not sure.

What I have found out:

  • rustfmt only touches the contents if the closing brace for proptest! is not aligned correctly
  • or if something inside starts to the left of the closing brace and is aligned before the macro

I have added more files in that repro repository, ones that do get formatted.

Edit:

Almost forgot, no, no global config file whatsoever. Running current master (8a2c0739):

$ cargo run --bin rustfmt -- -v --check ~/projects/repros/rustfmt-proptest-confusion-repro/src/*
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.03s
     Running `target/debug/rustfmt -v --check /home/jaskij/projects/repros/rustfmt-proptest-confusion-repro/src/doesnt_format.rs /home/jaskij/projects/repros/rustfmt-proptest-confusion-repro/src/formats.rs /home/jaskij/projects/repros/rustfmt-proptest-confusion-repro/src/lib.rs /home/jaskij/projects/repros/rustfmt-proptest-confusion-repro/src/old-doesnt-format.rs /home/jaskij/projects/repros/rustfmt-proptest-confusion-repro/src/old-formats-2.rs /home/jaskij/projects/repros/rustfmt-proptest-confusion-repro/src/old-formats.rs`
Formatting /home/jaskij/projects/repros/rustfmt-proptest-confusion-repro/src/doesnt_format.rs
Spent 0.001 secs in the parsing phase, and 0.001 secs in the formatting phase

@ytmimi ytmimi added p-low poor-formatting and removed needs-mcve needs a Minimal Complete and Verifiable Example labels Dec 18, 2024
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