diff --git a/library/alloc/src/collections/linked_list/tests.rs b/library/alloc/src/collections/linked_list/tests.rs index 9b3c9ac5ce52e..c93e5813b1182 100644 --- a/library/alloc/src/collections/linked_list/tests.rs +++ b/library/alloc/src/collections/linked_list/tests.rs @@ -1,3 +1,6 @@ +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + use std::panic::{catch_unwind, AssertUnwindSafe}; use std::thread; diff --git a/library/alloc/src/collections/vec_deque/tests.rs b/library/alloc/src/collections/vec_deque/tests.rs index f8ce4ca97884e..c90679f179775 100644 --- a/library/alloc/src/collections/vec_deque/tests.rs +++ b/library/alloc/src/collections/vec_deque/tests.rs @@ -1,3 +1,6 @@ +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + use core::iter::TrustedLen; use super::*; diff --git a/library/alloc/tests/fmt.rs b/library/alloc/tests/fmt.rs index ce24a40f4c051..c13074c53b73d 100644 --- a/library/alloc/tests/fmt.rs +++ b/library/alloc/tests/fmt.rs @@ -1,4 +1,6 @@ #![deny(warnings)] +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] use std::cell::RefCell; use std::fmt::{self, Write}; diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs index 3722fb06a6a8a..cf2ca4f0d657c 100644 --- a/library/alloc/tests/vec.rs +++ b/library/alloc/tests/vec.rs @@ -1,3 +1,6 @@ +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + use core::alloc::{Allocator, Layout}; use core::num::NonZero; use core::ptr::NonNull; @@ -1284,6 +1287,8 @@ fn test_from_iter_specialization_panic_during_iteration_drops() { #[test] #[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")] +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#[cfg_attr(not(bootstrap), allow(static_mut_refs))] fn test_from_iter_specialization_panic_during_drop_doesnt_leak() { static mut DROP_COUNTER_OLD: [usize; 5] = [0; 5]; static mut DROP_COUNTER_NEW: [usize; 2] = [0; 2]; diff --git a/library/alloc/tests/vec_deque.rs b/library/alloc/tests/vec_deque.rs index f32ba8d5aa461..0891d99fe666b 100644 --- a/library/alloc/tests/vec_deque.rs +++ b/library/alloc/tests/vec_deque.rs @@ -1,3 +1,6 @@ +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + use core::num::NonZero; use std::assert_matches::assert_matches; use std::collections::vec_deque::Drain; diff --git a/library/core/tests/atomic.rs b/library/core/tests/atomic.rs index 0d1c72a689291..2bdaeb3845a72 100644 --- a/library/core/tests/atomic.rs +++ b/library/core/tests/atomic.rs @@ -228,6 +228,8 @@ fn static_init() { } #[test] +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#[cfg_attr(not(bootstrap), allow(static_mut_refs))] fn atomic_access_bool() { static mut ATOMIC: AtomicBool = AtomicBool::new(false); diff --git a/library/panic_unwind/src/seh.rs b/library/panic_unwind/src/seh.rs index 070c11926f6e0..9e74a45a0e26e 100644 --- a/library/panic_unwind/src/seh.rs +++ b/library/panic_unwind/src/seh.rs @@ -291,6 +291,8 @@ cfg_if::cfg_if! { } } +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#[allow(static_mut_refs)] pub unsafe fn panic(data: Box) -> u32 { use core::intrinsics::atomic_store_seqcst; diff --git a/library/std/src/sync/mod.rs b/library/std/src/sync/mod.rs index d0ba8cc3b47df..70b419a1e33d1 100644 --- a/library/std/src/sync/mod.rs +++ b/library/std/src/sync/mod.rs @@ -9,6 +9,9 @@ //! Consider the following code, operating on some global static variables: //! //! ```rust +//! // FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +//! #![allow(static_mut_refs)] +//! //! static mut A: u32 = 0; //! static mut B: u32 = 0; //! static mut C: u32 = 0; diff --git a/library/std/src/sys/alloc/wasm.rs b/library/std/src/sys/alloc/wasm.rs index ef9d753d7f86c..a308fafc68b15 100644 --- a/library/std/src/sys/alloc/wasm.rs +++ b/library/std/src/sys/alloc/wasm.rs @@ -16,6 +16,9 @@ //! The crate itself provides a global allocator which on wasm has no //! synchronization as there are no threads! +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + use crate::alloc::{GlobalAlloc, Layout, System}; static mut DLMALLOC: dlmalloc::Dlmalloc = dlmalloc::Dlmalloc::new(); diff --git a/library/std/src/thread/local/tests.rs b/library/std/src/thread/local/tests.rs index 25019b554bb6a..6abb9b85a2e29 100644 --- a/library/std/src/thread/local/tests.rs +++ b/library/std/src/thread/local/tests.rs @@ -103,6 +103,9 @@ fn smoke_dtor() { #[test] fn circular() { + // FIXME(static_mut_refs): Do not allow `static_mut_refs` lint + #![allow(static_mut_refs)] + struct S1(&'static LocalKey>>, &'static LocalKey>>); struct S2(&'static LocalKey>>, &'static LocalKey>>); thread_local!(static K1: UnsafeCell> = UnsafeCell::new(None)); diff --git a/src/tools/clippy/tests/ui/checked_unwrap/simple_conditionals.stderr b/src/tools/clippy/tests/ui/checked_unwrap/simple_conditionals.stderr index f7e338935a770..d92c03f488813 100644 --- a/src/tools/clippy/tests/ui/checked_unwrap/simple_conditionals.stderr +++ b/src/tools/clippy/tests/ui/checked_unwrap/simple_conditionals.stderr @@ -236,5 +236,16 @@ LL | if result.is_ok() { LL | result.as_mut().unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 25 previous errors +error: creating a shared reference to mutable static is discouraged + --> tests/ui/checked_unwrap/simple_conditionals.rs:174:12 + | +LL | if X.is_some() { + | ^^^^^^^^^^^ shared reference to mutable static + | + = note: for more information, see + = 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: `-D static-mut-refs` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(static_mut_refs)]` + +error: aborting due to 26 previous errors diff --git a/src/tools/clippy/tests/ui/multiple_unsafe_ops_per_block.rs b/src/tools/clippy/tests/ui/multiple_unsafe_ops_per_block.rs index 6b8a103d4a947..87d3517cd5f22 100644 --- a/src/tools/clippy/tests/ui/multiple_unsafe_ops_per_block.rs +++ b/src/tools/clippy/tests/ui/multiple_unsafe_ops_per_block.rs @@ -4,6 +4,7 @@ #![allow(deref_nullptr)] #![allow(clippy::unnecessary_operation)] #![allow(dropping_copy_types)] +#![allow(clippy::assign_op_pattern)] #![warn(clippy::multiple_unsafe_ops_per_block)] extern crate proc_macros; diff --git a/src/tools/clippy/tests/ui/multiple_unsafe_ops_per_block.stderr b/src/tools/clippy/tests/ui/multiple_unsafe_ops_per_block.stderr index e732bde0707e5..a9417a9ef91c7 100644 --- a/src/tools/clippy/tests/ui/multiple_unsafe_ops_per_block.stderr +++ b/src/tools/clippy/tests/ui/multiple_unsafe_ops_per_block.stderr @@ -1,5 +1,5 @@ error: this `unsafe` block contains 2 unsafe operations, expected only one - --> tests/ui/multiple_unsafe_ops_per_block.rs:37:5 + --> tests/ui/multiple_unsafe_ops_per_block.rs:38:5 | LL | / unsafe { LL | | STATIC += 1; @@ -8,12 +8,12 @@ LL | | } | |_____^ | note: modification of a mutable static occurs here - --> tests/ui/multiple_unsafe_ops_per_block.rs:38:9 + --> tests/ui/multiple_unsafe_ops_per_block.rs:39:9 | LL | STATIC += 1; | ^^^^^^^^^^^ note: unsafe function call occurs here - --> tests/ui/multiple_unsafe_ops_per_block.rs:39:9 + --> tests/ui/multiple_unsafe_ops_per_block.rs:40:9 | LL | not_very_safe(); | ^^^^^^^^^^^^^^^ @@ -21,7 +21,7 @@ LL | not_very_safe(); = help: to override `-D warnings` add `#[allow(clippy::multiple_unsafe_ops_per_block)]` error: this `unsafe` block contains 2 unsafe operations, expected only one - --> tests/ui/multiple_unsafe_ops_per_block.rs:46:5 + --> tests/ui/multiple_unsafe_ops_per_block.rs:47:5 | LL | / unsafe { LL | | drop(u.u); @@ -30,18 +30,18 @@ LL | | } | |_____^ | note: union field access occurs here - --> tests/ui/multiple_unsafe_ops_per_block.rs:47:14 + --> tests/ui/multiple_unsafe_ops_per_block.rs:48:14 | LL | drop(u.u); | ^^^ note: raw pointer dereference occurs here - --> tests/ui/multiple_unsafe_ops_per_block.rs:48:9 + --> tests/ui/multiple_unsafe_ops_per_block.rs:49:9 | LL | *raw_ptr(); | ^^^^^^^^^^ error: this `unsafe` block contains 3 unsafe operations, expected only one - --> tests/ui/multiple_unsafe_ops_per_block.rs:53:5 + --> tests/ui/multiple_unsafe_ops_per_block.rs:54:5 | LL | / unsafe { LL | | asm!("nop"); @@ -51,23 +51,23 @@ LL | | } | |_____^ | note: inline assembly used here - --> tests/ui/multiple_unsafe_ops_per_block.rs:54:9 + --> tests/ui/multiple_unsafe_ops_per_block.rs:55:9 | LL | asm!("nop"); | ^^^^^^^^^^^ note: unsafe method call occurs here - --> tests/ui/multiple_unsafe_ops_per_block.rs:55:9 + --> tests/ui/multiple_unsafe_ops_per_block.rs:56:9 | LL | sample.not_very_safe(); | ^^^^^^^^^^^^^^^^^^^^^^ note: modification of a mutable static occurs here - --> tests/ui/multiple_unsafe_ops_per_block.rs:56:9 + --> tests/ui/multiple_unsafe_ops_per_block.rs:57:9 | LL | STATIC = 0; | ^^^^^^^^^^ error: this `unsafe` block contains 6 unsafe operations, expected only one - --> tests/ui/multiple_unsafe_ops_per_block.rs:62:5 + --> tests/ui/multiple_unsafe_ops_per_block.rs:63:5 | LL | / unsafe { LL | | drop(u.u); @@ -79,55 +79,55 @@ LL | | } | |_____^ | note: union field access occurs here - --> tests/ui/multiple_unsafe_ops_per_block.rs:63:14 + --> tests/ui/multiple_unsafe_ops_per_block.rs:64:14 | LL | drop(u.u); | ^^^ note: access of a mutable static occurs here - --> tests/ui/multiple_unsafe_ops_per_block.rs:64:14 + --> tests/ui/multiple_unsafe_ops_per_block.rs:65:14 | LL | drop(STATIC); | ^^^^^^ note: unsafe method call occurs here - --> tests/ui/multiple_unsafe_ops_per_block.rs:65:9 + --> tests/ui/multiple_unsafe_ops_per_block.rs:66:9 | LL | sample.not_very_safe(); | ^^^^^^^^^^^^^^^^^^^^^^ note: unsafe function call occurs here - --> tests/ui/multiple_unsafe_ops_per_block.rs:66:9 + --> tests/ui/multiple_unsafe_ops_per_block.rs:67:9 | LL | not_very_safe(); | ^^^^^^^^^^^^^^^ note: raw pointer dereference occurs here - --> tests/ui/multiple_unsafe_ops_per_block.rs:67:9 + --> tests/ui/multiple_unsafe_ops_per_block.rs:68:9 | LL | *raw_ptr(); | ^^^^^^^^^^ note: inline assembly used here - --> tests/ui/multiple_unsafe_ops_per_block.rs:68:9 + --> tests/ui/multiple_unsafe_ops_per_block.rs:69:9 | LL | asm!("nop"); | ^^^^^^^^^^^ error: this `unsafe` block contains 2 unsafe operations, expected only one - --> tests/ui/multiple_unsafe_ops_per_block.rs:106:5 + --> tests/ui/multiple_unsafe_ops_per_block.rs:107:5 | LL | unsafe { char::from_u32_unchecked(*ptr.cast::()) } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: unsafe function call occurs here - --> tests/ui/multiple_unsafe_ops_per_block.rs:106:14 + --> tests/ui/multiple_unsafe_ops_per_block.rs:107:14 | LL | unsafe { char::from_u32_unchecked(*ptr.cast::()) } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ note: raw pointer dereference occurs here - --> tests/ui/multiple_unsafe_ops_per_block.rs:106:39 + --> tests/ui/multiple_unsafe_ops_per_block.rs:107:39 | LL | unsafe { char::from_u32_unchecked(*ptr.cast::()) } | ^^^^^^^^^^^^^^^^^^ error: this `unsafe` block contains 2 unsafe operations, expected only one - --> tests/ui/multiple_unsafe_ops_per_block.rs:124:5 + --> tests/ui/multiple_unsafe_ops_per_block.rs:125:5 | LL | / unsafe { LL | | x(); @@ -136,18 +136,18 @@ LL | | } | |_____^ | note: unsafe function call occurs here - --> tests/ui/multiple_unsafe_ops_per_block.rs:125:9 + --> tests/ui/multiple_unsafe_ops_per_block.rs:126:9 | LL | x(); | ^^^ note: unsafe function call occurs here - --> tests/ui/multiple_unsafe_ops_per_block.rs:126:9 + --> tests/ui/multiple_unsafe_ops_per_block.rs:127:9 | LL | x(); | ^^^ error: this `unsafe` block contains 2 unsafe operations, expected only one - --> tests/ui/multiple_unsafe_ops_per_block.rs:135:9 + --> tests/ui/multiple_unsafe_ops_per_block.rs:136:9 | LL | / unsafe { LL | | T::X(); @@ -156,18 +156,18 @@ LL | | } | |_________^ | note: unsafe function call occurs here - --> tests/ui/multiple_unsafe_ops_per_block.rs:136:13 + --> tests/ui/multiple_unsafe_ops_per_block.rs:137:13 | LL | T::X(); | ^^^^^^ note: unsafe function call occurs here - --> tests/ui/multiple_unsafe_ops_per_block.rs:137:13 + --> tests/ui/multiple_unsafe_ops_per_block.rs:138:13 | LL | T::X(); | ^^^^^^ error: this `unsafe` block contains 2 unsafe operations, expected only one - --> tests/ui/multiple_unsafe_ops_per_block.rs:145:5 + --> tests/ui/multiple_unsafe_ops_per_block.rs:146:5 | LL | / unsafe { LL | | x.0(); @@ -176,12 +176,12 @@ LL | | } | |_____^ | note: unsafe function call occurs here - --> tests/ui/multiple_unsafe_ops_per_block.rs:146:9 + --> tests/ui/multiple_unsafe_ops_per_block.rs:147:9 | LL | x.0(); | ^^^^^ note: unsafe function call occurs here - --> tests/ui/multiple_unsafe_ops_per_block.rs:147:9 + --> tests/ui/multiple_unsafe_ops_per_block.rs:148:9 | LL | x.0(); | ^^^^^ diff --git a/src/tools/clippy/tests/ui/must_use_candidates.fixed b/src/tools/clippy/tests/ui/must_use_candidates.fixed index c057eba4aca16..adb266ffbc8dd 100644 --- a/src/tools/clippy/tests/ui/must_use_candidates.fixed +++ b/src/tools/clippy/tests/ui/must_use_candidates.fixed @@ -1,5 +1,10 @@ #![feature(never_type)] -#![allow(unused_mut, clippy::redundant_allocation, clippy::needless_pass_by_ref_mut)] +#![allow( + unused_mut, + clippy::redundant_allocation, + clippy::needless_pass_by_ref_mut, + static_mut_refs +)] #![warn(clippy::must_use_candidate)] use std::rc::Rc; use std::sync::atomic::{AtomicBool, Ordering}; diff --git a/src/tools/clippy/tests/ui/must_use_candidates.rs b/src/tools/clippy/tests/ui/must_use_candidates.rs index 360196520062f..49bb16af788f1 100644 --- a/src/tools/clippy/tests/ui/must_use_candidates.rs +++ b/src/tools/clippy/tests/ui/must_use_candidates.rs @@ -1,5 +1,10 @@ #![feature(never_type)] -#![allow(unused_mut, clippy::redundant_allocation, clippy::needless_pass_by_ref_mut)] +#![allow( + unused_mut, + clippy::redundant_allocation, + clippy::needless_pass_by_ref_mut, + static_mut_refs +)] #![warn(clippy::must_use_candidate)] use std::rc::Rc; use std::sync::atomic::{AtomicBool, Ordering}; diff --git a/src/tools/clippy/tests/ui/must_use_candidates.stderr b/src/tools/clippy/tests/ui/must_use_candidates.stderr index c64636ba44258..2117e37866e2d 100644 --- a/src/tools/clippy/tests/ui/must_use_candidates.stderr +++ b/src/tools/clippy/tests/ui/must_use_candidates.stderr @@ -1,5 +1,5 @@ error: this function could have a `#[must_use]` attribute - --> tests/ui/must_use_candidates.rs:11:1 + --> tests/ui/must_use_candidates.rs:16:1 | LL | pub fn pure(i: u8) -> u8 { | ^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn pure(i: u8) -> u8` @@ -8,25 +8,25 @@ LL | pub fn pure(i: u8) -> u8 { = help: to override `-D warnings` add `#[allow(clippy::must_use_candidate)]` error: this method could have a `#[must_use]` attribute - --> tests/ui/must_use_candidates.rs:16:5 + --> tests/ui/must_use_candidates.rs:21:5 | LL | pub fn inherent_pure(&self) -> u8 { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn inherent_pure(&self) -> u8` error: this function could have a `#[must_use]` attribute - --> tests/ui/must_use_candidates.rs:47:1 + --> tests/ui/must_use_candidates.rs:52:1 | LL | pub fn with_marker(_d: std::marker::PhantomData<&mut u32>) -> bool { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn with_marker(_d: std::marker::PhantomData<&mut u32>) -> bool` error: this function could have a `#[must_use]` attribute - --> tests/ui/must_use_candidates.rs:59:1 + --> tests/ui/must_use_candidates.rs:64:1 | LL | pub fn rcd(_x: Rc) -> bool { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn rcd(_x: Rc) -> bool` error: this function could have a `#[must_use]` attribute - --> tests/ui/must_use_candidates.rs:67:1 + --> tests/ui/must_use_candidates.rs:72:1 | LL | pub fn arcd(_x: Arc) -> bool { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn arcd(_x: Arc) -> bool` diff --git a/src/tools/clippy/tests/ui/redundant_static_lifetimes.fixed b/src/tools/clippy/tests/ui/redundant_static_lifetimes.fixed index 9787bb635e709..3d1c78bd12d9e 100644 --- a/src/tools/clippy/tests/ui/redundant_static_lifetimes.fixed +++ b/src/tools/clippy/tests/ui/redundant_static_lifetimes.fixed @@ -1,4 +1,6 @@ #![allow(unused)] +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] #[derive(Debug)] struct Foo; diff --git a/src/tools/clippy/tests/ui/redundant_static_lifetimes.rs b/src/tools/clippy/tests/ui/redundant_static_lifetimes.rs index b5a4827fa948e..5932f14b8d9a9 100644 --- a/src/tools/clippy/tests/ui/redundant_static_lifetimes.rs +++ b/src/tools/clippy/tests/ui/redundant_static_lifetimes.rs @@ -1,4 +1,6 @@ #![allow(unused)] +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] #[derive(Debug)] struct Foo; diff --git a/src/tools/clippy/tests/ui/redundant_static_lifetimes.stderr b/src/tools/clippy/tests/ui/redundant_static_lifetimes.stderr index 5c5e2f2a57360..48871eba2dc75 100644 --- a/src/tools/clippy/tests/ui/redundant_static_lifetimes.stderr +++ b/src/tools/clippy/tests/ui/redundant_static_lifetimes.stderr @@ -1,5 +1,5 @@ error: constants have by default a `'static` lifetime - --> tests/ui/redundant_static_lifetimes.rs:6:17 + --> tests/ui/redundant_static_lifetimes.rs:8:17 | LL | const VAR_ONE: &'static str = "Test constant #1"; // ERROR: Consider removing 'static. | -^^^^^^^---- help: consider removing `'static`: `&str` @@ -8,103 +8,103 @@ LL | const VAR_ONE: &'static str = "Test constant #1"; // ERROR: Consider removi = help: to override `-D warnings` add `#[allow(clippy::redundant_static_lifetimes)]` error: constants have by default a `'static` lifetime - --> tests/ui/redundant_static_lifetimes.rs:10:21 + --> tests/ui/redundant_static_lifetimes.rs:12:21 | LL | const VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR: Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: constants have by default a `'static` lifetime - --> tests/ui/redundant_static_lifetimes.rs:12:32 + --> tests/ui/redundant_static_lifetimes.rs:14:32 | LL | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR: Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: constants have by default a `'static` lifetime - --> tests/ui/redundant_static_lifetimes.rs:12:47 + --> tests/ui/redundant_static_lifetimes.rs:14:47 | LL | const VAR_FOUR: (&str, (&str, &'static str), &'static str) = ("on", ("th", "th"), "on"); // ERROR: Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: constants have by default a `'static` lifetime - --> tests/ui/redundant_static_lifetimes.rs:14:17 + --> tests/ui/redundant_static_lifetimes.rs:16:17 | LL | const VAR_SIX: &'static u8 = &5; | -^^^^^^^--- help: consider removing `'static`: `&u8` error: constants have by default a `'static` lifetime - --> tests/ui/redundant_static_lifetimes.rs:16:20 + --> tests/ui/redundant_static_lifetimes.rs:18:20 | LL | const VAR_HEIGHT: &'static Foo = &Foo {}; | -^^^^^^^---- help: consider removing `'static`: `&Foo` error: constants have by default a `'static` lifetime - --> tests/ui/redundant_static_lifetimes.rs:18:19 + --> tests/ui/redundant_static_lifetimes.rs:20:19 | LL | const VAR_SLICE: &'static [u8] = b"Test constant #1"; // ERROR: Consider removing 'static. | -^^^^^^^----- help: consider removing `'static`: `&[u8]` error: constants have by default a `'static` lifetime - --> tests/ui/redundant_static_lifetimes.rs:20:19 + --> tests/ui/redundant_static_lifetimes.rs:22:19 | LL | const VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR: Consider removing 'static. | -^^^^^^^--------- help: consider removing `'static`: `&(u8, u8)` error: constants have by default a `'static` lifetime - --> tests/ui/redundant_static_lifetimes.rs:22:19 + --> tests/ui/redundant_static_lifetimes.rs:24:19 | LL | const VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR: Consider removing 'static. | -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]` error: statics have by default a `'static` lifetime - --> tests/ui/redundant_static_lifetimes.rs:24:25 + --> tests/ui/redundant_static_lifetimes.rs:26:25 | LL | static STATIC_VAR_ONE: &'static str = "Test static #1"; // ERROR: Consider removing 'static. | -^^^^^^^---- help: consider removing `'static`: `&str` error: statics have by default a `'static` lifetime - --> tests/ui/redundant_static_lifetimes.rs:28:29 + --> tests/ui/redundant_static_lifetimes.rs:30:29 | LL | static STATIC_VAR_THREE: &[&'static str] = &["one", "two"]; // ERROR: Consider removing 'static | -^^^^^^^---- help: consider removing `'static`: `&str` error: statics have by default a `'static` lifetime - --> tests/ui/redundant_static_lifetimes.rs:30:25 + --> tests/ui/redundant_static_lifetimes.rs:32:25 | LL | static STATIC_VAR_SIX: &'static u8 = &5; | -^^^^^^^--- help: consider removing `'static`: `&u8` error: statics have by default a `'static` lifetime - --> tests/ui/redundant_static_lifetimes.rs:32:28 + --> tests/ui/redundant_static_lifetimes.rs:34:28 | LL | static STATIC_VAR_HEIGHT: &'static Foo = &Foo {}; | -^^^^^^^---- help: consider removing `'static`: `&Foo` error: statics have by default a `'static` lifetime - --> tests/ui/redundant_static_lifetimes.rs:34:27 + --> tests/ui/redundant_static_lifetimes.rs:36:27 | LL | static STATIC_VAR_SLICE: &'static [u8] = b"Test static #3"; // ERROR: Consider removing 'static. | -^^^^^^^----- help: consider removing `'static`: `&[u8]` error: statics have by default a `'static` lifetime - --> tests/ui/redundant_static_lifetimes.rs:36:27 + --> tests/ui/redundant_static_lifetimes.rs:38:27 | LL | static STATIC_VAR_TUPLE: &'static (u8, u8) = &(1, 2); // ERROR: Consider removing 'static. | -^^^^^^^--------- help: consider removing `'static`: `&(u8, u8)` error: statics have by default a `'static` lifetime - --> tests/ui/redundant_static_lifetimes.rs:38:27 + --> tests/ui/redundant_static_lifetimes.rs:40:27 | LL | static STATIC_VAR_ARRAY: &'static [u8; 1] = b"T"; // ERROR: Consider removing 'static. | -^^^^^^^-------- help: consider removing `'static`: `&[u8; 1]` error: statics have by default a `'static` lifetime - --> tests/ui/redundant_static_lifetimes.rs:40:31 + --> tests/ui/redundant_static_lifetimes.rs:42:31 | LL | static mut STATIC_MUT_SLICE: &'static mut [u32] = &mut [0]; | -^^^^^^^---------- help: consider removing `'static`: `&mut [u32]` error: statics have by default a `'static` lifetime - --> tests/ui/redundant_static_lifetimes.rs:69:16 + --> tests/ui/redundant_static_lifetimes.rs:71:16 | LL | static V: &'static u8 = &17; | -^^^^^^^--- help: consider removing `'static`: `&u8` diff --git a/src/tools/clippy/tests/ui/useless_conversion.fixed b/src/tools/clippy/tests/ui/useless_conversion.fixed index ce00fde2f9930..eff617a801689 100644 --- a/src/tools/clippy/tests/ui/useless_conversion.fixed +++ b/src/tools/clippy/tests/ui/useless_conversion.fixed @@ -1,5 +1,7 @@ #![deny(clippy::useless_conversion)] #![allow(clippy::needless_if, clippy::unnecessary_wraps)] +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] fn test_generic(val: T) -> T { let _ = val; diff --git a/src/tools/clippy/tests/ui/useless_conversion.rs b/src/tools/clippy/tests/ui/useless_conversion.rs index 399796195868d..64b0662078919 100644 --- a/src/tools/clippy/tests/ui/useless_conversion.rs +++ b/src/tools/clippy/tests/ui/useless_conversion.rs @@ -1,5 +1,7 @@ #![deny(clippy::useless_conversion)] #![allow(clippy::needless_if, clippy::unnecessary_wraps)] +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] fn test_generic(val: T) -> T { let _ = T::from(val); diff --git a/src/tools/clippy/tests/ui/useless_conversion.stderr b/src/tools/clippy/tests/ui/useless_conversion.stderr index 82f945c5e89f6..b149357bcf4f4 100644 --- a/src/tools/clippy/tests/ui/useless_conversion.stderr +++ b/src/tools/clippy/tests/ui/useless_conversion.stderr @@ -1,5 +1,5 @@ error: useless conversion to the same type: `T` - --> tests/ui/useless_conversion.rs:5:13 + --> tests/ui/useless_conversion.rs:7:13 | LL | let _ = T::from(val); | ^^^^^^^^^^^^ help: consider removing `T::from()`: `val` @@ -11,217 +11,217 @@ LL | #![deny(clippy::useless_conversion)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: useless conversion to the same type: `T` - --> tests/ui/useless_conversion.rs:6:5 + --> tests/ui/useless_conversion.rs:8:5 | LL | val.into() | ^^^^^^^^^^ help: consider removing `.into()`: `val` error: useless conversion to the same type: `i32` - --> tests/ui/useless_conversion.rs:18:22 + --> tests/ui/useless_conversion.rs:20:22 | LL | let _: i32 = 0i32.into(); | ^^^^^^^^^^^ help: consider removing `.into()`: `0i32` error: useless conversion to the same type: `std::str::Lines<'_>` - --> tests/ui/useless_conversion.rs:48:22 + --> tests/ui/useless_conversion.rs:50:22 | LL | if Some("ok") == lines.into_iter().next() {} | ^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `lines` error: useless conversion to the same type: `std::str::Lines<'_>` - --> tests/ui/useless_conversion.rs:53:21 + --> tests/ui/useless_conversion.rs:55:21 | LL | let mut lines = text.lines().into_iter(); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `text.lines()` error: useless conversion to the same type: `std::str::Lines<'_>` - --> tests/ui/useless_conversion.rs:59:22 + --> tests/ui/useless_conversion.rs:61:22 | LL | if Some("ok") == text.lines().into_iter().next() {} | ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `text.lines()` error: useless conversion to the same type: `std::ops::Range` - --> tests/ui/useless_conversion.rs:65:13 + --> tests/ui/useless_conversion.rs:67:13 | LL | let _ = NUMBERS.into_iter().next(); | ^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `NUMBERS` error: useless conversion to the same type: `std::ops::Range` - --> tests/ui/useless_conversion.rs:70:17 + --> tests/ui/useless_conversion.rs:72:17 | LL | let mut n = NUMBERS.into_iter(); | ^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `NUMBERS` error: useless conversion to the same type: `std::string::String` - --> tests/ui/useless_conversion.rs:132:21 + --> tests/ui/useless_conversion.rs:134:21 | LL | let _: String = "foo".to_string().into(); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `"foo".to_string()` error: useless conversion to the same type: `std::string::String` - --> tests/ui/useless_conversion.rs:133:21 + --> tests/ui/useless_conversion.rs:135:21 | LL | let _: String = From::from("foo".to_string()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `From::from()`: `"foo".to_string()` error: useless conversion to the same type: `std::string::String` - --> tests/ui/useless_conversion.rs:134:13 + --> tests/ui/useless_conversion.rs:136:13 | LL | let _ = String::from("foo".to_string()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `String::from()`: `"foo".to_string()` error: useless conversion to the same type: `std::string::String` - --> tests/ui/useless_conversion.rs:135:13 + --> tests/ui/useless_conversion.rs:137:13 | LL | let _ = String::from(format!("A: {:04}", 123)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `String::from()`: `format!("A: {:04}", 123)` error: useless conversion to the same type: `std::str::Lines<'_>` - --> tests/ui/useless_conversion.rs:136:13 + --> tests/ui/useless_conversion.rs:138:13 | LL | let _ = "".lines().into_iter(); | ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `"".lines()` error: useless conversion to the same type: `std::vec::IntoIter` - --> tests/ui/useless_conversion.rs:137:13 + --> tests/ui/useless_conversion.rs:139:13 | LL | let _ = vec![1, 2, 3].into_iter().into_iter(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![1, 2, 3].into_iter()` error: useless conversion to the same type: `std::string::String` - --> tests/ui/useless_conversion.rs:138:21 + --> tests/ui/useless_conversion.rs:140:21 | LL | let _: String = format!("Hello {}", "world").into(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `format!("Hello {}", "world")` error: useless conversion to the same type: `i32` - --> tests/ui/useless_conversion.rs:143:13 + --> tests/ui/useless_conversion.rs:145:13 | LL | let _ = i32::from(a + b) * 3; | ^^^^^^^^^^^^^^^^ help: consider removing `i32::from()`: `(a + b)` error: useless conversion to the same type: `Foo<'a'>` - --> tests/ui/useless_conversion.rs:149:23 + --> tests/ui/useless_conversion.rs:151:23 | LL | let _: Foo<'a'> = s2.into(); | ^^^^^^^^^ help: consider removing `.into()`: `s2` error: useless conversion to the same type: `Foo<'a'>` - --> tests/ui/useless_conversion.rs:151:13 + --> tests/ui/useless_conversion.rs:153:13 | LL | let _ = Foo::<'a'>::from(s3); | ^^^^^^^^^^^^^^^^^^^^ help: consider removing `Foo::<'a'>::from()`: `s3` error: useless conversion to the same type: `std::vec::IntoIter>` - --> tests/ui/useless_conversion.rs:153:13 + --> tests/ui/useless_conversion.rs:155:13 | LL | let _ = vec![s4, s4, s4].into_iter().into_iter(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![s4, s4, s4].into_iter()` error: explicit call to `.into_iter()` in function argument accepting `IntoIterator` - --> tests/ui/useless_conversion.rs:185:7 + --> tests/ui/useless_conversion.rs:187:7 | LL | b(vec![1, 2].into_iter()); | ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `vec![1, 2]` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` - --> tests/ui/useless_conversion.rs:175:13 + --> tests/ui/useless_conversion.rs:177:13 | LL | fn b>(_: T) {} | ^^^^^^^^^^^^^^^^^^^^^^^^ error: explicit call to `.into_iter()` in function argument accepting `IntoIterator` - --> tests/ui/useless_conversion.rs:186:7 + --> tests/ui/useless_conversion.rs:188:7 | LL | c(vec![1, 2].into_iter()); | ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `vec![1, 2]` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` - --> tests/ui/useless_conversion.rs:176:18 + --> tests/ui/useless_conversion.rs:178:18 | LL | fn c(_: impl IntoIterator) {} | ^^^^^^^^^^^^^^^^^^^^^^^^ error: explicit call to `.into_iter()` in function argument accepting `IntoIterator` - --> tests/ui/useless_conversion.rs:187:7 + --> tests/ui/useless_conversion.rs:189:7 | LL | d(vec![1, 2].into_iter()); | ^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `vec![1, 2]` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` - --> tests/ui/useless_conversion.rs:179:12 + --> tests/ui/useless_conversion.rs:181:12 | LL | T: IntoIterator, | ^^^^^^^^^^^^^^^^^^^^^^^^ error: explicit call to `.into_iter()` in function argument accepting `IntoIterator` - --> tests/ui/useless_conversion.rs:190:7 + --> tests/ui/useless_conversion.rs:192:7 | LL | b(vec![1, 2].into_iter().into_iter()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`s: `vec![1, 2]` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` - --> tests/ui/useless_conversion.rs:175:13 + --> tests/ui/useless_conversion.rs:177:13 | LL | fn b>(_: T) {} | ^^^^^^^^^^^^^^^^^^^^^^^^ error: explicit call to `.into_iter()` in function argument accepting `IntoIterator` - --> tests/ui/useless_conversion.rs:191:7 + --> tests/ui/useless_conversion.rs:193:7 | LL | b(vec![1, 2].into_iter().into_iter().into_iter()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`s: `vec![1, 2]` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` - --> tests/ui/useless_conversion.rs:175:13 + --> tests/ui/useless_conversion.rs:177:13 | LL | fn b>(_: T) {} | ^^^^^^^^^^^^^^^^^^^^^^^^ error: explicit call to `.into_iter()` in function argument accepting `IntoIterator` - --> tests/ui/useless_conversion.rs:237:24 + --> tests/ui/useless_conversion.rs:239:24 | LL | foo2::([1, 2, 3].into_iter()); | ^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `[1, 2, 3]` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` - --> tests/ui/useless_conversion.rs:216:12 + --> tests/ui/useless_conversion.rs:218:12 | LL | I: IntoIterator + Helper, | ^^^^^^^^^^^^^^^^^^^^^^^^ error: explicit call to `.into_iter()` in function argument accepting `IntoIterator` - --> tests/ui/useless_conversion.rs:245:14 + --> tests/ui/useless_conversion.rs:247:14 | LL | foo3([1, 2, 3].into_iter()); | ^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `[1, 2, 3]` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` - --> tests/ui/useless_conversion.rs:225:12 + --> tests/ui/useless_conversion.rs:227:12 | LL | I: IntoIterator, | ^^^^^^^^^^^^^^^^^^^^^^^^ error: explicit call to `.into_iter()` in function argument accepting `IntoIterator` - --> tests/ui/useless_conversion.rs:254:16 + --> tests/ui/useless_conversion.rs:256:16 | LL | S1.foo([1, 2].into_iter()); | ^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `[1, 2]` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` - --> tests/ui/useless_conversion.rs:251:27 + --> tests/ui/useless_conversion.rs:253:27 | LL | pub fn foo(&self, _: I) {} | ^^^^^^^^^^^^ error: explicit call to `.into_iter()` in function argument accepting `IntoIterator` - --> tests/ui/useless_conversion.rs:273:44 + --> tests/ui/useless_conversion.rs:275:44 | LL | v0.into_iter().interleave_shortest(v1.into_iter()); | ^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `v1` | note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()` - --> tests/ui/useless_conversion.rs:260:20 + --> tests/ui/useless_conversion.rs:262:20 | LL | J: IntoIterator, | ^^^^^^^^^^^^ diff --git a/src/tools/miri/tests/pass-dep/concurrency/linux-futex.rs b/src/tools/miri/tests/pass-dep/concurrency/linux-futex.rs index 399d6df73ff12..7293d7e6e46d9 100644 --- a/src/tools/miri/tests/pass-dep/concurrency/linux-futex.rs +++ b/src/tools/miri/tests/pass-dep/concurrency/linux-futex.rs @@ -1,6 +1,9 @@ //@only-target-linux //@compile-flags: -Zmiri-disable-isolation +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + use std::mem::MaybeUninit; use std::ptr::{self, addr_of}; use std::sync::atomic::AtomicI32; diff --git a/src/tools/miri/tests/pass-dep/concurrency/tls_pthread_drop_order.rs b/src/tools/miri/tests/pass-dep/concurrency/tls_pthread_drop_order.rs index 52348aad33d72..c78cddba96a57 100644 --- a/src/tools/miri/tests/pass-dep/concurrency/tls_pthread_drop_order.rs +++ b/src/tools/miri/tests/pass-dep/concurrency/tls_pthread_drop_order.rs @@ -5,6 +5,9 @@ //! the fallback path in `guard::key::enable`, which uses a *single* pthread_key //! to manage a thread-local list of dtors to call. +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + use std::mem; use std::ptr; diff --git a/src/tools/miri/tests/pass-dep/libc/libc-eventfd.rs b/src/tools/miri/tests/pass-dep/libc/libc-eventfd.rs index a3567eeb7cb4a..1b6a2f9cc7b23 100644 --- a/src/tools/miri/tests/pass-dep/libc/libc-eventfd.rs +++ b/src/tools/miri/tests/pass-dep/libc/libc-eventfd.rs @@ -2,6 +2,9 @@ // test_race depends on a deterministic schedule. //@compile-flags: -Zmiri-preemption-rate=0 +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + use std::thread; fn main() { diff --git a/src/tools/miri/tests/pass-dep/libc/libc-pipe.rs b/src/tools/miri/tests/pass-dep/libc/libc-pipe.rs index 90dbd888392bf..390b0825b0d95 100644 --- a/src/tools/miri/tests/pass-dep/libc/libc-pipe.rs +++ b/src/tools/miri/tests/pass-dep/libc/libc-pipe.rs @@ -72,6 +72,8 @@ fn test_pipe_threaded() { thread2.join().unwrap(); } +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#[allow(static_mut_refs)] fn test_race() { static mut VAL: u8 = 0; let mut fds = [-1, -1]; diff --git a/src/tools/miri/tests/pass-dep/libc/libc-socketpair.rs b/src/tools/miri/tests/pass-dep/libc/libc-socketpair.rs index 15e040116debc..69bfa454665fa 100644 --- a/src/tools/miri/tests/pass-dep/libc/libc-socketpair.rs +++ b/src/tools/miri/tests/pass-dep/libc/libc-socketpair.rs @@ -1,6 +1,10 @@ //@ignore-target-windows: No libc socketpair on Windows // test_race depends on a deterministic schedule. //@compile-flags: -Zmiri-preemption-rate=0 + +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + use std::thread; fn main() { test_socketpair(); diff --git a/src/tools/miri/tests/pass/atomic.rs b/src/tools/miri/tests/pass/atomic.rs index dfdc9b42f81fc..c03023ee6930b 100644 --- a/src/tools/miri/tests/pass/atomic.rs +++ b/src/tools/miri/tests/pass/atomic.rs @@ -1,7 +1,11 @@ //@revisions: stack tree //@[tree]compile-flags: -Zmiri-tree-borrows //@compile-flags: -Zmiri-strict-provenance + #![feature(strict_provenance, strict_provenance_atomic_ptr)] +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + use std::sync::atomic::{ compiler_fence, fence, AtomicBool, AtomicIsize, AtomicPtr, AtomicU64, Ordering::*, }; diff --git a/src/tools/miri/tests/pass/drop_on_array_elements.rs b/src/tools/miri/tests/pass/drop_on_array_elements.rs index ae1ef036267e2..4bf241fee2b3f 100644 --- a/src/tools/miri/tests/pass/drop_on_array_elements.rs +++ b/src/tools/miri/tests/pass/drop_on_array_elements.rs @@ -1,3 +1,5 @@ +#![allow(static_mut_refs)] + struct Bar(u16); // ZSTs are tested separately static mut DROP_COUNT: usize = 0; diff --git a/src/tools/miri/tests/pass/drop_on_fat_ptr_array_elements.rs b/src/tools/miri/tests/pass/drop_on_fat_ptr_array_elements.rs index 40025cd07f7e0..04723af94c311 100644 --- a/src/tools/miri/tests/pass/drop_on_fat_ptr_array_elements.rs +++ b/src/tools/miri/tests/pass/drop_on_fat_ptr_array_elements.rs @@ -1,3 +1,5 @@ +#![allow(static_mut_refs)] + trait Foo {} struct Bar; diff --git a/src/tools/miri/tests/pass/drop_on_zst_array_elements.rs b/src/tools/miri/tests/pass/drop_on_zst_array_elements.rs index babe098e4e6f5..71398724f2cc8 100644 --- a/src/tools/miri/tests/pass/drop_on_zst_array_elements.rs +++ b/src/tools/miri/tests/pass/drop_on_zst_array_elements.rs @@ -1,3 +1,5 @@ +#![allow(static_mut_refs)] + struct Bar; static mut DROP_COUNT: usize = 0; diff --git a/src/tools/miri/tests/pass/drop_through_owned_slice.rs b/src/tools/miri/tests/pass/drop_through_owned_slice.rs index 8cdeb57d02f79..2d1d876e6f239 100644 --- a/src/tools/miri/tests/pass/drop_through_owned_slice.rs +++ b/src/tools/miri/tests/pass/drop_through_owned_slice.rs @@ -1,3 +1,5 @@ +#![allow(static_mut_refs)] + struct Bar; static mut DROP_COUNT: usize = 0; diff --git a/src/tools/miri/tests/pass/static_memory_modification.rs b/src/tools/miri/tests/pass/static_memory_modification.rs index 84a524b1ed163..1900250bf4f8f 100644 --- a/src/tools/miri/tests/pass/static_memory_modification.rs +++ b/src/tools/miri/tests/pass/static_memory_modification.rs @@ -1,3 +1,6 @@ +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + use std::sync::atomic::{AtomicUsize, Ordering}; static mut X: usize = 5; diff --git a/src/tools/miri/tests/pass/static_mut.rs b/src/tools/miri/tests/pass/static_mut.rs index 4488b5a09d5ba..f88c7a16e6dd7 100644 --- a/src/tools/miri/tests/pass/static_mut.rs +++ b/src/tools/miri/tests/pass/static_mut.rs @@ -1,3 +1,6 @@ +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + use std::ptr::addr_of; static mut FOO: i32 = 42; diff --git a/src/tools/miri/tests/pass/tls/tls_static.rs b/src/tools/miri/tests/pass/tls/tls_static.rs index 8d0e5089d404a..cc4eaca72ab6e 100644 --- a/src/tools/miri/tests/pass/tls/tls_static.rs +++ b/src/tools/miri/tests/pass/tls/tls_static.rs @@ -7,6 +7,8 @@ //! dereferencing the pointer on `t2` resolves to `t1`'s thread-local. In this //! test, we also check that thread-locals act as per-thread statics. +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] #![feature(thread_local)] use std::ptr::addr_of_mut; diff --git a/src/tools/tidy/src/issues.txt b/src/tools/tidy/src/issues.txt index 5205fa1429435..ce313c52c3760 100644 --- a/src/tools/tidy/src/issues.txt +++ b/src/tools/tidy/src/issues.txt @@ -1651,7 +1651,6 @@ ui/issues/issue-17068.rs ui/issues/issue-17121.rs ui/issues/issue-17216.rs ui/issues/issue-17252.rs -ui/issues/issue-17302.rs ui/issues/issue-17322.rs ui/issues/issue-17336.rs ui/issues/issue-17337.rs diff --git a/tests/ui/abi/statics/static-mut-foreign.rs b/tests/ui/abi/statics/static-mut-foreign.rs index 167b7a4e36e9e..40cd57637e6cd 100644 --- a/tests/ui/abi/statics/static-mut-foreign.rs +++ b/tests/ui/abi/statics/static-mut-foreign.rs @@ -3,6 +3,9 @@ // statics cannot. This ensures that there's some form of error if this is // attempted. +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + use std::ffi::c_int; #[link(name = "rust_test_helpers", kind = "static")] @@ -29,9 +32,7 @@ unsafe fn run() { rust_dbg_static_mut = -3; assert_eq!(rust_dbg_static_mut, -3); static_bound(&rust_dbg_static_mut); - //~^ WARN shared reference to mutable static is discouraged [static_mut_refs] static_bound_set(&mut rust_dbg_static_mut); - //~^ WARN mutable reference to mutable static is discouraged [static_mut_refs] } pub fn main() { diff --git a/tests/ui/abi/statics/static-mut-foreign.stderr b/tests/ui/abi/statics/static-mut-foreign.stderr deleted file mode 100644 index 0c4bd5382a1f0..0000000000000 --- a/tests/ui/abi/statics/static-mut-foreign.stderr +++ /dev/null @@ -1,31 +0,0 @@ -warning: creating a shared reference to mutable static is discouraged - --> $DIR/static-mut-foreign.rs:31:18 - | -LL | static_bound(&rust_dbg_static_mut); - | ^^^^^^^^^^^^^^^^^^^^ shared reference to mutable static - | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior - = note: `#[warn(static_mut_refs)]` on by default -help: use `addr_of!` instead to create a raw pointer - | -LL | static_bound(addr_of!(rust_dbg_static_mut)); - | ~~~~~~~~~ + - -warning: creating a mutable reference to mutable static is discouraged - --> $DIR/static-mut-foreign.rs:33:22 - | -LL | static_bound_set(&mut rust_dbg_static_mut); - | ^^^^^^^^^^^^^^^^^^^^^^^^ mutable reference to mutable static - | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior -help: use `addr_of_mut!` instead to create a raw pointer - | -LL | static_bound_set(addr_of_mut!(rust_dbg_static_mut)); - | ~~~~~~~~~~~~~ + - -warning: 2 warnings emitted - diff --git a/tests/ui/array-slice-vec/slice-panic-1.rs b/tests/ui/array-slice-vec/slice-panic-1.rs index 4436b63385641..2e5b6e0b624c1 100644 --- a/tests/ui/array-slice-vec/slice-panic-1.rs +++ b/tests/ui/array-slice-vec/slice-panic-1.rs @@ -5,15 +5,17 @@ // Test that if a slicing expr[..] fails, the correct cleanups happen. - use std::thread; +use std::sync::atomic::{AtomicUsize, Ordering}; struct Foo; -static mut DTOR_COUNT: isize = 0; +static DTOR_COUNT: AtomicUsize = AtomicUsize::new(0); impl Drop for Foo { - fn drop(&mut self) { unsafe { DTOR_COUNT += 1; } } + fn drop(&mut self) { + DTOR_COUNT.fetch_add(1, Ordering::Relaxed); + } } fn foo() { @@ -22,6 +24,6 @@ fn foo() { } fn main() { - let _ = thread::spawn(move|| foo()).join(); - unsafe { assert_eq!(DTOR_COUNT, 2); } + let _ = thread::spawn(move || foo()).join(); + assert_eq!(DTOR_COUNT.load(Ordering::Relaxed), 2); } diff --git a/tests/ui/array-slice-vec/slice-panic-2.rs b/tests/ui/array-slice-vec/slice-panic-2.rs index 4bd139884244e..4086c3f2ea525 100644 --- a/tests/ui/array-slice-vec/slice-panic-2.rs +++ b/tests/ui/array-slice-vec/slice-panic-2.rs @@ -7,13 +7,16 @@ use std::thread; +use std::sync::atomic::{AtomicUsize, Ordering}; struct Foo; -static mut DTOR_COUNT: isize = 0; +static DTOR_COUNT: AtomicUsize = AtomicUsize::new(0); impl Drop for Foo { - fn drop(&mut self) { unsafe { DTOR_COUNT += 1; } } + fn drop(&mut self) { + DTOR_COUNT.fetch_add(1, Ordering::Relaxed); + } } fn bar() -> usize { @@ -27,5 +30,5 @@ fn foo() { fn main() { let _ = thread::spawn(move|| foo()).join(); - unsafe { assert_eq!(DTOR_COUNT, 2); } + assert_eq!(DTOR_COUNT.load(Ordering::Relaxed), 2); } diff --git a/tests/ui/array-slice-vec/slice.rs b/tests/ui/array-slice-vec/slice.rs index 2adcd96f5984a..9d1cc559463f0 100644 --- a/tests/ui/array-slice-vec/slice.rs +++ b/tests/ui/array-slice-vec/slice.rs @@ -5,61 +5,62 @@ extern crate core; use core::ops::{Index, IndexMut, Range, RangeTo, RangeFrom, RangeFull}; +use std::sync::atomic::{AtomicUsize, Ordering}; -static mut COUNT: usize = 0; +static COUNT: AtomicUsize = AtomicUsize::new(0); struct Foo; impl Index> for Foo { type Output = Foo; fn index(&self, index: Range) -> &Foo { - unsafe { COUNT += 1; } + COUNT.fetch_add(1, Ordering::Relaxed); self } } impl Index> for Foo { type Output = Foo; fn index(&self, index: RangeTo) -> &Foo { - unsafe { COUNT += 1; } + COUNT.fetch_add(1, Ordering::Relaxed); self } } impl Index> for Foo { type Output = Foo; fn index(&self, index: RangeFrom) -> &Foo { - unsafe { COUNT += 1; } + COUNT.fetch_add(1, Ordering::Relaxed); self } } impl Index for Foo { type Output = Foo; fn index(&self, _index: RangeFull) -> &Foo { - unsafe { COUNT += 1; } + COUNT.fetch_add(1, Ordering::Relaxed); self } } impl IndexMut> for Foo { fn index_mut(&mut self, index: Range) -> &mut Foo { - unsafe { COUNT += 1; } + COUNT.fetch_add(1, Ordering::Relaxed); self } } impl IndexMut> for Foo { fn index_mut(&mut self, index: RangeTo) -> &mut Foo { - unsafe { COUNT += 1; } + COUNT.fetch_add(1, Ordering::Relaxed); self } } impl IndexMut> for Foo { fn index_mut(&mut self, index: RangeFrom) -> &mut Foo { - unsafe { COUNT += 1; } + COUNT.fetch_add(1, Ordering::Relaxed); self } } impl IndexMut for Foo { fn index_mut(&mut self, _index: RangeFull) -> &mut Foo { - unsafe { COUNT += 1; } + COUNT.fetch_add(1, Ordering::Relaxed); self } } @@ -75,7 +76,5 @@ fn main() { let _ = &mut x[Foo..]; let _ = &mut x[..Foo]; let _ = &mut x[Foo..Foo]; - unsafe { - assert_eq!(COUNT, 8); - } + assert_eq!(COUNT.load(Ordering::Relaxed), 8); } diff --git a/tests/ui/async-await/issues/issue-67611-static-mut-refs.rs b/tests/ui/async-await/issues/issue-67611-static-mut-refs.rs index 51e85931dbf20..547c7414526c2 100644 --- a/tests/ui/async-await/issues/issue-67611-static-mut-refs.rs +++ b/tests/ui/async-await/issues/issue-67611-static-mut-refs.rs @@ -2,6 +2,8 @@ //@ edition:2018 #![feature(if_let_guard)] +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] static mut A: [i32; 5] = [1, 2, 3, 4, 5]; diff --git a/tests/ui/binding/order-drop-with-match.rs b/tests/ui/binding/order-drop-with-match.rs index c12c5e4c62779..cd0ff73cf12da 100644 --- a/tests/ui/binding/order-drop-with-match.rs +++ b/tests/ui/binding/order-drop-with-match.rs @@ -5,6 +5,8 @@ // in ORDER matching up to when it ran. // Correct order is: matched, inner, outer +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] static mut ORDER: [usize; 3] = [0, 0, 0]; static mut INDEX: usize = 0; diff --git a/tests/ui/borrowck/borrowck-access-permissions.rs b/tests/ui/borrowck/borrowck-access-permissions.rs index be11286a523df..0e9e2bc1354a2 100644 --- a/tests/ui/borrowck/borrowck-access-permissions.rs +++ b/tests/ui/borrowck/borrowck-access-permissions.rs @@ -16,7 +16,6 @@ fn main() { let _y1 = &mut static_x; //~ ERROR [E0596] unsafe { let _y2 = &mut static_x_mut; - //~^ WARN mutable reference to mutable static is discouraged [static_mut_refs] } } diff --git a/tests/ui/borrowck/borrowck-access-permissions.stderr b/tests/ui/borrowck/borrowck-access-permissions.stderr index 36e259fa6e8ee..ade10dbbfbdb9 100644 --- a/tests/ui/borrowck/borrowck-access-permissions.stderr +++ b/tests/ui/borrowck/borrowck-access-permissions.stderr @@ -1,18 +1,3 @@ -warning: creating a mutable reference to mutable static is discouraged - --> $DIR/borrowck-access-permissions.rs:18:23 - | -LL | let _y2 = &mut static_x_mut; - | ^^^^^^^^^^^^^^^^^ mutable reference to mutable static - | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior - = note: `#[warn(static_mut_refs)]` on by default -help: use `addr_of_mut!` instead to create a raw pointer - | -LL | let _y2 = addr_of_mut!(static_x_mut); - | ~~~~~~~~~~~~~ + - error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable --> $DIR/borrowck-access-permissions.rs:10:19 | @@ -31,7 +16,7 @@ LL | let _y1 = &mut static_x; | ^^^^^^^^^^^^^ cannot borrow as mutable error[E0596]: cannot borrow `*box_x` as mutable, as `box_x` is not declared as mutable - --> $DIR/borrowck-access-permissions.rs:28:19 + --> $DIR/borrowck-access-permissions.rs:27:19 | LL | let _y1 = &mut *box_x; | ^^^^^^^^^^^ cannot borrow as mutable @@ -42,7 +27,7 @@ LL | let mut box_x = Box::new(1); | +++ error[E0596]: cannot borrow `*ref_x` as mutable, as it is behind a `&` reference - --> $DIR/borrowck-access-permissions.rs:37:19 + --> $DIR/borrowck-access-permissions.rs:36:19 | LL | let _y1 = &mut *ref_x; | ^^^^^^^^^^^ `ref_x` is a `&` reference, so the data it refers to cannot be borrowed as mutable @@ -53,7 +38,7 @@ LL | let ref_x = &mut x; | +++ error[E0596]: cannot borrow `*ptr_x` as mutable, as it is behind a `*const` pointer - --> $DIR/borrowck-access-permissions.rs:47:23 + --> $DIR/borrowck-access-permissions.rs:46:23 | LL | let _y1 = &mut *ptr_x; | ^^^^^^^^^^^ `ptr_x` is a `*const` pointer, so the data it refers to cannot be borrowed as mutable @@ -64,7 +49,7 @@ LL | let ptr_x: *const _ = &mut x; | +++ error[E0596]: cannot borrow `*foo_ref.f` as mutable, as it is behind a `&` reference - --> $DIR/borrowck-access-permissions.rs:60:18 + --> $DIR/borrowck-access-permissions.rs:59:18 | LL | let _y = &mut *foo_ref.f; | ^^^^^^^^^^^^^^^ `foo_ref` is a `&` reference, so the data it refers to cannot be borrowed as mutable @@ -74,6 +59,6 @@ help: consider changing this to be a mutable reference LL | let foo_ref = &mut foo; | +++ -error: aborting due to 6 previous errors; 1 warning emitted +error: aborting due to 6 previous errors For more information about this error, try `rustc --explain E0596`. diff --git a/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.stderr b/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.stderr index a727c9414c556..e4dfa6d7fba7c 100644 --- a/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.stderr +++ b/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.stderr @@ -4,14 +4,13 @@ warning: creating a mutable reference to mutable static is discouraged LL | let sfoo: *mut Foo = &mut SFOO; | ^^^^^^^^^ mutable reference to mutable static | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior + = note: for more information, see + = 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 = note: `#[warn(static_mut_refs)]` on by default -help: use `addr_of_mut!` instead to create a raw pointer +help: use `&raw mut` instead to create a raw pointer | -LL | let sfoo: *mut Foo = addr_of_mut!(SFOO); - | ~~~~~~~~~~~~~ + +LL | let sfoo: *mut Foo = &raw mut SFOO; + | ~~~~~~~~ warning: 1 warning emitted diff --git a/tests/ui/borrowck/issue-20801.rs b/tests/ui/borrowck/issue-20801.rs index 7e3d3703dc765..c3f136f2876b6 100644 --- a/tests/ui/borrowck/issue-20801.rs +++ b/tests/ui/borrowck/issue-20801.rs @@ -12,7 +12,6 @@ fn imm_ref() -> &'static T { fn mut_ref() -> &'static mut T { unsafe { &mut GLOBAL_MUT_T } - //~^ WARN mutable reference to mutable static is discouraged [static_mut_refs] } fn mut_ptr() -> *mut T { diff --git a/tests/ui/borrowck/issue-20801.stderr b/tests/ui/borrowck/issue-20801.stderr index 769b34831c120..5fda92634d8f3 100644 --- a/tests/ui/borrowck/issue-20801.stderr +++ b/tests/ui/borrowck/issue-20801.stderr @@ -1,20 +1,5 @@ -warning: creating a mutable reference to mutable static is discouraged - --> $DIR/issue-20801.rs:14:14 - | -LL | unsafe { &mut GLOBAL_MUT_T } - | ^^^^^^^^^^^^^^^^^ mutable reference to mutable static - | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior - = note: `#[warn(static_mut_refs)]` on by default -help: use `addr_of_mut!` instead to create a raw pointer - | -LL | unsafe { addr_of_mut!(GLOBAL_MUT_T) } - | ~~~~~~~~~~~~~ + - error[E0507]: cannot move out of a mutable reference - --> $DIR/issue-20801.rs:27:22 + --> $DIR/issue-20801.rs:26:22 | LL | let a = unsafe { *mut_ref() }; | ^^^^^^^^^^ move occurs because value has type `T`, which does not implement the `Copy` trait @@ -34,7 +19,7 @@ LL + let a = unsafe { mut_ref() }; | error[E0507]: cannot move out of a shared reference - --> $DIR/issue-20801.rs:30:22 + --> $DIR/issue-20801.rs:29:22 | LL | let b = unsafe { *imm_ref() }; | ^^^^^^^^^^ move occurs because value has type `T`, which does not implement the `Copy` trait @@ -54,7 +39,7 @@ LL + let b = unsafe { imm_ref() }; | error[E0507]: cannot move out of a raw pointer - --> $DIR/issue-20801.rs:33:22 + --> $DIR/issue-20801.rs:32:22 | LL | let c = unsafe { *mut_ptr() }; | ^^^^^^^^^^ move occurs because value has type `T`, which does not implement the `Copy` trait @@ -69,7 +54,7 @@ LL | let c = unsafe { *mut_ptr() }; | ---------- you could clone this value error[E0507]: cannot move out of a raw pointer - --> $DIR/issue-20801.rs:36:22 + --> $DIR/issue-20801.rs:35:22 | LL | let d = unsafe { *const_ptr() }; | ^^^^^^^^^^^^ move occurs because value has type `T`, which does not implement the `Copy` trait @@ -83,6 +68,6 @@ LL | struct T(u8); LL | let d = unsafe { *const_ptr() }; | ------------ you could clone this value -error: aborting due to 4 previous errors; 1 warning emitted +error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0507`. diff --git a/tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.rs b/tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.rs index c3909d0596339..e22bf42e02142 100644 --- a/tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.rs +++ b/tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.rs @@ -10,7 +10,6 @@ mod borrowck_closures_unique { //~^ ERROR is not declared as mutable unsafe { c1(&mut Y); - //~^ WARN mutable reference to mutable static is discouraged [static_mut_refs] } } } @@ -25,7 +24,6 @@ mod borrowck_closures_unique_grandparent { }; unsafe { c1(&mut Z); - //~^ WARN mutable reference to mutable static is discouraged [static_mut_refs] } } } @@ -62,7 +60,6 @@ fn main() { static mut X: isize = 2; unsafe { borrowck_closures_unique::e(&mut X); - //~^ WARN mutable reference to mutable static is discouraged [static_mut_refs] } mutability_errors::capture_assign_whole((1000,)); diff --git a/tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.stderr b/tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.stderr index 72fd0d8ce161e..6fc8d3a6c35c7 100644 --- a/tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.stderr +++ b/tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.stderr @@ -1,46 +1,3 @@ -warning: creating a mutable reference to mutable static is discouraged - --> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:12:16 - | -LL | c1(&mut Y); - | ^^^^^^ mutable reference to mutable static - | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior - = note: `#[warn(static_mut_refs)]` on by default -help: use `addr_of_mut!` instead to create a raw pointer - | -LL | c1(addr_of_mut!(Y)); - | ~~~~~~~~~~~~~ + - -warning: creating a mutable reference to mutable static is discouraged - --> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:27:16 - | -LL | c1(&mut Z); - | ^^^^^^ mutable reference to mutable static - | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior -help: use `addr_of_mut!` instead to create a raw pointer - | -LL | c1(addr_of_mut!(Z)); - | ~~~~~~~~~~~~~ + - -warning: creating a mutable reference to mutable static is discouraged - --> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:64:37 - | -LL | borrowck_closures_unique::e(&mut X); - | ^^^^^^ mutable reference to mutable static - | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior -help: use `addr_of_mut!` instead to create a raw pointer - | -LL | borrowck_closures_unique::e(addr_of_mut!(X)); - | ~~~~~~~~~~~~~ + - error[E0594]: cannot assign to `x`, as it is not declared as mutable --> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:9:46 | @@ -53,7 +10,7 @@ LL | pub fn e(mut x: &'static mut isize) { | +++ error[E0594]: cannot assign to `x`, as it is not declared as mutable - --> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:22:50 + --> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:21:50 | LL | let mut c2 = |y: &'static mut isize| x = y; | ^^^^^ cannot assign @@ -64,7 +21,7 @@ LL | pub fn ee(mut x: &'static mut isize) { | +++ error[E0594]: cannot assign to `x`, as it is not declared as mutable - --> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:37:13 + --> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:35:13 | LL | x = (1,); | ^^^^^^^^ cannot assign @@ -75,7 +32,7 @@ LL | pub fn capture_assign_whole(mut x: (i32,)) { | +++ error[E0594]: cannot assign to `x.0`, as `x` is not declared as mutable - --> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:43:13 + --> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:41:13 | LL | x.0 = 1; | ^^^^^^^ cannot assign @@ -86,7 +43,7 @@ LL | pub fn capture_assign_part(mut x: (i32,)) { | +++ error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable - --> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:49:13 + --> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:47:13 | LL | &mut x; | ^^^^^^ cannot borrow as mutable @@ -97,7 +54,7 @@ LL | pub fn capture_reborrow_whole(mut x: (i32,)) { | +++ error[E0596]: cannot borrow `x.0` as mutable, as `x` is not declared as mutable - --> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:55:13 + --> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:53:13 | LL | &mut x.0; | ^^^^^^^^ cannot borrow as mutable @@ -107,7 +64,7 @@ help: consider changing this to be mutable LL | pub fn capture_reborrow_part(mut x: (i32,)) { | +++ -error: aborting due to 6 previous errors; 3 warnings emitted +error: aborting due to 6 previous errors Some errors have detailed explanations: E0594, E0596. For more information about an error, try `rustc --explain E0594`. diff --git a/tests/ui/consts/const_let_assign2.stderr b/tests/ui/consts/const_let_assign2.stderr index 87b94a7be67e2..9a1b84dbf09f5 100644 --- a/tests/ui/consts/const_let_assign2.stderr +++ b/tests/ui/consts/const_let_assign2.stderr @@ -4,14 +4,13 @@ warning: creating a mutable reference to mutable static is discouraged LL | let ptr = unsafe { &mut BB }; | ^^^^^^^ mutable reference to mutable static | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior + = note: for more information, see + = 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 = note: `#[warn(static_mut_refs)]` on by default -help: use `addr_of_mut!` instead to create a raw pointer +help: use `&raw mut` instead to create a raw pointer | -LL | let ptr = unsafe { addr_of_mut!(BB) }; - | ~~~~~~~~~~~~~ + +LL | let ptr = unsafe { &raw mut BB }; + | ~~~~~~~~ warning: 1 warning emitted diff --git a/tests/ui/consts/static-mut-refs.rs b/tests/ui/consts/static-mut-refs.rs index d4ebfbbf17a86..c0d0bb613325c 100644 --- a/tests/ui/consts/static-mut-refs.rs +++ b/tests/ui/consts/static-mut-refs.rs @@ -1,6 +1,9 @@ //@ run-pass #![allow(dead_code)] +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + // Checks that mutable static items can have mutable slices and other references diff --git a/tests/ui/coroutine/static-mut-reference-across-yield.rs b/tests/ui/coroutine/static-mut-reference-across-yield.rs index 40d5fdf2d5736..d45d6e6428b9c 100644 --- a/tests/ui/coroutine/static-mut-reference-across-yield.rs +++ b/tests/ui/coroutine/static-mut-reference-across-yield.rs @@ -1,6 +1,8 @@ //@ build-pass #![feature(coroutines, stmt_expr_attributes)] +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] static mut A: [i32; 5] = [1, 2, 3, 4, 5]; diff --git a/tests/ui/drop/drop-struct-as-object.rs b/tests/ui/drop/drop-struct-as-object.rs index 07c8950f1b2ba..09f26eea42a7d 100644 --- a/tests/ui/drop/drop-struct-as-object.rs +++ b/tests/ui/drop/drop-struct-as-object.rs @@ -5,7 +5,9 @@ // Test that destructor on a struct runs successfully after the struct // is boxed and converted to an object. -static mut value: usize = 0; +use std::sync::atomic::{AtomicUsize, Ordering}; + +static value: AtomicUsize = AtomicUsize::new(0); struct Cat { name : usize, @@ -21,7 +23,7 @@ impl Dummy for Cat { impl Drop for Cat { fn drop(&mut self) { - unsafe { value = self.name; } + value.store(self.name, Ordering::Relaxed); } } @@ -30,7 +32,5 @@ pub fn main() { let x = Box::new(Cat {name: 22}); let nyan: Box = x as Box; } - unsafe { - assert_eq!(value, 22); - } + assert_eq!(value.load(Ordering::Relaxed), 22); } diff --git a/tests/ui/drop/drop-struct-as-object.stderr b/tests/ui/drop/drop-struct-as-object.stderr index 10527c968ed70..b0d997f0a043c 100644 --- a/tests/ui/drop/drop-struct-as-object.stderr +++ b/tests/ui/drop/drop-struct-as-object.stderr @@ -1,5 +1,5 @@ warning: method `get` is never used - --> $DIR/drop-struct-as-object.rs:15:8 + --> $DIR/drop-struct-as-object.rs:17:8 | LL | trait Dummy { | ----- method in this trait diff --git a/tests/ui/drop/issue-23338-ensure-param-drop-order.rs b/tests/ui/drop/issue-23338-ensure-param-drop-order.rs index 1fa68a2e73813..6fee4520cbcc1 100644 --- a/tests/ui/drop/issue-23338-ensure-param-drop-order.rs +++ b/tests/ui/drop/issue-23338-ensure-param-drop-order.rs @@ -1,5 +1,7 @@ //@ run-pass #![allow(non_upper_case_globals)] +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] // This test is ensuring that parameters are indeed dropped after // temporaries in a fn body. @@ -91,7 +93,6 @@ pub mod d { pub fn max_width() -> u32 { unsafe { (mem::size_of_val(&trails) * 8) as u32 - //~^ WARN shared reference to mutable static is discouraged [static_mut_refs] } } diff --git a/tests/ui/drop/issue-23338-ensure-param-drop-order.stderr b/tests/ui/drop/issue-23338-ensure-param-drop-order.stderr deleted file mode 100644 index 9126e6023913d..0000000000000 --- a/tests/ui/drop/issue-23338-ensure-param-drop-order.stderr +++ /dev/null @@ -1,17 +0,0 @@ -warning: creating a shared reference to mutable static is discouraged - --> $DIR/issue-23338-ensure-param-drop-order.rs:93:31 - | -LL | (mem::size_of_val(&trails) * 8) as u32 - | ^^^^^^^ shared reference to mutable static - | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior - = note: `#[warn(static_mut_refs)]` on by default -help: use `addr_of!` instead to create a raw pointer - | -LL | (mem::size_of_val(addr_of!(trails)) * 8) as u32 - | ~~~~~~~~~ + - -warning: 1 warning emitted - diff --git a/tests/ui/drop/issue-23611-enum-swap-in-drop.rs b/tests/ui/drop/issue-23611-enum-swap-in-drop.rs index 1afaff0f735be..410b07b16fc77 100644 --- a/tests/ui/drop/issue-23611-enum-swap-in-drop.rs +++ b/tests/ui/drop/issue-23611-enum-swap-in-drop.rs @@ -1,5 +1,7 @@ //@ run-pass #![allow(non_upper_case_globals)] +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] // Issue 23611: this test is ensuring that, for an instance `X` of the // enum `E`, if you swap in a different variant during the execution @@ -187,7 +189,6 @@ pub mod d { pub fn max_width() -> u32 { unsafe { (mem::size_of_val(&trails) * 8) as u32 - //~^ WARN shared reference to mutable static is discouraged [static_mut_refs] } } diff --git a/tests/ui/drop/issue-23611-enum-swap-in-drop.stderr b/tests/ui/drop/issue-23611-enum-swap-in-drop.stderr deleted file mode 100644 index 6da8741680274..0000000000000 --- a/tests/ui/drop/issue-23611-enum-swap-in-drop.stderr +++ /dev/null @@ -1,17 +0,0 @@ -warning: creating a shared reference to mutable static is discouraged - --> $DIR/issue-23611-enum-swap-in-drop.rs:189:31 - | -LL | (mem::size_of_val(&trails) * 8) as u32 - | ^^^^^^^ shared reference to mutable static - | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior - = note: `#[warn(static_mut_refs)]` on by default -help: use `addr_of!` instead to create a raw pointer - | -LL | (mem::size_of_val(addr_of!(trails)) * 8) as u32 - | ~~~~~~~~~ + - -warning: 1 warning emitted - diff --git a/tests/ui/drop/issue-48962.rs b/tests/ui/drop/issue-48962.rs index 428a6ca6cd21b..98d3af49ac1ec 100644 --- a/tests/ui/drop/issue-48962.rs +++ b/tests/ui/drop/issue-48962.rs @@ -1,6 +1,9 @@ //@ run-pass #![allow(unused_must_use)] // Test that we are able to reinitialize box with moved referent +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + static mut ORDER: [usize; 3] = [0, 0, 0]; static mut INDEX: usize = 0; diff --git a/tests/ui/drop/repeat-drop.rs b/tests/ui/drop/repeat-drop.rs index b83bee8c1bf82..240377a486bd0 100644 --- a/tests/ui/drop/repeat-drop.rs +++ b/tests/ui/drop/repeat-drop.rs @@ -3,24 +3,26 @@ #![allow(dropping_references, dropping_copy_types)] -static mut CHECK: usize = 0; +use std::sync::atomic::{AtomicUsize, Ordering}; + +static CHECK: AtomicUsize = AtomicUsize::new(0); struct DropChecker(usize); impl Drop for DropChecker { fn drop(&mut self) { - unsafe { - if CHECK != self.0 - 1 { - panic!("Found {}, should have found {}", CHECK, self.0 - 1); - } - CHECK = self.0; + if CHECK.load(Ordering::Relaxed) != self.0 - 1 { + panic!("Found {}, should have found {}", CHECK.load(Ordering::Relaxed), self.0 - 1); } + CHECK.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |_c| { + Some(self.0) + }).unwrap(); } } macro_rules! check_drops { ($l:literal) => { - unsafe { assert_eq!(CHECK, $l) } + assert_eq!(CHECK.load(Ordering::Relaxed), $l) }; } @@ -33,7 +35,7 @@ impl Drop for DropPanic { } fn value_zero() { - unsafe { CHECK = 0 }; + CHECK.store(0, Ordering::Relaxed); let foo = DropChecker(1); let v: [DropChecker; 0] = [foo; 0]; check_drops!(1); @@ -42,7 +44,7 @@ fn value_zero() { } fn value_one() { - unsafe { CHECK = 0 }; + CHECK.store(0, Ordering::Relaxed); let foo = DropChecker(1); let v: [DropChecker; 1] = [foo; 1]; check_drops!(0); @@ -53,7 +55,7 @@ fn value_one() { const DROP_CHECKER: DropChecker = DropChecker(1); fn const_zero() { - unsafe { CHECK = 0 }; + CHECK.store(0, Ordering::Relaxed); let v: [DropChecker; 0] = [DROP_CHECKER; 0]; check_drops!(0); std::mem::drop(v); @@ -61,7 +63,7 @@ fn const_zero() { } fn const_one() { - unsafe { CHECK = 0 }; + CHECK.store(0, Ordering::Relaxed); let v: [DropChecker; 1] = [DROP_CHECKER; 1]; check_drops!(0); std::mem::drop(v); @@ -69,7 +71,7 @@ fn const_one() { } fn const_generic_zero() { - unsafe { CHECK = 0 }; + CHECK.store(0, Ordering::Relaxed); let v: [DropChecker; N] = [DROP_CHECKER; N]; check_drops!(0); std::mem::drop(v); @@ -77,7 +79,7 @@ fn const_generic_zero() { } fn const_generic_one() { - unsafe { CHECK = 0 }; + CHECK.store(0, Ordering::Relaxed); let v: [DropChecker; N] = [DROP_CHECKER; N]; check_drops!(0); std::mem::drop(v); @@ -87,7 +89,7 @@ fn const_generic_one() { // Make sure that things are allowed to promote as expected fn allow_promote() { - unsafe { CHECK = 0 }; + CHECK.store(0, Ordering::Relaxed); let foo = DropChecker(1); let v: &'static [DropChecker; 0] = &[foo; 0]; check_drops!(1); @@ -97,7 +99,7 @@ fn allow_promote() { // Verify that unwinding in the drop causes the right things to drop in the right order fn on_unwind() { - unsafe { CHECK = 0 }; + CHECK.store(0, Ordering::Relaxed); std::panic::catch_unwind(|| { let panic = DropPanic; let _local = DropChecker(2); diff --git a/tests/ui/issues/issue-17302.rs b/tests/ui/drop/static-issue-17302.rs similarity index 82% rename from tests/ui/issues/issue-17302.rs rename to tests/ui/drop/static-issue-17302.rs index c499cc5281f3a..060f5564efdae 100644 --- a/tests/ui/issues/issue-17302.rs +++ b/tests/ui/drop/static-issue-17302.rs @@ -1,5 +1,8 @@ //@ run-pass +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + static mut DROPPED: [bool; 2] = [false, false]; struct A(usize); diff --git a/tests/ui/for-loop-while/cleanup-rvalue-during-if-and-while.rs b/tests/ui/for-loop-while/cleanup-rvalue-during-if-and-while.rs index fef9f24d462d8..a9effa03c75a4 100644 --- a/tests/ui/for-loop-while/cleanup-rvalue-during-if-and-while.rs +++ b/tests/ui/for-loop-while/cleanup-rvalue-during-if-and-while.rs @@ -2,13 +2,15 @@ // This test verifies that temporaries created for `while`'s and `if` // conditions are dropped after the condition is evaluated. +use std::sync::atomic::{AtomicUsize, Ordering}; + struct Temporary; -static mut DROPPED: isize = 0; +static DROPPED: AtomicUsize = AtomicUsize::new(0); impl Drop for Temporary { fn drop(&mut self) { - unsafe { DROPPED += 1; } + DROPPED.fetch_add(1, Ordering::Relaxed); } } @@ -27,7 +29,7 @@ pub fn main() { // `drop` 6 times. while borrow().do_stuff() { i += 1; - unsafe { assert_eq!(DROPPED, i) } + assert_eq!(DROPPED.load(Ordering::Relaxed), i); if i > 5 { break; } @@ -36,6 +38,6 @@ pub fn main() { // This if condition should // call it 1 time if borrow().do_stuff() { - unsafe { assert_eq!(DROPPED, i + 1) } + assert_eq!(DROPPED.load(Ordering::Relaxed), i + 1); } } diff --git a/tests/ui/functional-struct-update/functional-struct-update-respects-privacy.rs b/tests/ui/functional-struct-update/functional-struct-update-respects-privacy.rs index 500633edf12de..a05d20ffbd280 100644 --- a/tests/ui/functional-struct-update/functional-struct-update-respects-privacy.rs +++ b/tests/ui/functional-struct-update/functional-struct-update-respects-privacy.rs @@ -2,6 +2,10 @@ // The `foo` module attempts to maintains an invariant that each `S` // has a unique `u64` id. + +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + use self::foo::S; mod foo { use std::cell::{UnsafeCell}; diff --git a/tests/ui/functional-struct-update/functional-struct-update-respects-privacy.stderr b/tests/ui/functional-struct-update/functional-struct-update-respects-privacy.stderr index 692d98bc53c82..8924800ef2796 100644 --- a/tests/ui/functional-struct-update/functional-struct-update-respects-privacy.stderr +++ b/tests/ui/functional-struct-update/functional-struct-update-respects-privacy.stderr @@ -1,5 +1,5 @@ error[E0451]: field `secret_uid` of struct `S` is private - --> $DIR/functional-struct-update-respects-privacy.rs:28:49 + --> $DIR/functional-struct-update-respects-privacy.rs:32:49 | LL | let s_2 = foo::S { b: format!("ess two"), ..s_1 }; // FRU ... | ^^^ field `secret_uid` is private diff --git a/tests/ui/issues/issue-10734.rs b/tests/ui/issues/issue-10734.rs index 8daa401748c7c..8256e3fa1244f 100644 --- a/tests/ui/issues/issue-10734.rs +++ b/tests/ui/issues/issue-10734.rs @@ -1,7 +1,9 @@ //@ run-pass #![allow(non_upper_case_globals)] -static mut drop_count: usize = 0; +use std::sync::atomic::{AtomicUsize, Ordering}; + +static drop_count: AtomicUsize = AtomicUsize::new(0); struct Foo { dropped: bool @@ -13,7 +15,7 @@ impl Drop for Foo { assert!(!self.dropped); self.dropped = true; // And record the fact that we dropped for verification later - unsafe { drop_count += 1; } + drop_count.fetch_add(1, Ordering::Relaxed); } } @@ -23,7 +25,7 @@ pub fn main() { let _a = Foo{ dropped: false }; } // Check that we dropped already (as expected from a `{ expr }`). - unsafe { assert_eq!(drop_count, 1); } + assert_eq!(drop_count.load(Ordering::Relaxed), 1); // An `if false {} else { expr }` statement should compile the same as `{ expr }`. if false { @@ -32,5 +34,5 @@ pub fn main() { let _a = Foo{ dropped: false }; } // Check that we dropped already (as expected from a `{ expr }`). - unsafe { assert_eq!(drop_count, 2); } + assert_eq!(drop_count.load(Ordering::Relaxed), 2); } diff --git a/tests/ui/issues/issue-15858.rs b/tests/ui/issues/issue-15858.rs index 2d4aac01fbe8e..62b6a5dc6ac64 100644 --- a/tests/ui/issues/issue-15858.rs +++ b/tests/ui/issues/issue-15858.rs @@ -1,5 +1,8 @@ //@ run-pass -static mut DROP_RAN: bool = false; + +use std::sync::atomic::{AtomicBool, Ordering}; + +static DROP_RAN: AtomicBool = AtomicBool::new(false); trait Bar { fn do_something(&mut self); //~ WARN method `do_something` is never used @@ -16,9 +19,7 @@ struct Foo(#[allow(dead_code)] B); impl Drop for Foo { fn drop(&mut self) { - unsafe { - DROP_RAN = true; - } + DROP_RAN.store(true, Ordering::Relaxed); } } @@ -27,7 +28,5 @@ fn main() { { let _x: Foo = Foo(BarImpl); } - unsafe { - assert_eq!(DROP_RAN, true); - } + assert_eq!(DROP_RAN.load(Ordering::Relaxed), true); } diff --git a/tests/ui/issues/issue-15858.stderr b/tests/ui/issues/issue-15858.stderr index f36bcc21bd7fb..96bede23098c1 100644 --- a/tests/ui/issues/issue-15858.stderr +++ b/tests/ui/issues/issue-15858.stderr @@ -1,5 +1,5 @@ warning: method `do_something` is never used - --> $DIR/issue-15858.rs:5:8 + --> $DIR/issue-15858.rs:8:8 | LL | trait Bar { | --- method in this trait diff --git a/tests/ui/issues/issue-16151.rs b/tests/ui/issues/issue-16151.rs index 20a3b5a04da23..b18108e0a8ad8 100644 --- a/tests/ui/issues/issue-16151.rs +++ b/tests/ui/issues/issue-16151.rs @@ -1,5 +1,8 @@ //@ run-pass +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + use std::mem; static mut DROP_COUNT: usize = 0; diff --git a/tests/ui/issues/issue-39367.rs b/tests/ui/issues/issue-39367.rs index dd16d4da1bdd2..937d6c4b9e0b0 100644 --- a/tests/ui/issues/issue-39367.rs +++ b/tests/ui/issues/issue-39367.rs @@ -20,6 +20,7 @@ fn arena() -> &'static ArenaSet> { static mut ONCE: Once = Once::new(); ONCE.call_once(|| { + //~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] DATA = transmute ::>>, *const ArenaSet>> (Box::new(__static_ref_initialize())); diff --git a/tests/ui/issues/issue-39367.stderr b/tests/ui/issues/issue-39367.stderr new file mode 100644 index 0000000000000..333c9f9634a31 --- /dev/null +++ b/tests/ui/issues/issue-39367.stderr @@ -0,0 +1,17 @@ +warning: creating a shared reference to mutable static is discouraged + --> $DIR/issue-39367.rs:22:13 + | +LL | / ONCE.call_once(|| { +LL | | +LL | | DATA = transmute +LL | | ::>>, *const ArenaSet>> +LL | | (Box::new(__static_ref_initialize())); +LL | | }); + | |______________^ shared reference to mutable static + | + = note: for more information, see + = 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: `#[warn(static_mut_refs)]` on by default + +warning: 1 warning emitted + diff --git a/tests/ui/issues/issue-4734.rs b/tests/ui/issues/issue-4734.rs index 938d7064789de..58aa0179693e7 100644 --- a/tests/ui/issues/issue-4734.rs +++ b/tests/ui/issues/issue-4734.rs @@ -3,7 +3,8 @@ // Ensures that destructors are run for expressions of the form "e;" where // `e` is a type which requires a destructor. - +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] #![allow(path_statements)] struct A { n: isize } diff --git a/tests/ui/issues/issue-54410.rs b/tests/ui/issues/issue-54410.rs index 208be6f221c25..e3e8ca985b970 100644 --- a/tests/ui/issues/issue-54410.rs +++ b/tests/ui/issues/issue-54410.rs @@ -5,5 +5,4 @@ extern "C" { fn main() { println!("{:p}", unsafe { &symbol }); - //~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] } diff --git a/tests/ui/issues/issue-54410.stderr b/tests/ui/issues/issue-54410.stderr index 6cc5cd95e2f7d..97e5990750e3c 100644 --- a/tests/ui/issues/issue-54410.stderr +++ b/tests/ui/issues/issue-54410.stderr @@ -6,21 +6,6 @@ LL | pub static mut symbol: [i8]; | = help: the trait `Sized` is not implemented for `[i8]` -warning: creating a shared reference to mutable static is discouraged - --> $DIR/issue-54410.rs:7:31 - | -LL | println!("{:p}", unsafe { &symbol }); - | ^^^^^^^ shared reference to mutable static - | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior - = note: `#[warn(static_mut_refs)]` on by default -help: use `addr_of!` instead to create a raw pointer - | -LL | println!("{:p}", unsafe { addr_of!(symbol) }); - | ~~~~~~~~~ + - -error: aborting due to 1 previous error; 1 warning emitted +error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/issues/issue-6892.rs b/tests/ui/issues/issue-6892.rs index aff00cf60bacd..7d99aef4ac5c1 100644 --- a/tests/ui/issues/issue-6892.rs +++ b/tests/ui/issues/issue-6892.rs @@ -3,6 +3,8 @@ // Ensures that destructors are run for expressions of the form "let _ = e;" // where `e` is a type which requires a destructor. +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] struct Foo; struct Bar { x: isize } diff --git a/tests/ui/issues/issue-8860.rs b/tests/ui/issues/issue-8860.rs index 67e9a276ae487..b2029e755e31e 100644 --- a/tests/ui/issues/issue-8860.rs +++ b/tests/ui/issues/issue-8860.rs @@ -1,17 +1,17 @@ //@ run-pass #![allow(dead_code)] -static mut DROP: isize = 0; -static mut DROP_S: isize = 0; -static mut DROP_T: isize = 0; +use std::sync::atomic::{AtomicUsize, Ordering}; + +static DROP: AtomicUsize = AtomicUsize::new(0); +static DROP_S: AtomicUsize = AtomicUsize::new(0); +static DROP_T: AtomicUsize = AtomicUsize::new(0); struct S; impl Drop for S { fn drop(&mut self) { - unsafe { - DROP_S += 1; - DROP += 1; - } + DROP_S.fetch_add(1, Ordering::Relaxed); + DROP.fetch_add(1, Ordering::Relaxed); } } fn f(ref _s: S) {} @@ -19,10 +19,8 @@ fn f(ref _s: S) {} struct T { i: isize } impl Drop for T { fn drop(&mut self) { - unsafe { - DROP_T += 1; - DROP += 1; - } + DROP_T.fetch_add(1, Ordering::Relaxed); + DROP.fetch_add(1, Ordering::Relaxed); } } fn g(ref _t: T) {} @@ -30,20 +28,16 @@ fn g(ref _t: T) {} fn do_test() { let s = S; f(s); - unsafe { - assert_eq!(1, DROP); - assert_eq!(1, DROP_S); - } + assert_eq!(1, DROP.load(Ordering::Relaxed)); + assert_eq!(1, DROP_S.load(Ordering::Relaxed)); let t = T { i: 1 }; g(t); - unsafe { assert_eq!(1, DROP_T); } + assert_eq!(1, DROP_T.load(Ordering::Relaxed)); } fn main() { do_test(); - unsafe { - assert_eq!(2, DROP); - assert_eq!(1, DROP_S); - assert_eq!(1, DROP_T); - } + assert_eq!(2, DROP.load(Ordering::Relaxed)); + assert_eq!(1, DROP_S.load(Ordering::Relaxed)); + assert_eq!(1, DROP_T.load(Ordering::Relaxed)); } diff --git a/tests/ui/link-section.rs b/tests/ui/link-section.rs index 1a791b88ef957..a8de8c2e1e77d 100644 --- a/tests/ui/link-section.rs +++ b/tests/ui/link-section.rs @@ -1,5 +1,8 @@ //@ run-pass +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + #![allow(non_upper_case_globals)] #[cfg(not(target_vendor = "apple"))] #[link_section = ".moretext"] diff --git a/tests/ui/linkage-attr/linkage-attr-mutable-static.rs b/tests/ui/linkage-attr/linkage-attr-mutable-static.rs index ed11947f59e74..cc93f61e28f37 100644 --- a/tests/ui/linkage-attr/linkage-attr-mutable-static.rs +++ b/tests/ui/linkage-attr/linkage-attr-mutable-static.rs @@ -2,6 +2,8 @@ //! them at runtime, so deny mutable statics with #[linkage]. #![feature(linkage)] +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] fn main() { #[rustfmt::skip] diff --git a/tests/ui/linkage-attr/linkage-attr-mutable-static.stderr b/tests/ui/linkage-attr/linkage-attr-mutable-static.stderr index ad9997690475b..50d5d8121969f 100644 --- a/tests/ui/linkage-attr/linkage-attr-mutable-static.stderr +++ b/tests/ui/linkage-attr/linkage-attr-mutable-static.stderr @@ -1,5 +1,5 @@ error: extern mutable statics are not allowed with `#[linkage]` - --> $DIR/linkage-attr-mutable-static.rs:9:9 + --> $DIR/linkage-attr-mutable-static.rs:11:9 | LL | #[linkage = "extern_weak"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/tests/ui/lint/static-mut-refs.e2021.stderr b/tests/ui/lint/static-mut-refs.e2021.stderr new file mode 100644 index 0000000000000..09f560652e7bf --- /dev/null +++ b/tests/ui/lint/static-mut-refs.e2021.stderr @@ -0,0 +1,143 @@ +warning: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:39:18 + | +LL | let _y = &X; + | ^^ shared reference to mutable static + | + = note: for more information, see + = 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: `#[warn(static_mut_refs)]` on by default +help: use `&raw const` instead to create a raw pointer + | +LL | let _y = &raw const X; + | ~~~~~~~~~~ + +warning: creating a mutable reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:43:18 + | +LL | let _y = &mut X; + | ^^^^^^ mutable reference to mutable static + | + = note: for more information, see + = 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 `&raw mut` instead to create a raw pointer + | +LL | let _y = &raw mut X; + | ~~~~~~~~ + +warning: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:51:22 + | +LL | let ref _a = X; + | ^ shared reference to mutable static + | + = note: for more information, see + = 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 + +warning: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:55:25 + | +LL | let (_b, _c) = (&X, &Y); + | ^^ shared reference to mutable static + | + = note: for more information, see + = 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 `&raw const` instead to create a raw pointer + | +LL | let (_b, _c) = (&raw const X, &Y); + | ~~~~~~~~~~ + +warning: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:55:29 + | +LL | let (_b, _c) = (&X, &Y); + | ^^ shared reference to mutable static + | + = note: for more information, see + = 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 `&raw const` instead to create a raw pointer + | +LL | let (_b, _c) = (&X, &raw const Y); + | ~~~~~~~~~~ + +warning: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:61:13 + | +LL | foo(&X); + | ^^ shared reference to mutable static + | + = note: for more information, see + = 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 `&raw const` instead to create a raw pointer + | +LL | foo(&raw const X); + | ~~~~~~~~~~ + +warning: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:67:17 + | +LL | let _ = Z.len(); + | ^^^^^^^ shared reference to mutable static + | + = note: for more information, see + = 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 + +warning: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:73:33 + | +LL | let _ = format!("{:?}", Z); + | ^ shared reference to mutable static + | + = note: for more information, see + = 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 + +warning: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:77:18 + | +LL | let _v = &A.value; + | ^^^^^^^^ shared reference to mutable static + | + = note: for more information, see + = 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 `&raw const` instead to create a raw pointer + | +LL | let _v = &raw const A.value; + | ~~~~~~~~~~ + +warning: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:81:18 + | +LL | let _s = &A.s.value; + | ^^^^^^^^^^ shared reference to mutable static + | + = note: for more information, see + = 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 `&raw const` instead to create a raw pointer + | +LL | let _s = &raw const A.s.value; + | ~~~~~~~~~~ + +warning: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:85:22 + | +LL | let ref _v = A.value; + | ^^^^^^^ shared reference to mutable static + | + = note: for more information, see + = 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 + +warning: creating a mutable reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:15:14 + | +LL | &mut ($x.0) + | ^^^^^^ mutable reference to mutable static +... +LL | let _x = bar!(FOO); + | --------- in this macro invocation + | + = note: for more information, see + = 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 + = note: this warning originates in the macro `bar` (in Nightly builds, run with -Z macro-backtrace for more info) + +warning: 12 warnings emitted + diff --git a/tests/ui/lint/static-mut-refs.e2024.stderr b/tests/ui/lint/static-mut-refs.e2024.stderr new file mode 100644 index 0000000000000..2d2a4f7afe00f --- /dev/null +++ b/tests/ui/lint/static-mut-refs.e2024.stderr @@ -0,0 +1,143 @@ +error: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:39:18 + | +LL | let _y = &X; + | ^^ shared reference to mutable static + | + = note: for more information, see + = 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: `#[deny(static_mut_refs)]` on by default +help: use `&raw const` instead to create a raw pointer + | +LL | let _y = &raw const X; + | ~~~~~~~~~~ + +error: creating a mutable reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:43:18 + | +LL | let _y = &mut X; + | ^^^^^^ mutable reference to mutable static + | + = note: for more information, see + = 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 `&raw mut` instead to create a raw pointer + | +LL | let _y = &raw mut X; + | ~~~~~~~~ + +error: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:51:22 + | +LL | let ref _a = X; + | ^ shared reference to mutable static + | + = note: for more information, see + = 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 + +error: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:55:25 + | +LL | let (_b, _c) = (&X, &Y); + | ^^ shared reference to mutable static + | + = note: for more information, see + = 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 `&raw const` instead to create a raw pointer + | +LL | let (_b, _c) = (&raw const X, &Y); + | ~~~~~~~~~~ + +error: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:55:29 + | +LL | let (_b, _c) = (&X, &Y); + | ^^ shared reference to mutable static + | + = note: for more information, see + = 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 `&raw const` instead to create a raw pointer + | +LL | let (_b, _c) = (&X, &raw const Y); + | ~~~~~~~~~~ + +error: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:61:13 + | +LL | foo(&X); + | ^^ shared reference to mutable static + | + = note: for more information, see + = 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 `&raw const` instead to create a raw pointer + | +LL | foo(&raw const X); + | ~~~~~~~~~~ + +error: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:67:17 + | +LL | let _ = Z.len(); + | ^^^^^^^ shared reference to mutable static + | + = note: for more information, see + = 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 + +error: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:73:33 + | +LL | let _ = format!("{:?}", Z); + | ^ shared reference to mutable static + | + = note: for more information, see + = 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 + +error: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:77:18 + | +LL | let _v = &A.value; + | ^^^^^^^^ shared reference to mutable static + | + = note: for more information, see + = 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 `&raw const` instead to create a raw pointer + | +LL | let _v = &raw const A.value; + | ~~~~~~~~~~ + +error: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:81:18 + | +LL | let _s = &A.s.value; + | ^^^^^^^^^^ shared reference to mutable static + | + = note: for more information, see + = 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 `&raw const` instead to create a raw pointer + | +LL | let _s = &raw const A.s.value; + | ~~~~~~~~~~ + +error: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:85:22 + | +LL | let ref _v = A.value; + | ^^^^^^^ shared reference to mutable static + | + = note: for more information, see + = 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 + +error: creating a mutable reference to mutable static is discouraged + --> $DIR/static-mut-refs.rs:15:14 + | +LL | &mut ($x.0) + | ^^^^^^ mutable reference to mutable static +... +LL | let _x = bar!(FOO); + | --------- in this macro invocation + | + = note: for more information, see + = 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 + = note: this error originates in the macro `bar` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 12 previous errors + diff --git a/tests/ui/lint/static-mut-refs.rs b/tests/ui/lint/static-mut-refs.rs new file mode 100644 index 0000000000000..3d84d7dbf40de --- /dev/null +++ b/tests/ui/lint/static-mut-refs.rs @@ -0,0 +1,95 @@ +// Test `static_mut_refs` lint. + +//@ revisions: e2021 e2024 + +//@ [e2021] edition:2021 +//@ [e2021] run-pass + +//@ [e2024] edition:2024 +//@ [e2024] compile-flags: -Zunstable-options + +static mut FOO: (u32, u32) = (1, 2); + +macro_rules! bar { + ($x:expr) => { + &mut ($x.0) + //[e2021]~^ WARN creating a mutable reference to mutable static is discouraged [static_mut_refs] + //[e2024]~^^ ERROR creating a mutable reference to mutable static is discouraged [static_mut_refs] + }; +} + +static mut STATIC: i64 = 1; + +fn main() { + static mut X: i32 = 1; + + static mut Y: i32 = 1; + + struct TheStruct { + pub value: i32, + } + struct MyStruct { + pub value: i32, + pub s: TheStruct, + } + + static mut A: MyStruct = MyStruct { value: 1, s: TheStruct { value: 2 } }; + + unsafe { + let _y = &X; + //[e2021]~^ WARN shared reference to mutable static is discouraged [static_mut_refs] + //[e2024]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] + + let _y = &mut X; + //[e2021]~^ WARN mutable reference to mutable static is discouraged [static_mut_refs] + //[e2024]~^^ ERROR mutable reference to mutable static is discouraged [static_mut_refs] + + let _z = &raw mut X; + + let _p = &raw const X; + + let ref _a = X; + //[e2021]~^ WARN shared reference to mutable static is discouraged [static_mut_refs] + //[e2024]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] + + let (_b, _c) = (&X, &Y); + //[e2021]~^ WARN shared reference to mutable static is discouraged [static_mut_refs] + //[e2024]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] + //[e2021]~^^^ WARN shared reference to mutable static is discouraged [static_mut_refs] + //[e2024]~^^^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] + + foo(&X); + //[e2021]~^ WARN shared reference to mutable static is discouraged [static_mut_refs] + //[e2024]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] + + static mut Z: &[i32; 3] = &[0, 1, 2]; + + let _ = Z.len(); + //[e2021]~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] + //[e2024]~^^ ERROR creating a shared reference to mutable static is discouraged [static_mut_refs] + + let _ = Z[0]; + + let _ = format!("{:?}", Z); + //[e2021]~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] + //[e2024]~^^ ERROR creating a shared reference to mutable static is discouraged [static_mut_refs] + + let _v = &A.value; + //[e2021]~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] + //[e2024]~^^ ERROR creating a shared reference to mutable static is discouraged [static_mut_refs] + + let _s = &A.s.value; + //[e2021]~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] + //[e2024]~^^ ERROR creating a shared reference to mutable static is discouraged [static_mut_refs] + + let ref _v = A.value; + //[e2021]~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] + //[e2024]~^^ ERROR creating a shared reference to mutable static is discouraged [static_mut_refs] + + let _x = bar!(FOO); + + STATIC += 1; + } +} + +fn foo<'a>(_x: &'a i32) {} diff --git a/tests/ui/lto/lto-still-runs-thread-dtors.rs b/tests/ui/lto/lto-still-runs-thread-dtors.rs index 504923a93c279..17cc7cee2b22c 100644 --- a/tests/ui/lto/lto-still-runs-thread-dtors.rs +++ b/tests/ui/lto/lto-still-runs-thread-dtors.rs @@ -4,8 +4,9 @@ //@ needs-threads use std::thread; +use std::sync::atomic::{AtomicUsize, Ordering}; -static mut HIT: usize = 0; +static HIT: AtomicUsize = AtomicUsize::new(0); thread_local!(static A: Foo = Foo); @@ -13,20 +14,16 @@ struct Foo; impl Drop for Foo { fn drop(&mut self) { - unsafe { - HIT += 1; - } + HIT.fetch_add(1, Ordering::Relaxed); } } fn main() { - unsafe { - assert_eq!(HIT, 0); - thread::spawn(|| { - assert_eq!(HIT, 0); - A.with(|_| ()); - assert_eq!(HIT, 0); - }).join().unwrap(); - assert_eq!(HIT, 1); - } + assert_eq!(HIT.load(Ordering::Relaxed), 0); + thread::spawn(|| { + assert_eq!(HIT.load(Ordering::Relaxed), 0); + A.with(|_| ()); + assert_eq!(HIT.load(Ordering::Relaxed), 0); + }).join().unwrap(); + assert_eq!(HIT.load(Ordering::Relaxed), 1); } diff --git a/tests/ui/methods/method-self-arg-trait.rs b/tests/ui/methods/method-self-arg-trait.rs index 63594380753ea..a3199e0fb7f24 100644 --- a/tests/ui/methods/method-self-arg-trait.rs +++ b/tests/ui/methods/method-self-arg-trait.rs @@ -1,44 +1,62 @@ //@ run-pass // Test method calls with self as an argument +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + static mut COUNT: u64 = 1; #[derive(Copy, Clone)] struct Foo; -trait Bar : Sized { +trait Bar: Sized { fn foo1(&self); fn foo2(self); fn foo3(self: Box); fn bar1(&self) { - unsafe { COUNT *= 7; } + unsafe { + COUNT *= 7; + } } fn bar2(self) { - unsafe { COUNT *= 11; } + unsafe { + COUNT *= 11; + } } fn bar3(self: Box) { - unsafe { COUNT *= 13; } + unsafe { + COUNT *= 13; + } } } impl Bar for Foo { fn foo1(&self) { - unsafe { COUNT *= 2; } + unsafe { + COUNT *= 2; + } } fn foo2(self) { - unsafe { COUNT *= 3; } + unsafe { + COUNT *= 3; + } } fn foo3(self: Box) { - unsafe { COUNT *= 5; } + unsafe { + COUNT *= 5; + } } } impl Foo { fn baz(self) { - unsafe { COUNT *= 17; } + unsafe { + COUNT *= 17; + } + // Test internal call. Bar::foo1(&self); Bar::foo2(self); @@ -63,5 +81,6 @@ fn main() { x.baz(); - unsafe { assert_eq!(COUNT, 2*2*3*3*5*5*7*7*11*11*13*13*17); } + let count = unsafe { COUNT }; + assert_eq!(count, 2 * 2 * 3 * 3 * 5 * 5 * 7 * 7 * 11 * 11 * 13 * 13 * 17); } diff --git a/tests/ui/methods/method-self-arg.rs b/tests/ui/methods/method-self-arg.rs index d26b9663fd027..2e058ee1077c6 100644 --- a/tests/ui/methods/method-self-arg.rs +++ b/tests/ui/methods/method-self-arg.rs @@ -1,6 +1,9 @@ //@ run-pass // Test method calls with self as an argument +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + static mut COUNT: usize = 1; #[derive(Copy, Clone)] diff --git a/tests/ui/mir/mir_early_return_scope.rs b/tests/ui/mir/mir_early_return_scope.rs index 6dc3f8bc39bbe..cb5f5618fb3d3 100644 --- a/tests/ui/mir/mir_early_return_scope.rs +++ b/tests/ui/mir/mir_early_return_scope.rs @@ -1,6 +1,9 @@ //@ run-pass #![allow(unused_variables)] -static mut DROP: bool = false; + +use std::sync::atomic::{AtomicBool, Ordering}; + +static DROP: AtomicBool = AtomicBool::new(false); struct ConnWrap(Conn); impl ::std::ops::Deref for ConnWrap { @@ -10,7 +13,9 @@ impl ::std::ops::Deref for ConnWrap { struct Conn; impl Drop for Conn { - fn drop(&mut self) { unsafe { DROP = true; } } + fn drop(&mut self) { + DROP.store(true, Ordering::Relaxed); + } } fn inner() { @@ -23,7 +28,5 @@ fn inner() { fn main() { inner(); - unsafe { - assert_eq!(DROP, true); - } + assert_eq!(DROP.load(Ordering::Relaxed), true); } diff --git a/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.stderr b/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.stderr index a6d4f9a2a5cb1..1f01d3e82609b 100644 --- a/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.stderr +++ b/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.stderr @@ -4,14 +4,13 @@ warning: creating a mutable reference to mutable static is discouraged LL | S1 { a: unsafe { &mut X1 } } | ^^^^^^^ mutable reference to mutable static | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior + = note: for more information, see + = 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 = note: `#[warn(static_mut_refs)]` on by default -help: use `addr_of_mut!` instead to create a raw pointer +help: use `&raw mut` instead to create a raw pointer | -LL | S1 { a: unsafe { addr_of_mut!(X1) } } - | ~~~~~~~~~~~~~ + +LL | S1 { a: unsafe { &raw mut X1 } } + | ~~~~~~~~ warning: 1 warning emitted diff --git a/tests/ui/nll/issue-69114-static-mut-ty.rs b/tests/ui/nll/issue-69114-static-mut-ty.rs index ce37da053e371..95c787488c08e 100644 --- a/tests/ui/nll/issue-69114-static-mut-ty.rs +++ b/tests/ui/nll/issue-69114-static-mut-ty.rs @@ -1,5 +1,8 @@ // Check that borrowck ensures that `static mut` items have the expected type. +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + static FOO: u8 = 42; static mut BAR: &'static u8 = &FOO; static mut BAR_ELIDED: &u8 = &FOO; diff --git a/tests/ui/nll/issue-69114-static-mut-ty.stderr b/tests/ui/nll/issue-69114-static-mut-ty.stderr index 1b41230d7ba39..eebfdee289620 100644 --- a/tests/ui/nll/issue-69114-static-mut-ty.stderr +++ b/tests/ui/nll/issue-69114-static-mut-ty.stderr @@ -1,5 +1,5 @@ error[E0597]: `n` does not live long enough - --> $DIR/issue-69114-static-mut-ty.rs:19:15 + --> $DIR/issue-69114-static-mut-ty.rs:22:15 | LL | let n = 42; | - binding `n` declared here @@ -14,7 +14,7 @@ LL | } | - `n` dropped here while still borrowed error[E0597]: `n` does not live long enough - --> $DIR/issue-69114-static-mut-ty.rs:27:22 + --> $DIR/issue-69114-static-mut-ty.rs:30:22 | LL | let n = 42; | - binding `n` declared here diff --git a/tests/ui/numbers-arithmetic/shift-near-oflo.rs b/tests/ui/numbers-arithmetic/shift-near-oflo.rs index 5cca31af0e38e..97227bd843f07 100644 --- a/tests/ui/numbers-arithmetic/shift-near-oflo.rs +++ b/tests/ui/numbers-arithmetic/shift-near-oflo.rs @@ -1,6 +1,9 @@ //@ run-pass //@ compile-flags: -C debug-assertions +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + // Check that we do *not* overflow on a number of edge cases. // (compare with test/run-fail/overflowing-{lsh,rsh}*.rs) diff --git a/tests/ui/self/where-for-self.rs b/tests/ui/self/where-for-self.rs index 9b4e83256645d..4cb67a9be46b5 100644 --- a/tests/ui/self/where-for-self.rs +++ b/tests/ui/self/where-for-self.rs @@ -2,8 +2,9 @@ // Test that we can quantify lifetimes outside a constraint (i.e., including // the self type) in a where clause. +use std::sync::atomic::{AtomicUsize, Ordering}; -static mut COUNT: u32 = 1; +static COUNT: AtomicUsize = AtomicUsize::new(1); trait Bar<'a> { fn bar(&self); @@ -16,13 +17,19 @@ trait Baz<'a> impl<'a, 'b> Bar<'b> for &'a u32 { fn bar(&self) { - unsafe { COUNT *= 2; } + COUNT.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |mut c| { + c *= 2; + Some(c) + }).unwrap(); } } impl<'a, 'b> Baz<'b> for &'a u32 { fn baz(&self) { - unsafe { COUNT *= 3; } + COUNT.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |mut c| { + c *= 3; + Some(c) + }).unwrap(); } } @@ -45,7 +52,5 @@ fn main() { let x = 42; foo1(&x); foo2(&x); - unsafe { - assert_eq!(COUNT, 12); - } + assert_eq!(COUNT.load(Ordering::Relaxed), 12); } diff --git a/tests/ui/static/reference-to-mut-static-safe.e2021.stderr b/tests/ui/static/reference-to-mut-static-safe.e2021.stderr deleted file mode 100644 index 9fdfc00dfcd91..0000000000000 --- a/tests/ui/static/reference-to-mut-static-safe.e2021.stderr +++ /dev/null @@ -1,26 +0,0 @@ -warning: creating a shared reference to mutable static is discouraged - --> $DIR/reference-to-mut-static-safe.rs:9:14 - | -LL | let _x = &X; - | ^^ shared reference to mutable static - | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior - = note: `#[warn(static_mut_refs)]` on by default -help: use `addr_of!` instead to create a raw pointer - | -LL | let _x = addr_of!(X); - | ~~~~~~~~~ + - -error[E0133]: use of mutable static is unsafe and requires unsafe function or block - --> $DIR/reference-to-mut-static-safe.rs:9:15 - | -LL | let _x = &X; - | ^ use of mutable static - | - = note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior - -error: aborting due to 1 previous error; 1 warning emitted - -For more information about this error, try `rustc --explain E0133`. diff --git a/tests/ui/static/reference-to-mut-static-safe.e2024.stderr b/tests/ui/static/reference-to-mut-static-safe.e2024.stderr deleted file mode 100644 index b3e0c84d1d89d..0000000000000 --- a/tests/ui/static/reference-to-mut-static-safe.e2024.stderr +++ /dev/null @@ -1,24 +0,0 @@ -error[E0796]: creating a shared reference to a mutable static - --> $DIR/reference-to-mut-static-safe.rs:9:14 - | -LL | let _x = &X; - | ^^ shared reference to mutable static - | - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior -help: use `addr_of!` instead to create a raw pointer - | -LL | let _x = addr_of!(X); - | ~~~~~~~~~ + - -error[E0133]: use of mutable static is unsafe and requires unsafe block - --> $DIR/reference-to-mut-static-safe.rs:9:15 - | -LL | let _x = &X; - | ^ use of mutable static - | - = note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior - -error: aborting due to 2 previous errors - -Some errors have detailed explanations: E0133, E0796. -For more information about an error, try `rustc --explain E0133`. diff --git a/tests/ui/static/reference-to-mut-static-safe.rs b/tests/ui/static/reference-to-mut-static-safe.rs deleted file mode 100644 index 98afdadf4d255..0000000000000 --- a/tests/ui/static/reference-to-mut-static-safe.rs +++ /dev/null @@ -1,13 +0,0 @@ -//@ revisions: e2021 e2024 - -//@ [e2021] edition:2021 -//@ [e2024] compile-flags: --edition 2024 -Z unstable-options - -fn main() { - static mut X: i32 = 1; - - let _x = &X; - //[e2024]~^ creating a shared reference to a mutable static [E0796] - //~^^ use of mutable static is unsafe and requires unsafe - //[e2021]~^^^ shared reference to mutable static is discouraged [static_mut_refs] -} diff --git a/tests/ui/static/reference-to-mut-static-unsafe-fn.rs b/tests/ui/static/reference-to-mut-static-unsafe-fn.rs deleted file mode 100644 index d63fd5460d840..0000000000000 --- a/tests/ui/static/reference-to-mut-static-unsafe-fn.rs +++ /dev/null @@ -1,28 +0,0 @@ -//@ compile-flags: --edition 2024 -Z unstable-options - -fn main() {} - -unsafe fn _foo() { - unsafe { - static mut X: i32 = 1; - static mut Y: i32 = 1; - - let _y = &X; - //~^ ERROR creating a shared reference to a mutable static [E0796] - - let ref _a = X; - //~^ ERROR creating a shared reference to a mutable static [E0796] - - let ref mut _a = X; - //~^ ERROR creating a mutable reference to a mutable static [E0796] - - let (_b, _c) = (&X, &mut Y); - //~^ ERROR creating a shared reference to a mutable static [E0796] - //~^^ ERROR creating a mutable reference to a mutable static [E0796] - - foo(&X); - //~^ ERROR creating a shared reference to a mutable static [E0796] - } -} - -fn foo<'a>(_x: &'a i32) {} diff --git a/tests/ui/static/reference-to-mut-static-unsafe-fn.stderr b/tests/ui/static/reference-to-mut-static-unsafe-fn.stderr deleted file mode 100644 index ca9cfbf7ac787..0000000000000 --- a/tests/ui/static/reference-to-mut-static-unsafe-fn.stderr +++ /dev/null @@ -1,75 +0,0 @@ -error[E0796]: creating a shared reference to a mutable static - --> $DIR/reference-to-mut-static-unsafe-fn.rs:10:18 - | -LL | let _y = &X; - | ^^ shared reference to mutable static - | - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior -help: use `addr_of!` instead to create a raw pointer - | -LL | let _y = addr_of!(X); - | ~~~~~~~~~ + - -error[E0796]: creating a shared reference to a mutable static - --> $DIR/reference-to-mut-static-unsafe-fn.rs:13:22 - | -LL | let ref _a = X; - | ^ shared reference to mutable static - | - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior -help: use `addr_of!` instead to create a raw pointer - | -LL | let ref _a = addr_of!(X); - | +++++++++ + - -error[E0796]: creating a mutable reference to a mutable static - --> $DIR/reference-to-mut-static-unsafe-fn.rs:16:26 - | -LL | let ref mut _a = X; - | ^ mutable reference to mutable static - | - = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior -help: use `addr_of_mut!` instead to create a raw pointer - | -LL | let ref mut _a = addr_of_mut!(X); - | +++++++++++++ + - -error[E0796]: creating a shared reference to a mutable static - --> $DIR/reference-to-mut-static-unsafe-fn.rs:19:25 - | -LL | let (_b, _c) = (&X, &mut Y); - | ^^ shared reference to mutable static - | - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior -help: use `addr_of!` instead to create a raw pointer - | -LL | let (_b, _c) = (addr_of!(X), &mut Y); - | ~~~~~~~~~ + - -error[E0796]: creating a mutable reference to a mutable static - --> $DIR/reference-to-mut-static-unsafe-fn.rs:19:29 - | -LL | let (_b, _c) = (&X, &mut Y); - | ^^^^^^ mutable reference to mutable static - | - = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior -help: use `addr_of_mut!` instead to create a raw pointer - | -LL | let (_b, _c) = (&X, addr_of_mut!(Y)); - | ~~~~~~~~~~~~~ + - -error[E0796]: creating a shared reference to a mutable static - --> $DIR/reference-to-mut-static-unsafe-fn.rs:23:13 - | -LL | foo(&X); - | ^^ shared reference to mutable static - | - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior -help: use `addr_of!` instead to create a raw pointer - | -LL | foo(addr_of!(X)); - | ~~~~~~~~~ + - -error: aborting due to 6 previous errors - -For more information about this error, try `rustc --explain E0796`. diff --git a/tests/ui/static/reference-to-mut-static.e2021.stderr b/tests/ui/static/reference-to-mut-static.e2021.stderr deleted file mode 100644 index 667d7602f34a5..0000000000000 --- a/tests/ui/static/reference-to-mut-static.e2021.stderr +++ /dev/null @@ -1,91 +0,0 @@ -error: creating a shared reference to mutable static is discouraged - --> $DIR/reference-to-mut-static.rs:16:18 - | -LL | let _y = &X; - | ^^ shared reference to mutable static - | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior -note: the lint level is defined here - --> $DIR/reference-to-mut-static.rs:6:9 - | -LL | #![deny(static_mut_refs)] - | ^^^^^^^^^^^^^^^ -help: use `addr_of!` instead to create a raw pointer - | -LL | let _y = addr_of!(X); - | ~~~~~~~~~ + - -error: creating a mutable reference to mutable static is discouraged - --> $DIR/reference-to-mut-static.rs:20:18 - | -LL | let _y = &mut X; - | ^^^^^^ mutable reference to mutable static - | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior -help: use `addr_of_mut!` instead to create a raw pointer - | -LL | let _y = addr_of_mut!(X); - | ~~~~~~~~~~~~~ + - -error: creating a shared reference to mutable static is discouraged - --> $DIR/reference-to-mut-static.rs:28:22 - | -LL | let ref _a = X; - | ^ shared reference to mutable static - | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior -help: use `addr_of!` instead to create a raw pointer - | -LL | let ref _a = addr_of!(X); - | +++++++++ + - -error: creating a shared reference to mutable static is discouraged - --> $DIR/reference-to-mut-static.rs:32:25 - | -LL | let (_b, _c) = (&X, &Y); - | ^^ shared reference to mutable static - | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior -help: use `addr_of!` instead to create a raw pointer - | -LL | let (_b, _c) = (addr_of!(X), &Y); - | ~~~~~~~~~ + - -error: creating a shared reference to mutable static is discouraged - --> $DIR/reference-to-mut-static.rs:32:29 - | -LL | let (_b, _c) = (&X, &Y); - | ^^ shared reference to mutable static - | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior -help: use `addr_of!` instead to create a raw pointer - | -LL | let (_b, _c) = (&X, addr_of!(Y)); - | ~~~~~~~~~ + - -error: creating a shared reference to mutable static is discouraged - --> $DIR/reference-to-mut-static.rs:38:13 - | -LL | foo(&X); - | ^^ shared reference to mutable static - | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior -help: use `addr_of!` instead to create a raw pointer - | -LL | foo(addr_of!(X)); - | ~~~~~~~~~ + - -error: aborting due to 6 previous errors - diff --git a/tests/ui/static/reference-to-mut-static.e2024.stderr b/tests/ui/static/reference-to-mut-static.e2024.stderr deleted file mode 100644 index e77f43554666c..0000000000000 --- a/tests/ui/static/reference-to-mut-static.e2024.stderr +++ /dev/null @@ -1,75 +0,0 @@ -error[E0796]: creating a shared reference to a mutable static - --> $DIR/reference-to-mut-static.rs:16:18 - | -LL | let _y = &X; - | ^^ shared reference to mutable static - | - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior -help: use `addr_of!` instead to create a raw pointer - | -LL | let _y = addr_of!(X); - | ~~~~~~~~~ + - -error[E0796]: creating a mutable reference to a mutable static - --> $DIR/reference-to-mut-static.rs:20:18 - | -LL | let _y = &mut X; - | ^^^^^^ mutable reference to mutable static - | - = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior -help: use `addr_of_mut!` instead to create a raw pointer - | -LL | let _y = addr_of_mut!(X); - | ~~~~~~~~~~~~~ + - -error[E0796]: creating a shared reference to a mutable static - --> $DIR/reference-to-mut-static.rs:28:22 - | -LL | let ref _a = X; - | ^ shared reference to mutable static - | - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior -help: use `addr_of!` instead to create a raw pointer - | -LL | let ref _a = addr_of!(X); - | +++++++++ + - -error[E0796]: creating a shared reference to a mutable static - --> $DIR/reference-to-mut-static.rs:32:25 - | -LL | let (_b, _c) = (&X, &Y); - | ^^ shared reference to mutable static - | - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior -help: use `addr_of!` instead to create a raw pointer - | -LL | let (_b, _c) = (addr_of!(X), &Y); - | ~~~~~~~~~ + - -error[E0796]: creating a shared reference to a mutable static - --> $DIR/reference-to-mut-static.rs:32:29 - | -LL | let (_b, _c) = (&X, &Y); - | ^^ shared reference to mutable static - | - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior -help: use `addr_of!` instead to create a raw pointer - | -LL | let (_b, _c) = (&X, addr_of!(Y)); - | ~~~~~~~~~ + - -error[E0796]: creating a shared reference to a mutable static - --> $DIR/reference-to-mut-static.rs:38:13 - | -LL | foo(&X); - | ^^ shared reference to mutable static - | - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior -help: use `addr_of!` instead to create a raw pointer - | -LL | foo(addr_of!(X)); - | ~~~~~~~~~ + - -error: aborting due to 6 previous errors - -For more information about this error, try `rustc --explain E0796`. diff --git a/tests/ui/static/reference-to-mut-static.rs b/tests/ui/static/reference-to-mut-static.rs deleted file mode 100644 index af2cab7dd8723..0000000000000 --- a/tests/ui/static/reference-to-mut-static.rs +++ /dev/null @@ -1,50 +0,0 @@ -//@ revisions: e2021 e2024 - -//@ [e2021] edition:2021 -//@ [e2024] compile-flags: --edition 2024 -Z unstable-options - -#![deny(static_mut_refs)] - -use std::ptr::{addr_of, addr_of_mut}; - -fn main() { - static mut X: i32 = 1; - - static mut Y: i32 = 1; - - unsafe { - let _y = &X; - //[e2024]~^ ERROR creating a shared reference to a mutable static [E0796] - //[e2021]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] - - let _y = &mut X; - //[e2024]~^ ERROR creating a mutable reference to a mutable static [E0796] - //[e2021]~^^ ERROR mutable reference to mutable static is discouraged [static_mut_refs] - - let _z = addr_of_mut!(X); - - let _p = addr_of!(X); - - let ref _a = X; - //[e2024]~^ ERROR creating a shared reference to a mutable static [E0796] - //[e2021]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] - - let (_b, _c) = (&X, &Y); - //[e2024]~^ ERROR creating a shared reference to a mutable static [E0796] - //[e2021]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] - //[e2024]~^^^ ERROR creating a shared reference to a mutable static [E0796] - //[e2021]~^^^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] - - foo(&X); - //[e2024]~^ ERROR creating a shared reference to a mutable static [E0796] - //[e2021]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] - - static mut Z: &[i32; 3] = &[0, 1, 2]; - - let _ = Z.len(); - let _ = Z[0]; - let _ = format!("{:?}", Z); - } -} - -fn foo<'a>(_x: &'a i32) {} diff --git a/tests/ui/static/safe-extern-statics-mut.rs b/tests/ui/static/safe-extern-statics-mut.rs index 05a1bee8891e8..4dfcd41b73c13 100644 --- a/tests/ui/static/safe-extern-statics-mut.rs +++ b/tests/ui/static/safe-extern-statics-mut.rs @@ -10,8 +10,6 @@ extern "C" { fn main() { let b = B; //~ ERROR use of mutable static is unsafe let rb = &B; //~ ERROR use of mutable static is unsafe - //~^ WARN shared reference to mutable static is discouraged [static_mut_refs] let xb = XB; //~ ERROR use of mutable static is unsafe let xrb = &XB; //~ ERROR use of mutable static is unsafe - //~^ WARN shared reference to mutable static is discouraged [static_mut_refs] } diff --git a/tests/ui/static/safe-extern-statics-mut.stderr b/tests/ui/static/safe-extern-statics-mut.stderr index 7705a897e2717..e390625f20a98 100644 --- a/tests/ui/static/safe-extern-statics-mut.stderr +++ b/tests/ui/static/safe-extern-statics-mut.stderr @@ -1,32 +1,3 @@ -warning: creating a shared reference to mutable static is discouraged - --> $DIR/safe-extern-statics-mut.rs:12:14 - | -LL | let rb = &B; - | ^^ shared reference to mutable static - | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior - = note: `#[warn(static_mut_refs)]` on by default -help: use `addr_of!` instead to create a raw pointer - | -LL | let rb = addr_of!(B); - | ~~~~~~~~~ + - -warning: creating a shared reference to mutable static is discouraged - --> $DIR/safe-extern-statics-mut.rs:15:15 - | -LL | let xrb = &XB; - | ^^^ shared reference to mutable static - | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior -help: use `addr_of!` instead to create a raw pointer - | -LL | let xrb = addr_of!(XB); - | ~~~~~~~~~ + - error[E0133]: use of mutable static is unsafe and requires unsafe function or block --> $DIR/safe-extern-statics-mut.rs:11:13 | @@ -44,7 +15,7 @@ LL | let rb = &B; = note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior error[E0133]: use of mutable static is unsafe and requires unsafe function or block - --> $DIR/safe-extern-statics-mut.rs:14:14 + --> $DIR/safe-extern-statics-mut.rs:13:14 | LL | let xb = XB; | ^^ use of mutable static @@ -52,13 +23,13 @@ LL | let xb = XB; = note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior error[E0133]: use of mutable static is unsafe and requires unsafe function or block - --> $DIR/safe-extern-statics-mut.rs:15:16 + --> $DIR/safe-extern-statics-mut.rs:14:16 | LL | let xrb = &XB; | ^^ use of mutable static | = note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior -error: aborting due to 4 previous errors; 2 warnings emitted +error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0133`. diff --git a/tests/ui/statics/issue-15261.stderr b/tests/ui/statics/issue-15261.stderr index 6035eef5b7140..417dbae9db18f 100644 --- a/tests/ui/statics/issue-15261.stderr +++ b/tests/ui/statics/issue-15261.stderr @@ -4,14 +4,13 @@ warning: creating a shared reference to mutable static is discouraged LL | static n: &'static usize = unsafe { &n_mut }; | ^^^^^^ shared reference to mutable static | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior + = note: for more information, see + = 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: `#[warn(static_mut_refs)]` on by default -help: use `addr_of!` instead to create a raw pointer +help: use `&raw const` instead to create a raw pointer | -LL | static n: &'static usize = unsafe { addr_of!(n_mut) }; - | ~~~~~~~~~ + +LL | static n: &'static usize = unsafe { &raw const n_mut }; + | ~~~~~~~~~~ warning: 1 warning emitted diff --git a/tests/ui/statics/static-mut-xc.rs b/tests/ui/statics/static-mut-xc.rs index a772d4151f7cd..c23cc822ce741 100644 --- a/tests/ui/statics/static-mut-xc.rs +++ b/tests/ui/statics/static-mut-xc.rs @@ -17,14 +17,19 @@ fn static_bound_set(a: &'static mut isize) { unsafe fn run() { assert_eq!(static_mut_xc::a, 3); + //~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] static_mut_xc::a = 4; assert_eq!(static_mut_xc::a, 4); + //~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] static_mut_xc::a += 1; assert_eq!(static_mut_xc::a, 5); + //~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] static_mut_xc::a *= 3; assert_eq!(static_mut_xc::a, 15); + //~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] static_mut_xc::a = -3; assert_eq!(static_mut_xc::a, -3); + //~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] static_bound(&static_mut_xc::a); //~^ WARN shared reference to mutable static is discouraged [static_mut_refs] static_bound_set(&mut static_mut_xc::a); diff --git a/tests/ui/statics/static-mut-xc.stderr b/tests/ui/statics/static-mut-xc.stderr index 9751f754332c9..176deb518fce0 100644 --- a/tests/ui/statics/static-mut-xc.stderr +++ b/tests/ui/statics/static-mut-xc.stderr @@ -1,31 +1,74 @@ warning: creating a shared reference to mutable static is discouraged - --> $DIR/static-mut-xc.rs:28:18 + --> $DIR/static-mut-xc.rs:19:16 + | +LL | assert_eq!(static_mut_xc::a, 3); + | ^^^^^^^^^^^^^^^^ shared reference to mutable static + | + = note: for more information, see + = 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: `#[warn(static_mut_refs)]` on by default + +warning: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-xc.rs:22:16 + | +LL | assert_eq!(static_mut_xc::a, 4); + | ^^^^^^^^^^^^^^^^ shared reference to mutable static + | + = note: for more information, see + = 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 + +warning: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-xc.rs:25:16 + | +LL | assert_eq!(static_mut_xc::a, 5); + | ^^^^^^^^^^^^^^^^ shared reference to mutable static + | + = note: for more information, see + = 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 + +warning: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-xc.rs:28:16 + | +LL | assert_eq!(static_mut_xc::a, 15); + | ^^^^^^^^^^^^^^^^ shared reference to mutable static + | + = note: for more information, see + = 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 + +warning: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-xc.rs:31:16 + | +LL | assert_eq!(static_mut_xc::a, -3); + | ^^^^^^^^^^^^^^^^ shared reference to mutable static + | + = note: for more information, see + = 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 + +warning: creating a shared reference to mutable static is discouraged + --> $DIR/static-mut-xc.rs:33:18 | LL | static_bound(&static_mut_xc::a); | ^^^^^^^^^^^^^^^^^ shared reference to mutable static | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior - = note: `#[warn(static_mut_refs)]` on by default -help: use `addr_of!` instead to create a raw pointer + = note: for more information, see + = 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 `&raw const` instead to create a raw pointer | -LL | static_bound(addr_of!(static_mut_xc::a)); - | ~~~~~~~~~ + +LL | static_bound(&raw const static_mut_xc::a); + | ~~~~~~~~~~ warning: creating a mutable reference to mutable static is discouraged - --> $DIR/static-mut-xc.rs:30:22 + --> $DIR/static-mut-xc.rs:35:22 | LL | static_bound_set(&mut static_mut_xc::a); | ^^^^^^^^^^^^^^^^^^^^^ mutable reference to mutable static | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior -help: use `addr_of_mut!` instead to create a raw pointer + = note: for more information, see + = 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 `&raw mut` instead to create a raw pointer | -LL | static_bound_set(addr_of_mut!(static_mut_xc::a)); - | ~~~~~~~~~~~~~ + +LL | static_bound_set(&raw mut static_mut_xc::a); + | ~~~~~~~~ -warning: 2 warnings emitted +warning: 7 warnings emitted diff --git a/tests/ui/statics/static-recursive.rs b/tests/ui/statics/static-recursive.rs index 29b80818b7d46..da23b54d1fca0 100644 --- a/tests/ui/statics/static-recursive.rs +++ b/tests/ui/statics/static-recursive.rs @@ -17,6 +17,7 @@ static L3: StaticDoubleLinked = StaticDoubleLinked { prev: &L2, next: &L1, data: pub fn main() { unsafe { assert_eq!(S, *(S as *const *const u8)); + //~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs] } let mut test_vec = Vec::new(); diff --git a/tests/ui/statics/static-recursive.stderr b/tests/ui/statics/static-recursive.stderr index a7a1a1610afbb..f2dd5b8a6cfe8 100644 --- a/tests/ui/statics/static-recursive.stderr +++ b/tests/ui/statics/static-recursive.stderr @@ -4,14 +4,22 @@ warning: creating a shared reference to mutable static is discouraged LL | static mut S: *const u8 = unsafe { &S as *const *const u8 as *const u8 }; | ^^ shared reference to mutable static | - = note: for more information, see issue #114447 - = note: this will be a hard error in the 2024 edition - = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior + = note: for more information, see + = 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: `#[warn(static_mut_refs)]` on by default -help: use `addr_of!` instead to create a raw pointer +help: use `&raw const` instead to create a raw pointer | -LL | static mut S: *const u8 = unsafe { addr_of!(S) as *const *const u8 as *const u8 }; - | ~~~~~~~~~ + +LL | static mut S: *const u8 = unsafe { &raw const S as *const *const u8 as *const u8 }; + | ~~~~~~~~~~ -warning: 1 warning emitted +warning: creating a shared reference to mutable static is discouraged + --> $DIR/static-recursive.rs:19:20 + | +LL | assert_eq!(S, *(S as *const *const u8)); + | ^ shared reference to mutable static + | + = note: for more information, see + = 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 + +warning: 2 warnings emitted diff --git a/tests/ui/thread-local/thread-local-issue-37508.rs b/tests/ui/thread-local/thread-local-issue-37508.rs index db430a3229c7b..34c3945345184 100644 --- a/tests/ui/thread-local/thread-local-issue-37508.rs +++ b/tests/ui/thread-local/thread-local-issue-37508.rs @@ -4,6 +4,9 @@ // // Regression test for issue #37508 +// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint +#![allow(static_mut_refs)] + #![no_main] #![no_std] #![feature(thread_local, lang_items)] diff --git a/tests/ui/traits/impl.rs b/tests/ui/traits/impl.rs index 348096f37f7be..bb2b8df457e4b 100644 --- a/tests/ui/traits/impl.rs +++ b/tests/ui/traits/impl.rs @@ -3,10 +3,12 @@ //@ aux-build:traitimpl.rs +use std::sync::atomic::{AtomicUsize, Ordering}; + extern crate traitimpl; use traitimpl::Bar; -static mut COUNT: usize = 1; +static COUNT: AtomicUsize = AtomicUsize::new(1); trait T { fn t(&self) {} //~ WARN method `t` is never used @@ -14,10 +16,16 @@ trait T { impl<'a> dyn T+'a { fn foo(&self) { - unsafe { COUNT *= 2; } + COUNT.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |mut c| { + c *= 2; + Some(c) + }).unwrap(); } fn bar() { - unsafe { COUNT *= 3; } + COUNT.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |mut c| { + c *= 3; + Some(c) + }).unwrap(); } } @@ -33,7 +41,7 @@ fn main() { ::foo(x); ::bar(); - unsafe { assert_eq!(COUNT, 12); } + assert_eq!(COUNT.load(Ordering::Relaxed), 12); // Cross-crait case let x: &dyn Bar = &Foo; diff --git a/tests/ui/traits/impl.stderr b/tests/ui/traits/impl.stderr index 98b6fb03d836f..0697f62da0dc7 100644 --- a/tests/ui/traits/impl.stderr +++ b/tests/ui/traits/impl.stderr @@ -1,5 +1,5 @@ warning: method `t` is never used - --> $DIR/impl.rs:12:8 + --> $DIR/impl.rs:14:8 | LL | trait T { | - method in this trait diff --git a/tests/ui/union/union-drop-assign.rs b/tests/ui/union/union-drop-assign.rs index e9165fddc51e4..7685d74e713c1 100644 --- a/tests/ui/union/union-drop-assign.rs +++ b/tests/ui/union/union-drop-assign.rs @@ -4,6 +4,7 @@ // Drop works for union itself. use std::mem::ManuallyDrop; +use std::sync::atomic::{AtomicUsize, Ordering}; struct S; @@ -13,25 +14,26 @@ union U { impl Drop for S { fn drop(&mut self) { - unsafe { CHECK += 10; } + CHECK.fetch_add(10, Ordering::Relaxed); } } impl Drop for U { fn drop(&mut self) { - unsafe { CHECK += 1; } + CHECK.fetch_add(1, Ordering::Relaxed); } } -static mut CHECK: u8 = 0; +static CHECK: AtomicUsize = AtomicUsize::new(0); fn main() { + let mut u = U { a: ManuallyDrop::new(S) }; + assert_eq!(CHECK.load(Ordering::Relaxed), 0); + u = U { a: ManuallyDrop::new(S) }; + // union itself is assigned, union is dropped, field is not dropped + assert_eq!(CHECK.load(Ordering::Relaxed), 1); unsafe { - let mut u = U { a: ManuallyDrop::new(S) }; - assert_eq!(CHECK, 0); - u = U { a: ManuallyDrop::new(S) }; - assert_eq!(CHECK, 1); // union itself is assigned, union is dropped, field is not dropped *u.a = S; - assert_eq!(CHECK, 11); // union field is assigned, field is dropped } + assert_eq!(CHECK.load(Ordering::Relaxed), 11); // union field is assigned, field is dropped } diff --git a/tests/ui/union/union-drop.rs b/tests/ui/union/union-drop.rs index e467eda293504..041135b16a5c0 100644 --- a/tests/ui/union/union-drop.rs +++ b/tests/ui/union/union-drop.rs @@ -3,6 +3,8 @@ #![allow(dead_code)] #![allow(unused_variables)] +use std::sync::atomic::{AtomicUsize, Ordering}; + // Drop works for union itself. #[derive(Copy, Clone)] @@ -22,37 +24,35 @@ union Y { impl Drop for U { fn drop(&mut self) { - unsafe { CHECK += 1; } + CHECK.fetch_add(1, Ordering::Relaxed); } } impl Drop for W { fn drop(&mut self) { - unsafe { CHECK += 1; } + CHECK.fetch_add(1, Ordering::Relaxed); } } -static mut CHECK: u8 = 0; +static CHECK: AtomicUsize = AtomicUsize::new(0); fn main() { - unsafe { - assert_eq!(CHECK, 0); - { - let u = U { a: 1 }; - } - assert_eq!(CHECK, 1); // 1, dtor of U is called - { - let w = W { a: S }; - } - assert_eq!(CHECK, 2); // 2, dtor of W is called - { - let y = Y { a: S }; - } - assert_eq!(CHECK, 2); // 2, Y has no dtor - { - let u2 = U { a: 1 }; - std::mem::forget(u2); - } - assert_eq!(CHECK, 2); // 2, dtor of U *not* called for u2 + assert_eq!(CHECK.load(Ordering::Relaxed), 0); + { + let u = U { a: 1 }; + } + assert_eq!(CHECK.load(Ordering::Relaxed), 1); // 1, dtor of U is called + { + let w = W { a: S }; + } + assert_eq!(CHECK.load(Ordering::Relaxed), 2); // 2, dtor of W is called + { + let y = Y { a: S }; + } + assert_eq!(CHECK.load(Ordering::Relaxed), 2); // 2, Y has no dtor + { + let u2 = U { a: 1 }; + std::mem::forget(u2); } + assert_eq!(CHECK.load(Ordering::Relaxed), 2); // 2, dtor of U *not* called for u2 }