Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lint “non_upper_case_globals” wrongly warns the consumer #25207

Open
nwin opened this issue May 8, 2015 · 10 comments
Open

lint “non_upper_case_globals” wrongly warns the consumer #25207

nwin opened this issue May 8, 2015 · 10 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@nwin
Copy link
Contributor

nwin commented May 8, 2015

The lint “non_upper_case_globals” wrongly warns the consumer about a ill-named constant (playpen). It only seems to happen if the constant is imported into the current namespace and if you’re using it in a match statement:

mod consts {
    #![allow(non_upper_case_globals)]
    pub const fooBAR: usize = 23;
}

fn main() {
    use consts::*;
    println!("{}", match 23 {
        fooBAR => "yes",
        _ => "no"
    });
}
@pnkfelix
Copy link
Member

pnkfelix commented May 8, 2015

I think this is by design; someone reading the match arm fooBAR => ... is likely to interpret fooBAR as a binding, not a constant that is being matched against...

@nwin
Copy link
Contributor Author

nwin commented May 8, 2015

I don’t think so. You have the same ambiguity problem when matching against lower-case enum variants and there is no warning emitted for that.

@pnkfelix
Copy link
Member

pnkfelix commented May 9, 2015

I think I can agree that there is an "if-and-only-if" relationship here: if (and only if) we warn about fooBAR => ... when fooBAR is a const-item, then we should warn about fooBAR => ... when fooBAR is an enum-variant.

@nwin
Copy link
Contributor Author

nwin commented May 9, 2015

I think there are two things to agree on:

  1. What is the purpose of non_upper_case_globals? Warning about naming conventions or ambiguity?
  2. Do we want to warn about ambiguity (because of expectations) in this case?

If it is the purpose of non_upper_case_globals to warning about naming conventions this lint should not warn about this case because you cannot adhere to naming conventions on the consumer side.

On the other hand, if it’s purpose is to warn about ambiguity, and we want to warn about ambiguity in this case it should give a better hint (e.g. "use full qualifier").

Furthermore we would also have to warn about this case (because it is not a constant):

fn main() {
    println!("{}", match 23 {
        FOO_BAR => "yes",
        _ => "no"
    });
}

This is why I oppose. The consequence would would be that would also have to warn about every variable not following naming conventions.

@steveklabnik steveklabnik added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label May 10, 2015
@steveklabnik
Copy link
Member

Triage: no changes

@Mark-Simulacrum Mark-Simulacrum added C-enhancement Category: An issue proposing an enhancement or a PR with one. C-bug Category: This is a bug. and removed C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Jul 22, 2017
@steveklabnik
Copy link
Member

Triage: no change

@Lokathor
Copy link
Contributor

Lokathor commented Jun 3, 2019

This seems to have been filed a second time: #39371

I presume one of the two issues can be merged into the other.

@Spoonbender
Copy link

Triage: no change

@traviscross traviscross added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Dec 6, 2023
@traviscross
Copy link
Contributor

@rustbot labels +I-lang-nominated

Nominating this on behalf of @Lokathor. The main issue here seems to be that the person whose code is getting the warning is not necessarily well positioned to change the name, and the upstream who is well placed to change the name may have already silenced this warning but we still warn in the downstream code.

@rustbot rustbot added the I-lang-nominated Nominated for discussion during a lang team meeting. label Dec 6, 2023
@joshtriplett
Copy link
Member

joshtriplett commented Dec 6, 2023

We discussed this in today's @rust-lang/lang meeting.

We agreed that the current lint should not fire on uses, only on declarations. However, we also felt cases that are ambiguous between being a constant or a binding (e.g. bare names like xyzABC without a ::), we should have a different lint to flag those and suggest that they be made unambiguously either a binding or a constant.

A pattern could be made unambiguously a binding with an bound_ident @ _, or made unambiguously a constant by giving a path that includes a ::.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

9 participants