rust-lang-nursery / lazy-static.rs

A small macro for defining lazy evaluated static variables in Rust.
Apache License 2.0
1.9k stars 111 forks source link

#[allow(non_upper_case_globals)] doesn't work #153

Closed lilyball closed 2 years ago

lilyball commented 5 years ago

Adding the attribute #[allow(non_upper_case_globals)] doesn't work, since attributes I provide are applied to the synthesized type but not to the actual static variable.

lazy_static! {
    #[allow(non_upper_case_globals)]
    static ref fooBar: i32 = 42;
}

produces

warning: static variable `fooBar` should have an upper case name
 --> src/lib.rs:6:16
  |
6 |     static ref fooBar: i32 = 42;
  |                ^^^^^^ help: convert the identifier to upper case: `FOO_BAR`
  |
  = note: #[warn(non_upper_case_globals)] on by default

Playground link

Stzx commented 4 years ago

https://github.com/rust-lang-nursery/lazy-static.rs/blob/421669662b35fcb455f2902daed2e20bbbba79b6/src/lib.rs#L160-L161

At the moment when static variables are expanded, no specific property additions are made, and it seems that some work needs to be done.

Shizcow commented 3 years ago

In the year since this issue was filed, has any progress been made here? Is there any problem with replacing the following lines: https://github.com/rust-lang-nursery/lazy-static.rs/blob/620be63b6ec4adf74870640ab1cf2c5280735f4e/src/lib.rs#L152-L160 With the following:

    (@MAKE TY, $(#[$attr:meta])*, ($($vis:tt)*), $N:ident) => {
        #[allow(missing_copy_implementations)]
        #[allow(non_camel_case_types)]
        #[allow(dead_code)]
        $(#[$attr])*
        $($vis)* struct $N {__private_field: ()}
        #[doc(hidden)]
        $(#[$attr])*
        $($vis)* static $N: $N = $N {__private_field: ()};
    };

Are there some attributes that should be applied to the static instead of the struct, and vice versa? Why even pass through attributes to the struct definition at all?

@Stzx said:

it seems that some work needs to be done

Any idea what work? As an outsider this seems a trivial issue. However, if there are additional considerations to be made I'm willing to put in the work to get this done.

Sorry to necro this issue, but it is still open and I've found a need for this functionality.

gcxfd commented 2 years ago

any update ?

nzinfo commented 2 years ago

Rust 1.59 , #[allow(non_upper_case_globals)] works

lilyball commented 2 years ago

Not according to the Rust Playground

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f3b937c211d0865c5d9c100c7def93e9

KodrAus commented 2 years ago

I've opened #197 that will just suppress the non_upper_case_globals warning like we already do for non_camel_case_types. It won't help users who have forbid(warnings) enabled but is in line with the way we handle this class of warning already.