diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index eb82afab13186..d2bb70b07f8c8 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -2728,7 +2728,7 @@ pub(crate) enum MutRefSugg { #[derive(Subdiagnostic)] #[suggestion( - "this type already provides \"interior mutability\", so its binding doesn't need to be declared as mutable", + "this type already provides \"interior mutability\", so its binding doesn't need to be declared as mutable when borrowed with a shared reference", style = "verbose", applicability = "maybe-incorrect", code = "" diff --git a/compiler/rustc_lint/src/static_mut_refs.rs b/compiler/rustc_lint/src/static_mut_refs.rs index 67e83d0652c3d..0dbed4ed1c0da 100644 --- a/compiler/rustc_lint/src/static_mut_refs.rs +++ b/compiler/rustc_lint/src/static_mut_refs.rs @@ -184,7 +184,7 @@ fn emit_static_mut_refs( }; let (interior_mutability_help, interior_mutability_sugg) = - interior_mutability_suggestion(cx, def_id); + interior_mutability_suggestion(cx, def_id, mut_note, suggest_addr_of); cx.emit_span_lint( STATIC_MUT_REFS, @@ -208,17 +208,23 @@ fn emit_static_mut_refs( fn interior_mutability_suggestion( cx: &LateContext<'_>, def_id: DefId, + mut_ref: bool, + suggest_addr_of: bool, ) -> (bool, Option) { let static_ty = cx.tcx.type_of(def_id).skip_binder(); let has_interior_mutability = !static_ty.is_freeze(cx.tcx, cx.typing_env()); if !has_interior_mutability { + return (!suggest_addr_of, None); + } + + if mut_ref { return (false, None); } let sugg = static_mutability_span(cx, def_id).map(|span| StaticMutRefsInteriorMutabilitySugg { span }); - (sugg.is_none(), sugg) + (false, sugg) } fn static_mutability_span(cx: &LateContext<'_>, def_id: DefId) -> Option { diff --git a/tests/ui/lint/static-mut-refs-interior-mutability-no-sugg.stderr b/tests/ui/lint/static-mut-refs-interior-mutability-no-sugg.stderr index 5ce69e1a14d83..e5db09a670cc9 100644 --- a/tests/ui/lint/static-mut-refs-interior-mutability-no-sugg.stderr +++ b/tests/ui/lint/static-mut-refs-interior-mutability-no-sugg.stderr @@ -5,7 +5,6 @@ LL | let _lock = unsafe { MACRO_MUTEX.lock().unwrap() }; | ^^^^^^^^^^^^^^^^^^ shared reference to mutable static | = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives - = help: use a type that relies on "interior mutability" instead; to read more on this, visit = note: for more information, see = note: `#[deny(static_mut_refs)]` (part of `#[deny(rust_2024_compatibility)]`) on by default diff --git a/tests/ui/lint/static-mut-refs-interior-mutability.stderr b/tests/ui/lint/static-mut-refs-interior-mutability.stderr index 29ab5a5c404de..d6a644374d8a2 100644 --- a/tests/ui/lint/static-mut-refs-interior-mutability.stderr +++ b/tests/ui/lint/static-mut-refs-interior-mutability.stderr @@ -7,7 +7,7 @@ LL | let _lock = unsafe { STDINOUT_MUTEX.lock().unwrap() }; = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives = note: for more information, see = note: `#[deny(static_mut_refs)]` (part of `#[deny(rust_2024_compatibility)]`) on by default -help: this type already provides "interior mutability", so its binding doesn't need to be declared as mutable +help: this type already provides "interior mutability", so its binding doesn't need to be declared as mutable when borrowed with a shared reference | LL - static mut STDINOUT_MUTEX: Mutex = Mutex::new(false); LL + static STDINOUT_MUTEX: Mutex = Mutex::new(false); diff --git a/tests/ui/lint/static-mut-refs.e2021.stderr b/tests/ui/lint/static-mut-refs.e2021.stderr index 56b4ad239afe3..e616ba0aa4b28 100644 --- a/tests/ui/lint/static-mut-refs.e2021.stderr +++ b/tests/ui/lint/static-mut-refs.e2021.stderr @@ -32,6 +32,7 @@ LL | let ref _a = X; | ^ shared reference to mutable static | = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives + = help: use a type that relies on "interior mutability" instead; to read more on this, visit = note: for more information, see warning: creating a shared reference to mutable static @@ -80,6 +81,7 @@ LL | let _ = Z.len(); | ^^^^^^^ shared reference to mutable static | = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives + = help: use a type that relies on "interior mutability" instead; to read more on this, visit = note: for more information, see warning: creating a shared reference to mutable static @@ -89,6 +91,7 @@ LL | let _ = format!("{:?}", Z); | ^ shared reference to mutable static | = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives + = help: use a type that relies on "interior mutability" instead; to read more on this, visit = note: for more information, see warning: creating a shared reference to mutable static @@ -124,6 +127,7 @@ LL | let ref _v = A.value; | ^^^^^^^ shared reference to mutable static | = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives + = help: use a type that relies on "interior mutability" instead; to read more on this, visit = note: for more information, see warning: creating a mutable reference to mutable static @@ -136,6 +140,7 @@ LL | let _x = bar!(FOO); | --------- in this macro invocation | = note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives + = help: use a type that relies on "interior mutability" instead; to read more on this, visit = note: for more information, see = note: this warning originates in the macro `bar` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/tests/ui/lint/static-mut-refs.e2024.stderr b/tests/ui/lint/static-mut-refs.e2024.stderr index 0b7f48a507c94..a8985fc8a1763 100644 --- a/tests/ui/lint/static-mut-refs.e2024.stderr +++ b/tests/ui/lint/static-mut-refs.e2024.stderr @@ -32,6 +32,7 @@ LL | let ref _a = X; | ^ shared reference to mutable static | = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives + = help: use a type that relies on "interior mutability" instead; to read more on this, visit = note: for more information, see error: creating a shared reference to mutable static @@ -80,6 +81,7 @@ LL | let _ = Z.len(); | ^^^^^^^ shared reference to mutable static | = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives + = help: use a type that relies on "interior mutability" instead; to read more on this, visit = note: for more information, see error: creating a shared reference to mutable static @@ -89,6 +91,7 @@ LL | let _ = format!("{:?}", Z); | ^ shared reference to mutable static | = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives + = help: use a type that relies on "interior mutability" instead; to read more on this, visit = note: for more information, see error: creating a shared reference to mutable static @@ -124,6 +127,7 @@ LL | let ref _v = A.value; | ^^^^^^^ shared reference to mutable static | = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives + = help: use a type that relies on "interior mutability" instead; to read more on this, visit = note: for more information, see error: creating a mutable reference to mutable static @@ -136,6 +140,7 @@ LL | let _x = bar!(FOO); | --------- in this macro invocation | = note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives + = help: use a type that relies on "interior mutability" instead; to read more on this, visit = note: for more information, see = note: this error originates in the macro `bar` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/tests/ui/statics/static-lazy-init-with-arena-set.stderr b/tests/ui/statics/static-lazy-init-with-arena-set.stderr index 43b244607885d..4aed8b06166c8 100644 --- a/tests/ui/statics/static-lazy-init-with-arena-set.stderr +++ b/tests/ui/statics/static-lazy-init-with-arena-set.stderr @@ -12,7 +12,7 @@ LL | | }); = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives = note: for more information, see = note: `#[warn(static_mut_refs)]` (part of `#[warn(rust_2024_compatibility)]`) on by default -help: this type already provides "interior mutability", so its binding doesn't need to be declared as mutable +help: this type already provides "interior mutability", so its binding doesn't need to be declared as mutable when borrowed with a shared reference | LL - static mut ONCE: Once = Once::new(); LL + static ONCE: Once = Once::new(); diff --git a/tests/ui/statics/static-mut-xc.stderr b/tests/ui/statics/static-mut-xc.stderr index d0b30ce6f8514..f8fa25e152f67 100644 --- a/tests/ui/statics/static-mut-xc.stderr +++ b/tests/ui/statics/static-mut-xc.stderr @@ -5,6 +5,7 @@ LL | assert_eq!(static_mut_xc::a, 3); | ^^^^^^^^^^^^^^^^ shared reference to mutable static | = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives + = help: use a type that relies on "interior mutability" instead; to read more on this, visit = note: for more information, see = note: `#[warn(static_mut_refs)]` (part of `#[warn(rust_2024_compatibility)]`) on by default @@ -15,6 +16,7 @@ LL | assert_eq!(static_mut_xc::a, 4); | ^^^^^^^^^^^^^^^^ shared reference to mutable static | = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives + = help: use a type that relies on "interior mutability" instead; to read more on this, visit = note: for more information, see warning: creating a shared reference to mutable static @@ -24,6 +26,7 @@ LL | assert_eq!(static_mut_xc::a, 5); | ^^^^^^^^^^^^^^^^ shared reference to mutable static | = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives + = help: use a type that relies on "interior mutability" instead; to read more on this, visit = note: for more information, see warning: creating a shared reference to mutable static @@ -33,6 +36,7 @@ LL | assert_eq!(static_mut_xc::a, 15); | ^^^^^^^^^^^^^^^^ shared reference to mutable static | = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives + = help: use a type that relies on "interior mutability" instead; to read more on this, visit = note: for more information, see warning: creating a shared reference to mutable static @@ -42,6 +46,7 @@ LL | assert_eq!(static_mut_xc::a, -3); | ^^^^^^^^^^^^^^^^ shared reference to mutable static | = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives + = help: use a type that relies on "interior mutability" instead; to read more on this, visit = note: for more information, see warning: creating a shared reference to mutable static diff --git a/tests/ui/statics/static-recursive.stderr b/tests/ui/statics/static-recursive.stderr index 1ae96dfd5a832..9578808ec7b03 100644 --- a/tests/ui/statics/static-recursive.stderr +++ b/tests/ui/statics/static-recursive.stderr @@ -19,6 +19,7 @@ LL | assert_eq!(S, *(S as *const *const u8)); | ^ shared reference to mutable static | = note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives + = help: use a type that relies on "interior mutability" instead; to read more on this, visit = note: for more information, see warning: 2 warnings emitted