Emit the usages suggestions as tool-only suggestions

This commit is contained in:
Urgau 2025-06-18 19:15:10 +02:00
parent 42bb66add3
commit 4df9f2f841
3 changed files with 28 additions and 36 deletions

View file

@ -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)]

View file

@ -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 },
);
}
}

View file

@ -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