Skip to content

Commit 4df9f2f

Browse files
committed
Emit the usages suggestions as tool-only suggestions
1 parent 42bb66a commit 4df9f2f

File tree

3 files changed

+28
-36
lines changed

3 files changed

+28
-36
lines changed

compiler/rustc_lint/src/lints.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@ pub(crate) struct NonUpperCaseGlobal<'a> {
13491349
#[subdiagnostic]
13501350
pub sub: NonUpperCaseGlobalSub,
13511351
#[subdiagnostic]
1352-
pub usages: Vec<NonUpperCaseGlobalSub>,
1352+
pub usages: Vec<NonUpperCaseGlobalSubTool>,
13531353
}
13541354

13551355
#[derive(Subdiagnostic)]
@@ -1367,6 +1367,19 @@ pub(crate) enum NonUpperCaseGlobalSub {
13671367
},
13681368
}
13691369

1370+
#[derive(Subdiagnostic)]
1371+
#[suggestion(
1372+
lint_suggestion,
1373+
code = "{replace}",
1374+
applicability = "maybe-incorrect",
1375+
style = "tool-only"
1376+
)]
1377+
pub(crate) struct NonUpperCaseGlobalSubTool {
1378+
#[primary_span]
1379+
pub(crate) span: Span,
1380+
pub(crate) replace: String,
1381+
}
1382+
13701383
// noop_method_call.rs
13711384
#[derive(LintDiagnostic)]
13721385
#[diag(lint_noop_method_call)]

compiler/rustc_lint/src/nonstandard_style.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use {rustc_ast as ast, rustc_hir as hir};
1414

1515
use crate::lints::{
1616
NonCamelCaseType, NonCamelCaseTypeSub, NonSnakeCaseDiag, NonSnakeCaseDiagSub,
17-
NonUpperCaseGlobal, NonUpperCaseGlobalSub,
17+
NonUpperCaseGlobal, NonUpperCaseGlobalSub, NonUpperCaseGlobalSubTool,
1818
};
1919
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
2020

@@ -497,12 +497,10 @@ impl NonUpperCaseGlobals {
497497

498498
// We cannot provide meaningful suggestions
499499
// if the characters are in the category of "Lowercase Letter".
500-
let sub = |span| {
501-
if *name != uc {
502-
NonUpperCaseGlobalSub::Suggestion { span, replace: uc.clone() }
503-
} else {
504-
NonUpperCaseGlobalSub::Label { span }
505-
}
500+
let sub = if *name != uc {
501+
NonUpperCaseGlobalSub::Suggestion { span: ident.span, replace: uc.clone() }
502+
} else {
503+
NonUpperCaseGlobalSub::Label { span: ident.span }
506504
};
507505

508506
struct UsageCollector<'a, 'tcx> {
@@ -531,18 +529,24 @@ impl NonUpperCaseGlobals {
531529
}
532530
}
533531

534-
let usages = if let Some(did) = did {
532+
let usages = if let Some(did) = did
533+
&& *name != uc
534+
{
535535
let mut usage_collector = UsageCollector { cx, did, collected: Vec::new() };
536536
cx.tcx.hir_walk_toplevel_module(&mut usage_collector);
537-
usage_collector.collected.into_iter().map(|span| sub(span)).collect()
537+
usage_collector
538+
.collected
539+
.into_iter()
540+
.map(|span| NonUpperCaseGlobalSubTool { span, replace: uc.clone() })
541+
.collect()
538542
} else {
539543
vec![]
540544
};
541545

542546
cx.emit_span_lint(
543547
NON_UPPER_CASE_GLOBALS,
544548
ident.span,
545-
NonUpperCaseGlobal { sort, name, sub: sub(ident.span), usages },
549+
NonUpperCaseGlobal { sort, name, sub, usages },
546550
);
547551
}
548552
}

tests/ui/lint/lint-non-uppercase-usages.stderr

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,6 @@ help: convert the identifier to upper case
1010
LL - const my_static: u32 = 0;
1111
LL + const MY_STATIC: u32 = 0;
1212
|
13-
help: convert the identifier to upper case
14-
|
15-
LL - const LOL: u32 = my_static + 0;
16-
LL + const LOL: u32 = MY_STATIC + 0;
17-
|
18-
help: convert the identifier to upper case
19-
|
20-
LL - let _a = crate::my_static;
21-
LL + let _a = crate::MY_STATIC;
22-
|
2313

2414
warning: constant `fooFOO` should have an upper case name
2515
--> $DIR/lint-non-uppercase-usages.rs:19:12
@@ -32,16 +22,6 @@ help: convert the identifier to upper case
3222
LL - static fooFOO: Cell<usize> = unreachable!();
3323
LL + static FOO_FOO: Cell<usize> = unreachable!();
3424
|
35-
help: convert the identifier to upper case
36-
|
37-
LL - fooFOO.set(9);
38-
LL + FOO_FOO.set(9);
39-
|
40-
help: convert the identifier to upper case
41-
|
42-
LL - println!("{}", fooFOO.get());
43-
LL + println!("{}", FOO_FOO.get());
44-
|
4525

4626
warning: const parameter `foo` should have an upper case name
4727
--> $DIR/lint-non-uppercase-usages.rs:24:14
@@ -54,11 +34,6 @@ help: convert the identifier to upper case (notice the capitalization difference
5434
LL - fn foo<const foo: u32>() {
5535
LL + fn foo<const FOO: u32>() {
5636
|
57-
help: convert the identifier to upper case (notice the capitalization difference)
58-
|
59-
LL - let _a = foo + 1;
60-
LL + let _a = FOO + 1;
61-
|
6237

6338
warning: 3 warnings emitted
6439

0 commit comments

Comments
 (0)