Skip to content

Commit 78490ad

Browse files
authored
Rollup merge of #110513 - Ezrashaw:fix-trait-const-name-lint, r=compiler-errors
make `non_upper_case_globals` lint not report trait impls We should not lint on trait `impl`s for `non_upper_case_globals`; the user doesn't have control over the name. This brings `non_upper_case_globals` into consistency with other `nonstandard_style` lints.
2 parents fdd2f4b + 6256195 commit 78490ad

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

compiler/rustc_lint/src/nonstandard_style.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,15 @@ impl<'tcx> LateLintPass<'tcx> for NonUpperCaseGlobals {
494494
hir::ItemKind::Const(..) => {
495495
NonUpperCaseGlobals::check_upper_case(cx, "constant", &it.ident);
496496
}
497+
// we only want to check inherent associated consts, trait consts
498+
// are linted at def-site.
499+
hir::ItemKind::Impl(hir::Impl { of_trait: None, items, .. }) => {
500+
for it in *items {
501+
if let hir::AssocItemKind::Const = it.kind {
502+
NonUpperCaseGlobals::check_upper_case(cx, "associated constant", &it.ident);
503+
}
504+
}
505+
}
497506
_ => {}
498507
}
499508
}
@@ -504,12 +513,6 @@ impl<'tcx> LateLintPass<'tcx> for NonUpperCaseGlobals {
504513
}
505514
}
506515

507-
fn check_impl_item(&mut self, cx: &LateContext<'_>, ii: &hir::ImplItem<'_>) {
508-
if let hir::ImplItemKind::Const(..) = ii.kind {
509-
NonUpperCaseGlobals::check_upper_case(cx, "associated constant", &ii.ident);
510-
}
511-
}
512-
513516
fn check_pat(&mut self, cx: &LateContext<'_>, p: &hir::Pat<'_>) {
514517
// Lint for constants that look like binding identifiers (#7526)
515518
if let PatKind::Path(hir::QPath::Resolved(None, ref path)) = p.kind {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![deny(non_upper_case_globals)]
2+
3+
trait Trait {
4+
const item: usize;
5+
//~^ ERROR associated constant `item` should have an upper case name [non_upper_case_globals]
6+
}
7+
8+
struct Foo;
9+
10+
impl Trait for Foo {
11+
const item: usize = 5;
12+
// ^^^ there should be no error here (in the trait `impl`)
13+
}
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: associated constant `item` should have an upper case name
2+
--> $DIR/lint-non-uppercase-trait-assoc-const.rs:4:11
3+
|
4+
LL | const item: usize;
5+
| ^^^^ help: convert the identifier to upper case: `ITEM`
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/lint-non-uppercase-trait-assoc-const.rs:1:9
9+
|
10+
LL | #![deny(non_upper_case_globals)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)