rustfmt: Module declared inside macro is not checked

When I declare modules like this

#[cfg(unix)]
pub mod stub_hal;

rustfmt will check module, irrespective of condition.

However, when I declare them using cfg-if crate rustfmt doesn’t check module.

cfg_if! {
    if #[cfg(unix)] {
        pub mod stub_hal;
    }
}

Is is designed behavior? Or is macro parsing not implemented for now?

That’s rustfmt 1.0.1-nightly (be13559 2018-12-10)

About this issue

  • Original URL
  • State: open
  • Created 8 years ago
  • Reactions: 5
  • Comments: 16 (10 by maintainers)

Commits related to this issue

Most upvoted comments

Currently running into this problem as well.

While it does make sense not to parse every macro like that, what if there was a #[rustfmt::follow_modules] attribute or something similar? It would go on macros where you want rustfmt to parse and find modules.

#[rustfmt::follow_modules]
macro_rules! custom_macro {
    ($(mod $mod_name:ident;)*) => {
        // do something here...
    }
}

custom_macro! {
    mod foo;
    mod bar;
    mod baz;
}

That’d be too costly for a formatter that’s supposed to only process the lexical level of the crate. In particular, procedural macros require compilation. If the procedural macro fails to compile, that would result in an error, which breaks backward compatibility of rustfmt.