Non-upper case constant warning misapplied in patterns

c01b571
Opened by Russell Johnston at 2023-11-19 06:23:55

A constant with #[allow(non_upper_case_globals)] applied still generates a warning when used in a pattern, unless the match also has the allow attribute. For example:

#[allow(non_upper_case_globals)]
mod keywords {
    pub const If: u32 = 0;
    pub const While: u32 = 1;
}

fn main() {
    use keywords::*;

    // the warning goes away with this attribute:
    // #[allow(non_upper_case_globals)]
    match 0 {
        keywords::If => println!("if"),
        While => println!("while"),
        _ => println!("<unknown>"),
    }
}

Playground

  1. The code that emits that warning links to issue #7526. One should find out what its actual purpose was and whether its still relevant today.

    est31 at 2017-01-28 23:36:13

  2. Is it sensible to have this warning at all in usages of the global? After all, the user of a library isn't really free to make this choice. How about just when the global / constant is defined? I'm encountering this for all of the constants from xlib - https://docs.rs/x11/2.15.0/x11/xlib/index.html#constants . For ease of copy+modifying (or corroding!) C code, this choice makes sense.

    Note: I'm rather new to rust. So, perhaps there's a good reason for this that I don't see. From the newbie perspective this is a puzzling warning.

    Michael Sloan at 2017-09-24 06:29:09

  3. haha, I'm deriving a trait for an enum that uses bindgen-generated constants:

    warning: constant in pattern `weston_layer_position_WESTON_LAYER_POSITION_UI` should have an upper case name such as `WESTON_LAYER_POSITION_WESTON_LAYER_POSITION_UI`        
      --> src/layer.rs:22:41                                                                                                                                                                
       |                                                                                                                                                                                    
    22 | #[derive(Debug, Copy, Clone, PartialEq, Primitive)]                                                                                                                                
       |                                         ^^^^^^^^^                                                                                                                                  
    

    I can't even put #[allow(non_upper_case_globals)] anywhere in my code :D

    Val Packett at 2017-12-20 21:01:47

  4. When is this warning ever even needed?

    Alix Bott at 2021-05-21 09:42:18