Suppress warnings then restore?

Sample:

mod some_character_codes {
    pub const a: u32 = 0x61;
    pub const b: u32 = 0x62;
    // Constants for letters from 'c' to 'y'
    pub const z: u32 = 0x7a;

    // Other constants that are compliant with naming convention
    pub const A: u32 = 0x41;
}

Question:
How can suppress non_upper_case_globals for just a region? Not the whole file, not a single line, but the region between a and z. In many other languages, I can first "disable", then "restore".

  1. I don't want to name the constants like LOWER_A. Keeping the constant name as the same character represented by the code point is most intuitive.
  2. Why not enum? Because I can easily compare a u32 number (which may not even be a valid Unicode code point) to these constants without any conversion.

Why not #[repr(u32)] enum, which can be converted with Charcode::a as u32?

Anyway, it's possible with the following trick:

mod some_character_codes {
    // Make an inner (private) module and override the lint rules for it only
    #[allow(nonstandard_style)] mod lowercase {
    pub const a: u32 = 0x61;
    pub const b: u32 = 0x62;
    // Constants for letters from 'c' to 'y'
    pub const z: u32 = 0x7a;
    }
 
    // Reexport anything from the inner module, so that the external code uses everything the same   
    pub use lowercase::*;

    // Other constants that are compliant with naming convention
    pub const A: u32 = 0x41;
}
1 Like

Thanks! Looks like good solution.

I don't like adding as u32 all over the places.

But not having the disable/restore pair is still inconvenient. Per line suppression is allowed, so I guess there is no design/technical reason to block this feature. Maybe it is not a priority?

Rust makes an emphasis on not having line-dependent attributes or anything of the sort. Attributes affect items, and a mod is an item. Lines X to Y in a file aren't an item, they're lines in the source code, and hence you can't attach an attribute to them.

Good point.

My 2 cents: the constants in the region can be considered a collection of items. And we are applying the attribute to the collection of items, not lines of source code.

Rust has #![allow(non_upper_case_globals)] that can be added to top of a file.

The #! syntax applies to the module actually :smile:, so it still applies it to an item.

Although the point about applying it to a collection of items is interesting, delimiting those items is (in my opinion) probably best left to the module system (as opposed to macros).


Note that this works (in nightly, and with the macro_rules_attribute crate to not deal with writing an actual proc macro).

#![feature(custom_inner_attributes)]

macro_rules! Bar {
    ($($x:tt)*) => {};
}

mod foo {
    #![macro_rules_attribute::apply(Bar)]
}

fn main() {}

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.