Skip to content

Commit 17f95ca

Browse files
committed
Emit the usages suggestions as multipart suggestions
1 parent 42bb66a commit 17f95ca

File tree

5 files changed

+40
-30
lines changed

5 files changed

+40
-30
lines changed

compiler/rustc_lint/src/lints.rs

Lines changed: 18 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: Option<NonUpperCaseGlobalSubUsages>,
13531353
}
13541354

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

1370+
pub(crate) struct NonUpperCaseGlobalSubUsages {
1371+
pub(crate) spans: Vec<Span>,
1372+
pub(crate) replace: String,
1373+
}
1374+
1375+
impl Subdiagnostic for NonUpperCaseGlobalSubUsages {
1376+
fn add_to_diag<G: EmissionGuarantee>(self, diag: &mut Diag<'_, G>) {
1377+
if !self.spans.is_empty() {
1378+
diag.multipart_suggestion_verbose(
1379+
crate::fluent_generated::_subdiag::suggestion,
1380+
self.spans.into_iter().map(|sp| (sp, self.replace.clone())).collect(),
1381+
Applicability::MaybeIncorrect,
1382+
);
1383+
}
1384+
}
1385+
}
1386+
13701387
// noop_method_call.rs
13711388
#[derive(LintDiagnostic)]
13721389
#[diag(lint_noop_method_call)]

compiler/rustc_lint/src/nonstandard_style.rs

Lines changed: 14 additions & 11 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, NonUpperCaseGlobalSubUsages,
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,23 @@ 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+
Some(NonUpperCaseGlobalSubUsages {
538+
spans: usage_collector.collected,
539+
replace: uc.clone(),
540+
})
538541
} else {
539-
vec![]
542+
None
540543
};
541544

542545
cx.emit_span_lint(
543546
NON_UPPER_CASE_GLOBALS,
544547
ident.span,
545-
NonUpperCaseGlobal { sort, name, sub: sub(ident.span), usages },
548+
NonUpperCaseGlobal { sort, name, sub, usages },
546549
);
547550
}
548551
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ fn foo<const FOO: u32>() {
3030

3131
fn main() {
3232
let _a = crate::MY_STATIC;
33-
//~^ SUGGESTION MY_STATIC
3433

3534
FOO_FOO.set(9);
3635
//~^ SUGGESTION FOO_FOO
3736
println!("{}", FOO_FOO.get());
38-
//~^ SUGGESTION FOO_FOO
3937
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ fn foo<const foo: u32>() {
3030

3131
fn main() {
3232
let _a = crate::my_static;
33-
//~^ SUGGESTION MY_STATIC
3433

3534
fooFOO.set(9);
3635
//~^ SUGGESTION FOO_FOO
3736
println!("{}", fooFOO.get());
38-
//~^ SUGGESTION FOO_FOO
3937
}

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ LL + const MY_STATIC: u32 = 0;
1212
|
1313
help: convert the identifier to upper case
1414
|
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;
15+
LL ~ const LOL: u32 = MY_STATIC + 0;
16+
LL |
17+
...
18+
LL | fn main() {
19+
LL ~ let _a = crate::MY_STATIC;
2220
|
2321

2422
warning: constant `fooFOO` should have an upper case name
@@ -34,13 +32,9 @@ LL + static FOO_FOO: Cell<usize> = unreachable!();
3432
|
3533
help: convert the identifier to upper case
3634
|
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());
35+
LL ~ FOO_FOO.set(9);
36+
LL |
37+
LL ~ println!("{}", FOO_FOO.get());
4438
|
4539

4640
warning: const parameter `foo` should have an upper case name

0 commit comments

Comments
 (0)