From 4df9f2f8412db164e787233d1fc56d2988f255c8 Mon Sep 17 00:00:00 2001 From: Urgau Date: Wed, 18 Jun 2025 19:15:10 +0200 Subject: [PATCH] Emit the usages suggestions as tool-only suggestions --- compiler/rustc_lint/src/lints.rs | 15 ++++++++++- compiler/rustc_lint/src/nonstandard_style.rs | 24 ++++++++++-------- .../ui/lint/lint-non-uppercase-usages.stderr | 25 ------------------- 3 files changed, 28 insertions(+), 36 deletions(-) diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index d157bf6986ce..a20050a10b48 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, + pub usages: Vec, } #[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 a5b3eb3f0ffb..e92e8e063829 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 fa47b4ba6a8f..34a0a5b0ca68 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 = unreachable!(); LL + static FOO_FOO: Cell = 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() { LL + fn foo() { | -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