Skip to content

The lint non_uppercase_globals is triggered even in local scope. It should trigger only for global scope, or be renamed to non_uppercase_const instead. #66954

@kingparra

Description

@kingparra

The warning "non_uppercase_globals" occurs for all uses of const, even ones in local scope. This should be renamed to "non_uppercase_const", instead.

I have some code like:

fn main() {
  fn shadow1(f: u32) {

    println!("f is {}", f)
    const s: u32 = 1;
    println!("s is {}", s);

    fn shadow2(f: u32) {
      println!("f is {}", f);
      const s: u32 = 2;
      println!("s is {}", s);
    }
    shadow2(s);

  }
  shadow1(8)
}

If I compile I get:

ƒ cargo run
   Compiling tp v0.1.0 (/home/chris/Downloads/tp)
warning: constant `s` should have an upper case name
 --> src/main.rs:5:11
  |
5 |     const s: u32 = 1;
  |           ^ help: convert the identifier to upper case: `S`
  |
  = note: `#[warn(non_upper_case_globals)]` on by default

warning: constant `s` should have an upper case name
  --> src/main.rs:10:13
   |
10 |       const s: u32 = 2;
   |             ^ help: convert the identifier to upper case: `S`

    Finished dev [unoptimized + debuginfo] target(s) in 0.19s
     Running `target/debug/tp`
f is 8
s is 1
f is 1
s is 2

You'll notice that const was never used directly within main(), or in any global context.

Using const within local scope is useful for decoupling implicit sequential control flow from variable assignment within that scope. This is useful for writing concurrent code, and communicating intent to the compiler so it can catch errors.

It would be great if the error non_uppercase_globals was renamed to non_uppercase_const, and non_uppercase_globals would only warn on usage of const in the global scope!

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-langRelevant to the language team

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions