Skip to content

Commit 6ffd0e6

Browse files
committed
Address review comments
1 parent 1b5ec3f commit 6ffd0e6

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed

compiler/rustc_lint/src/lints.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1359,10 +1359,12 @@ pub(crate) enum NonUpperCaseGlobalSub {
13591359
#[primary_span]
13601360
span: Span,
13611361
},
1362-
#[suggestion(lint_suggestion, code = "{replace}", applicability = "machine-applicable")]
1362+
#[suggestion(lint_suggestion, code = "{replace}")]
13631363
Suggestion {
13641364
#[primary_span]
13651365
span: Span,
1366+
#[applicability]
1367+
applicability: Applicability,
13661368
replace: String,
13671369
},
13681370
}

compiler/rustc_lint/src/nonstandard_style.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use rustc_abi::ExternAbi;
22
use rustc_attr_data_structures::{AttributeKind, ReprAttr};
33
use rustc_attr_parsing::AttributeParser;
4+
use rustc_errors::Applicability;
45
use rustc_hir::def::{DefKind, Res};
6+
use rustc_hir::def_id::DefId;
57
use rustc_hir::intravisit::{FnKind, Visitor};
68
use rustc_hir::{AttrArgs, AttrItem, Attribute, GenericParamKind, PatExprKind, PatKind};
79
use rustc_middle::hir::nested_filter::All;
@@ -495,17 +497,33 @@ impl NonUpperCaseGlobals {
495497
if name.chars().any(|c| c.is_lowercase()) {
496498
let uc = NonSnakeCase::to_snake_case(name).to_uppercase();
497499

500+
// If the item is exported, suggesting changing it's name would be breaking-change
501+
// and could break users without a "nice" applicable fix, so let's avoid it.
502+
let can_change_usages = if let Some(did) = did {
503+
!cx.tcx.effective_visibilities(()).is_exported(did)
504+
} else {
505+
false
506+
};
507+
498508
// We cannot provide meaningful suggestions
499509
// if the characters are in the category of "Lowercase Letter".
500510
let sub = if *name != uc {
501-
NonUpperCaseGlobalSub::Suggestion { span: ident.span, replace: uc.clone() }
511+
NonUpperCaseGlobalSub::Suggestion {
512+
span: ident.span,
513+
replace: uc.clone(),
514+
applicability: if can_change_usages {
515+
Applicability::MachineApplicable
516+
} else {
517+
Applicability::MaybeIncorrect
518+
},
519+
}
502520
} else {
503521
NonUpperCaseGlobalSub::Label { span: ident.span }
504522
};
505523

506524
struct UsageCollector<'a, 'tcx> {
507525
cx: &'tcx LateContext<'a>,
508-
did: LocalDefId,
526+
did: DefId,
509527
collected: Vec<Span>,
510528
}
511529

@@ -521,21 +539,23 @@ impl NonUpperCaseGlobals {
521539
path: &rustc_hir::Path<'v>,
522540
_id: rustc_hir::HirId,
523541
) -> Self::Result {
524-
for seg in path.segments {
525-
if seg.res.opt_def_id() == Some(self.did.to_def_id()) {
526-
self.collected.push(seg.ident.span);
527-
}
542+
if let Some(final_seg) = path.segments.last()
543+
&& final_seg.res.opt_def_id() == Some(self.did)
544+
{
545+
self.collected.push(final_seg.ident.span);
528546
}
529547
}
530548
}
531549

532550
cx.emit_span_lint_lazy(NON_UPPER_CASE_GLOBALS, ident.span, || {
533551
// Compute usages lazily as it can expansive and useless when the lint is allowed.
534552
// cf. https://github.com/rust-lang/rust/pull/142645#issuecomment-2993024625
535-
let usages = if let Some(did) = did
553+
let usages = if can_change_usages
536554
&& *name != uc
555+
&& let Some(did) = did
537556
{
538-
let mut usage_collector = UsageCollector { cx, did, collected: Vec::new() };
557+
let mut usage_collector =
558+
UsageCollector { cx, did: did.to_def_id(), collected: Vec::new() };
539559
cx.tcx.hir_walk_toplevel_module(&mut usage_collector);
540560
usage_collector
541561
.collected

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ const MY_STATIC: u32 = 0;
1515
const LOL: u32 = MY_STATIC + 0;
1616
//~^ SUGGESTION MY_STATIC
1717

18+
mod my_mod {
19+
const INSIDE_MOD: u32 = super::MY_STATIC + 0;
20+
//~^ SUGGESTION MY_STATIC
21+
}
22+
1823
thread_local! {
1924
static FOO_FOO: Cell<usize> = unreachable!();
2025
//~^ WARN constant `fooFOO` should have an upper case name

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ const my_static: u32 = 0;
1515
const LOL: u32 = my_static + 0;
1616
//~^ SUGGESTION MY_STATIC
1717

18+
mod my_mod {
19+
const INSIDE_MOD: u32 = super::my_static + 0;
20+
//~^ SUGGESTION MY_STATIC
21+
}
22+
1823
thread_local! {
1924
static fooFOO: Cell<usize> = unreachable!();
2025
//~^ WARN constant `fooFOO` should have an upper case name

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL + const MY_STATIC: u32 = 0;
1212
|
1313

1414
warning: constant `fooFOO` should have an upper case name
15-
--> $DIR/lint-non-uppercase-usages.rs:19:12
15+
--> $DIR/lint-non-uppercase-usages.rs:24:12
1616
|
1717
LL | static fooFOO: Cell<usize> = unreachable!();
1818
| ^^^^^^
@@ -24,7 +24,7 @@ LL + static FOO_FOO: Cell<usize> = unreachable!();
2424
|
2525

2626
warning: const parameter `foo` should have an upper case name
27-
--> $DIR/lint-non-uppercase-usages.rs:24:14
27+
--> $DIR/lint-non-uppercase-usages.rs:29:14
2828
|
2929
LL | fn foo<const foo: u32>() {
3030
| ^^^

0 commit comments

Comments
 (0)