diff options
| -rw-r--r-- | compiler/rustc_lint/src/lints.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_lint/src/nonstandard_style.rs | 36 | ||||
| -rw-r--r-- | tests/ui/lint/lint-non-uppercase-usages.fixed | 5 | ||||
| -rw-r--r-- | tests/ui/lint/lint-non-uppercase-usages.rs | 5 | ||||
| -rw-r--r-- | tests/ui/lint/lint-non-uppercase-usages.stderr | 4 |
5 files changed, 43 insertions, 11 deletions
diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index 0ef7b3c14ac..eebb9339e24 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -1359,10 +1359,12 @@ pub(crate) enum NonUpperCaseGlobalSub { #[primary_span] span: Span, }, - #[suggestion(lint_suggestion, code = "{replace}", applicability = "machine-applicable")] + #[suggestion(lint_suggestion, code = "{replace}")] Suggestion { #[primary_span] span: Span, + #[applicability] + applicability: Applicability, replace: String, }, } diff --git a/compiler/rustc_lint/src/nonstandard_style.rs b/compiler/rustc_lint/src/nonstandard_style.rs index c325b7a95c9..a42a6076fc3 100644 --- a/compiler/rustc_lint/src/nonstandard_style.rs +++ b/compiler/rustc_lint/src/nonstandard_style.rs @@ -1,7 +1,9 @@ use rustc_abi::ExternAbi; use rustc_attr_data_structures::{AttributeKind, ReprAttr}; use rustc_attr_parsing::AttributeParser; +use rustc_errors::Applicability; use rustc_hir::def::{DefKind, Res}; +use rustc_hir::def_id::DefId; use rustc_hir::intravisit::{FnKind, Visitor}; use rustc_hir::{AttrArgs, AttrItem, Attribute, GenericParamKind, PatExprKind, PatKind}; use rustc_middle::hir::nested_filter::All; @@ -495,17 +497,33 @@ impl NonUpperCaseGlobals { if name.chars().any(|c| c.is_lowercase()) { let uc = NonSnakeCase::to_snake_case(name).to_uppercase(); + // If the item is exported, suggesting changing it's name would be breaking-change + // and could break users without a "nice" applicable fix, so let's avoid it. + let can_change_usages = if let Some(did) = did { + !cx.tcx.effective_visibilities(()).is_exported(did) + } else { + false + }; + // We cannot provide meaningful suggestions // if the characters are in the category of "Lowercase Letter". let sub = if *name != uc { - NonUpperCaseGlobalSub::Suggestion { span: ident.span, replace: uc.clone() } + NonUpperCaseGlobalSub::Suggestion { + span: ident.span, + replace: uc.clone(), + applicability: if can_change_usages { + Applicability::MachineApplicable + } else { + Applicability::MaybeIncorrect + }, + } } else { NonUpperCaseGlobalSub::Label { span: ident.span } }; struct UsageCollector<'a, 'tcx> { cx: &'tcx LateContext<'a>, - did: LocalDefId, + did: DefId, collected: Vec<Span>, } @@ -521,10 +539,10 @@ impl NonUpperCaseGlobals { path: &rustc_hir::Path<'v>, _id: rustc_hir::HirId, ) -> Self::Result { - for seg in path.segments { - if seg.res.opt_def_id() == Some(self.did.to_def_id()) { - self.collected.push(seg.ident.span); - } + if let Some(final_seg) = path.segments.last() + && final_seg.res.opt_def_id() == Some(self.did) + { + self.collected.push(final_seg.ident.span); } } } @@ -532,10 +550,12 @@ impl NonUpperCaseGlobals { cx.emit_span_lint_lazy(NON_UPPER_CASE_GLOBALS, ident.span, || { // Compute usages lazily as it can expansive and useless when the lint is allowed. // cf. https://github.com/rust-lang/rust/pull/142645#issuecomment-2993024625 - let usages = if let Some(did) = did + let usages = if can_change_usages && *name != uc + && let Some(did) = did { - let mut usage_collector = UsageCollector { cx, did, collected: Vec::new() }; + let mut usage_collector = + UsageCollector { cx, did: did.to_def_id(), collected: Vec::new() }; cx.tcx.hir_walk_toplevel_module(&mut usage_collector); usage_collector .collected diff --git a/tests/ui/lint/lint-non-uppercase-usages.fixed b/tests/ui/lint/lint-non-uppercase-usages.fixed index 048a936ff27..231991dcae0 100644 --- a/tests/ui/lint/lint-non-uppercase-usages.fixed +++ b/tests/ui/lint/lint-non-uppercase-usages.fixed @@ -15,6 +15,11 @@ const MY_STATIC: u32 = 0; const LOL: u32 = MY_STATIC + 0; //~^ SUGGESTION MY_STATIC +mod my_mod { + const INSIDE_MOD: u32 = super::MY_STATIC + 0; + //~^ SUGGESTION MY_STATIC +} + thread_local! { static FOO_FOO: Cell<usize> = unreachable!(); //~^ WARN constant `fooFOO` should have an upper case name diff --git a/tests/ui/lint/lint-non-uppercase-usages.rs b/tests/ui/lint/lint-non-uppercase-usages.rs index b5b9ffac6bc..9cdf5e47003 100644 --- a/tests/ui/lint/lint-non-uppercase-usages.rs +++ b/tests/ui/lint/lint-non-uppercase-usages.rs @@ -15,6 +15,11 @@ const my_static: u32 = 0; const LOL: u32 = my_static + 0; //~^ SUGGESTION MY_STATIC +mod my_mod { + const INSIDE_MOD: u32 = super::my_static + 0; + //~^ SUGGESTION MY_STATIC +} + thread_local! { static fooFOO: Cell<usize> = unreachable!(); //~^ WARN constant `fooFOO` should have an upper case name diff --git a/tests/ui/lint/lint-non-uppercase-usages.stderr b/tests/ui/lint/lint-non-uppercase-usages.stderr index 34a0a5b0ca6..7c7e573a88e 100644 --- a/tests/ui/lint/lint-non-uppercase-usages.stderr +++ b/tests/ui/lint/lint-non-uppercase-usages.stderr @@ -12,7 +12,7 @@ LL + const MY_STATIC: u32 = 0; | warning: constant `fooFOO` should have an upper case name - --> $DIR/lint-non-uppercase-usages.rs:19:12 + --> $DIR/lint-non-uppercase-usages.rs:24:12 | LL | static fooFOO: Cell<usize> = unreachable!(); | ^^^^^^ @@ -24,7 +24,7 @@ LL + static FOO_FOO: Cell<usize> = unreachable!(); | warning: const parameter `foo` should have an upper case name - --> $DIR/lint-non-uppercase-usages.rs:24:14 + --> $DIR/lint-non-uppercase-usages.rs:29:14 | LL | fn foo<const foo: u32>() { | ^^^ |
