about summary refs log tree commit diff
path: root/compiler/rustc_lint/src/nonstandard_style.rs
diff options
context:
space:
mode:
authorUrgau <urgau@numericable.fr>2025-06-21 22:07:36 +0200
committerUrgau <urgau@numericable.fr>2025-06-22 16:45:16 +0200
commit6ffd0e6c235f9a28f724ce21bda7a9525a4dac03 (patch)
tree4525ab827f6d7a88774c7a8088ab9f0a8a57b7cb /compiler/rustc_lint/src/nonstandard_style.rs
parent1b5ec3fa1d81c1e469e5bc1c29fa2bc0452697c3 (diff)
downloadrust-6ffd0e6c235f9a28f724ce21bda7a9525a4dac03.tar.gz
rust-6ffd0e6c235f9a28f724ce21bda7a9525a4dac03.zip
Address review comments
Diffstat (limited to 'compiler/rustc_lint/src/nonstandard_style.rs')
-rw-r--r--compiler/rustc_lint/src/nonstandard_style.rs36
1 files changed, 28 insertions, 8 deletions
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