How to apply an attribute to mod name, but not its content?

When I try to suppress a warning about the module name, like this:

#[allow(non_snake_case)] // abbreviation
pub mod HSM;

it has the effect of disabling the warning for the entire module (all the functions and variables inside it).
That's not what I want, I only want it to stop complaining about HSM.
How can I do it?

You could

pub mod hsm;
pub use hsm as HSM;


pub mod HSM {
    pub use hsm::*;

    mod hsm;

Neither are very satisfying as they move where things are canonically defined (think documentation generation).

1 Like

That's solved by "just" doing

mod hsm;
pub use hsm as HSM;

instead, IIRC. If you want to be extra clear about intent, stick #[doc(inline)] on the pub use.

1 Like

UPDATE: Below DOES NOT WORK. Undeleting this reply to keep a record of what we've tried, so others would not waste time on it.

Thanks! You've actually nudged me toward an acceptable solution.
I've simply added


as a first line of file.

I had tried that first but got an

error[E0365]: `hsm` is private, and cannot be re-exported

Ah right. You can do silly things like

mod hidden {
    pub mod hsm;
pub use hidden::hsm as HSM;

and I think that would by-default show HSM as the defining module in documentation.

But if you want to do this via reƫxports, it'd be easier to just do

#[doc(hidden)] pub mod hsm;
#[doc(inline)] pub use hsm as HSM;

to get almost the same effect.

However, it's worth noting that the canonical path used by e.g. error messages (and name mangling) will still use the actual definition module (at least until name shortening heuristics get implemented). But there's a simpler solution that we've overlooked...

mod HSM {

Maybe not the prettiest thing to have to reƫnable the lint (and it won't respect e.g. a #[deny(nonstandard_style)] at the top level), but it functions without weird reƫxports and gives the expected canonical names.

1 Like

I tried that too (with non_snake_case) but both the outer and inner attribute attach to the module/thing-more-generally directly. That is, the inner attribute doesn't mean "put this on everything in here" it means "put it directly on the thing I'm inside", and in both cases it applies recursively. So the code as given will still warn for mod HSM itself.

I didn't find a way around this but didn't try excessively after figuring that much out. I didn't read ECMA-335 either. So it's still possible there is some way.

Thank you both for trying to help!
I thought I've overlooked some obvious way of specifying attribute target.
But since there is none, I've simply renamed the module to lowercase.
It's unfortunate, but not the end of the world. And using clever renaming tricks certainly does not worth the added complexity.

I've found it best to just give in and follow the Rust naming conventions. Trying to preserve uppercase in acronyms is a hopeless quest; what do you do when the acronym is itself an acronym of other acronyms?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.