about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-04-19 06:35:36 +0200
committerGitHub <noreply@github.com>2023-04-19 06:35:36 +0200
commit78490ada7c987cc13b7029fa5929d0c51b4c2a98 (patch)
tree99f32aaa3372d28898a02095bb32d4977bd0d44c
parentfdd2f4bde8452e806288f7aa8d42f10bdb4369c0 (diff)
parent625619551d6e33ada03186d4b9ae0ab7f2c07add (diff)
downloadrust-78490ada7c987cc13b7029fa5929d0c51b4c2a98.tar.gz
rust-78490ada7c987cc13b7029fa5929d0c51b4c2a98.zip
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.
-rw-r--r--compiler/rustc_lint/src/nonstandard_style.rs15
-rw-r--r--tests/ui/lint/lint-non-uppercase-trait-assoc-const.rs15
-rw-r--r--tests/ui/lint/lint-non-uppercase-trait-assoc-const.stderr14
3 files changed, 38 insertions, 6 deletions
diff --git a/compiler/rustc_lint/src/nonstandard_style.rs b/compiler/rustc_lint/src/nonstandard_style.rs
index 9efc14849c7..648a7c6eed6 100644
--- a/compiler/rustc_lint/src/nonstandard_style.rs
+++ b/compiler/rustc_lint/src/nonstandard_style.rs
@@ -494,6 +494,15 @@ impl<'tcx> LateLintPass<'tcx> for NonUpperCaseGlobals {
             hir::ItemKind::Const(..) => {
                 NonUpperCaseGlobals::check_upper_case(cx, "constant", &it.ident);
             }
+            // we only want to check inherent associated consts, trait consts
+            // are linted at def-site.
+            hir::ItemKind::Impl(hir::Impl { of_trait: None, items, .. }) => {
+                for it in *items {
+                    if let hir::AssocItemKind::Const = it.kind {
+                        NonUpperCaseGlobals::check_upper_case(cx, "associated constant", &it.ident);
+                    }
+                }
+            }
             _ => {}
         }
     }
@@ -504,12 +513,6 @@ impl<'tcx> LateLintPass<'tcx> for NonUpperCaseGlobals {
         }
     }
 
-    fn check_impl_item(&mut self, cx: &LateContext<'_>, ii: &hir::ImplItem<'_>) {
-        if let hir::ImplItemKind::Const(..) = ii.kind {
-            NonUpperCaseGlobals::check_upper_case(cx, "associated constant", &ii.ident);
-        }
-    }
-
     fn check_pat(&mut self, cx: &LateContext<'_>, p: &hir::Pat<'_>) {
         // Lint for constants that look like binding identifiers (#7526)
         if let PatKind::Path(hir::QPath::Resolved(None, ref path)) = p.kind {
diff --git a/tests/ui/lint/lint-non-uppercase-trait-assoc-const.rs b/tests/ui/lint/lint-non-uppercase-trait-assoc-const.rs
new file mode 100644
index 00000000000..4409611c200
--- /dev/null
+++ b/tests/ui/lint/lint-non-uppercase-trait-assoc-const.rs
@@ -0,0 +1,15 @@
+#![deny(non_upper_case_globals)]
+
+trait Trait {
+    const item: usize;
+    //~^ ERROR associated constant `item` should have an upper case name [non_upper_case_globals]
+}
+
+struct Foo;
+
+impl Trait for Foo {
+    const item: usize = 5;
+    // ^^^ there should be no error here (in the trait `impl`)
+}
+
+fn main() {}
diff --git a/tests/ui/lint/lint-non-uppercase-trait-assoc-const.stderr b/tests/ui/lint/lint-non-uppercase-trait-assoc-const.stderr
new file mode 100644
index 00000000000..98d8d1dd27f
--- /dev/null
+++ b/tests/ui/lint/lint-non-uppercase-trait-assoc-const.stderr
@@ -0,0 +1,14 @@
+error: associated constant `item` should have an upper case name
+  --> $DIR/lint-non-uppercase-trait-assoc-const.rs:4:11
+   |
+LL |     const item: usize;
+   |           ^^^^ help: convert the identifier to upper case: `ITEM`
+   |
+note: the lint level is defined here
+  --> $DIR/lint-non-uppercase-trait-assoc-const.rs:1:9
+   |
+LL | #![deny(non_upper_case_globals)]
+   |         ^^^^^^^^^^^^^^^^^^^^^^
+
+error: aborting due to previous error
+