about summary refs log tree commit diff
diff options
context:
space:
mode:
authorUrgau <urgau@numericable.fr>2025-06-18 19:15:10 +0200
committerUrgau <urgau@numericable.fr>2025-06-18 19:15:10 +0200
commit4df9f2f8412db164e787233d1fc56d2988f255c8 (patch)
treeee4364184b77c5dfb794d89789d78b1bac1da41e
parent42bb66add31d196bb8d0f0bfb79a00dfd2cad55b (diff)
downloadrust-4df9f2f8412db164e787233d1fc56d2988f255c8.tar.gz
rust-4df9f2f8412db164e787233d1fc56d2988f255c8.zip
Emit the usages suggestions as tool-only suggestions
-rw-r--r--compiler/rustc_lint/src/lints.rs15
-rw-r--r--compiler/rustc_lint/src/nonstandard_style.rs24
-rw-r--r--tests/ui/lint/lint-non-uppercase-usages.stderr25
3 files changed, 28 insertions, 36 deletions
diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs
index d157bf6986c..a20050a10b4 100644
--- a/compiler/rustc_lint/src/lints.rs
+++ b/compiler/rustc_lint/src/lints.rs
@@ -1349,7 +1349,7 @@ pub(crate) struct NonUpperCaseGlobal<'a> {
     #[subdiagnostic]
     pub sub: NonUpperCaseGlobalSub,
     #[subdiagnostic]
-    pub usages: Vec<NonUpperCaseGlobalSub>,
+    pub usages: Vec<NonUpperCaseGlobalSubTool>,
 }
 
 #[derive(Subdiagnostic)]
@@ -1367,6 +1367,19 @@ pub(crate) enum NonUpperCaseGlobalSub {
     },
 }
 
+#[derive(Subdiagnostic)]
+#[suggestion(
+    lint_suggestion,
+    code = "{replace}",
+    applicability = "maybe-incorrect",
+    style = "tool-only"
+)]
+pub(crate) struct NonUpperCaseGlobalSubTool {
+    #[primary_span]
+    pub(crate) span: Span,
+    pub(crate) replace: String,
+}
+
 // noop_method_call.rs
 #[derive(LintDiagnostic)]
 #[diag(lint_noop_method_call)]
diff --git a/compiler/rustc_lint/src/nonstandard_style.rs b/compiler/rustc_lint/src/nonstandard_style.rs
index a5b3eb3f0ff..e92e8e06382 100644
--- a/compiler/rustc_lint/src/nonstandard_style.rs
+++ b/compiler/rustc_lint/src/nonstandard_style.rs
@@ -14,7 +14,7 @@ use {rustc_ast as ast, rustc_hir as hir};
 
 use crate::lints::{
     NonCamelCaseType, NonCamelCaseTypeSub, NonSnakeCaseDiag, NonSnakeCaseDiagSub,
-    NonUpperCaseGlobal, NonUpperCaseGlobalSub,
+    NonUpperCaseGlobal, NonUpperCaseGlobalSub, NonUpperCaseGlobalSubTool,
 };
 use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
 
@@ -497,12 +497,10 @@ impl NonUpperCaseGlobals {
 
             // We cannot provide meaningful suggestions
             // if the characters are in the category of "Lowercase Letter".
-            let sub = |span| {
-                if *name != uc {
-                    NonUpperCaseGlobalSub::Suggestion { span, replace: uc.clone() }
-                } else {
-                    NonUpperCaseGlobalSub::Label { span }
-                }
+            let sub = if *name != uc {
+                NonUpperCaseGlobalSub::Suggestion { span: ident.span, replace: uc.clone() }
+            } else {
+                NonUpperCaseGlobalSub::Label { span: ident.span }
             };
 
             struct UsageCollector<'a, 'tcx> {
@@ -531,10 +529,16 @@ impl NonUpperCaseGlobals {
                 }
             }
 
-            let usages = if let Some(did) = did {
+            let usages = if let Some(did) = did
+                && *name != uc
+            {
                 let mut usage_collector = UsageCollector { cx, did, collected: Vec::new() };
                 cx.tcx.hir_walk_toplevel_module(&mut usage_collector);
-                usage_collector.collected.into_iter().map(|span| sub(span)).collect()
+                usage_collector
+                    .collected
+                    .into_iter()
+                    .map(|span| NonUpperCaseGlobalSubTool { span, replace: uc.clone() })
+                    .collect()
             } else {
                 vec![]
             };
@@ -542,7 +546,7 @@ impl NonUpperCaseGlobals {
             cx.emit_span_lint(
                 NON_UPPER_CASE_GLOBALS,
                 ident.span,
-                NonUpperCaseGlobal { sort, name, sub: sub(ident.span), usages },
+                NonUpperCaseGlobal { sort, name, sub, usages },
             );
         }
     }
diff --git a/tests/ui/lint/lint-non-uppercase-usages.stderr b/tests/ui/lint/lint-non-uppercase-usages.stderr
index fa47b4ba6a8..34a0a5b0ca6 100644
--- a/tests/ui/lint/lint-non-uppercase-usages.stderr
+++ b/tests/ui/lint/lint-non-uppercase-usages.stderr
@@ -10,16 +10,6 @@ help: convert the identifier to upper case
 LL - const my_static: u32 = 0;
 LL + const MY_STATIC: u32 = 0;
    |
-help: convert the identifier to upper case
-   |
-LL - const LOL: u32 = my_static + 0;
-LL + const LOL: u32 = MY_STATIC + 0;
-   |
-help: convert the identifier to upper case
-   |
-LL -     let _a = crate::my_static;
-LL +     let _a = crate::MY_STATIC;
-   |
 
 warning: constant `fooFOO` should have an upper case name
   --> $DIR/lint-non-uppercase-usages.rs:19:12
@@ -32,16 +22,6 @@ help: convert the identifier to upper case
 LL -     static fooFOO: Cell<usize> = unreachable!();
 LL +     static FOO_FOO: Cell<usize> = unreachable!();
    |
-help: convert the identifier to upper case
-   |
-LL -     fooFOO.set(9);
-LL +     FOO_FOO.set(9);
-   |
-help: convert the identifier to upper case
-   |
-LL -     println!("{}", fooFOO.get());
-LL +     println!("{}", FOO_FOO.get());
-   |
 
 warning: const parameter `foo` should have an upper case name
   --> $DIR/lint-non-uppercase-usages.rs:24:14
@@ -54,11 +34,6 @@ help: convert the identifier to upper case (notice the capitalization difference
 LL - fn foo<const foo: u32>() {
 LL + fn foo<const FOO: u32>() {
    |
-help: convert the identifier to upper case (notice the capitalization difference)
-   |
-LL -     let _a = foo + 1;
-LL +     let _a = FOO + 1;
-   |
 
 warning: 3 warnings emitted