Skip to content

Commit bb04c68

Browse files
committed
Auto merge of rust-lang#124895 - obeis:static-mut-hidden-ref, r=compiler-errors
Disallow hidden references to mutable static Closes rust-lang#123060 Tracking: - rust-lang#123758
2 parents 4f31900 + cd99729 commit bb04c68

12 files changed

+122
-92
lines changed

tests/ui/checked_unwrap/simple_conditionals.stderr

+12-1
Original file line numberDiff line numberDiff line change
@@ -236,5 +236,16 @@ LL | if result.is_ok() {
236236
LL | result.as_mut().unwrap();
237237
| ^^^^^^^^^^^^^^^^^^^^^^^^
238238

239-
error: aborting due to 25 previous errors
239+
error: creating a shared reference to mutable static is discouraged
240+
--> tests/ui/checked_unwrap/simple_conditionals.rs:174:12
241+
|
242+
LL | if X.is_some() {
243+
| ^^^^^^^^^^^ shared reference to mutable static
244+
|
245+
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
246+
= 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
247+
= note: `-D static-mut-refs` implied by `-D warnings`
248+
= help: to override `-D warnings` add `#[allow(static_mut_refs)]`
249+
250+
error: aborting due to 26 previous errors
240251

tests/ui/multiple_unsafe_ops_per_block.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![allow(deref_nullptr)]
55
#![allow(clippy::unnecessary_operation)]
66
#![allow(dropping_copy_types)]
7+
#![allow(clippy::assign_op_pattern)]
78
#![warn(clippy::multiple_unsafe_ops_per_block)]
89

910
extern crate proc_macros;

tests/ui/multiple_unsafe_ops_per_block.stderr

+29-29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this `unsafe` block contains 2 unsafe operations, expected only one
2-
--> tests/ui/multiple_unsafe_ops_per_block.rs:37:5
2+
--> tests/ui/multiple_unsafe_ops_per_block.rs:38:5
33
|
44
LL | / unsafe {
55
LL | | STATIC += 1;
@@ -8,20 +8,20 @@ LL | | }
88
| |_____^
99
|
1010
note: modification of a mutable static occurs here
11-
--> tests/ui/multiple_unsafe_ops_per_block.rs:38:9
11+
--> tests/ui/multiple_unsafe_ops_per_block.rs:39:9
1212
|
1313
LL | STATIC += 1;
1414
| ^^^^^^^^^^^
1515
note: unsafe function call occurs here
16-
--> tests/ui/multiple_unsafe_ops_per_block.rs:39:9
16+
--> tests/ui/multiple_unsafe_ops_per_block.rs:40:9
1717
|
1818
LL | not_very_safe();
1919
| ^^^^^^^^^^^^^^^
2020
= note: `-D clippy::multiple-unsafe-ops-per-block` implied by `-D warnings`
2121
= help: to override `-D warnings` add `#[allow(clippy::multiple_unsafe_ops_per_block)]`
2222

2323
error: this `unsafe` block contains 2 unsafe operations, expected only one
24-
--> tests/ui/multiple_unsafe_ops_per_block.rs:46:5
24+
--> tests/ui/multiple_unsafe_ops_per_block.rs:47:5
2525
|
2626
LL | / unsafe {
2727
LL | | drop(u.u);
@@ -30,18 +30,18 @@ LL | | }
3030
| |_____^
3131
|
3232
note: union field access occurs here
33-
--> tests/ui/multiple_unsafe_ops_per_block.rs:47:14
33+
--> tests/ui/multiple_unsafe_ops_per_block.rs:48:14
3434
|
3535
LL | drop(u.u);
3636
| ^^^
3737
note: raw pointer dereference occurs here
38-
--> tests/ui/multiple_unsafe_ops_per_block.rs:48:9
38+
--> tests/ui/multiple_unsafe_ops_per_block.rs:49:9
3939
|
4040
LL | *raw_ptr();
4141
| ^^^^^^^^^^
4242

4343
error: this `unsafe` block contains 3 unsafe operations, expected only one
44-
--> tests/ui/multiple_unsafe_ops_per_block.rs:53:5
44+
--> tests/ui/multiple_unsafe_ops_per_block.rs:54:5
4545
|
4646
LL | / unsafe {
4747
LL | | asm!("nop");
@@ -51,23 +51,23 @@ LL | | }
5151
| |_____^
5252
|
5353
note: inline assembly used here
54-
--> tests/ui/multiple_unsafe_ops_per_block.rs:54:9
54+
--> tests/ui/multiple_unsafe_ops_per_block.rs:55:9
5555
|
5656
LL | asm!("nop");
5757
| ^^^^^^^^^^^
5858
note: unsafe method call occurs here
59-
--> tests/ui/multiple_unsafe_ops_per_block.rs:55:9
59+
--> tests/ui/multiple_unsafe_ops_per_block.rs:56:9
6060
|
6161
LL | sample.not_very_safe();
6262
| ^^^^^^^^^^^^^^^^^^^^^^
6363
note: modification of a mutable static occurs here
64-
--> tests/ui/multiple_unsafe_ops_per_block.rs:56:9
64+
--> tests/ui/multiple_unsafe_ops_per_block.rs:57:9
6565
|
6666
LL | STATIC = 0;
6767
| ^^^^^^^^^^
6868

6969
error: this `unsafe` block contains 6 unsafe operations, expected only one
70-
--> tests/ui/multiple_unsafe_ops_per_block.rs:62:5
70+
--> tests/ui/multiple_unsafe_ops_per_block.rs:63:5
7171
|
7272
LL | / unsafe {
7373
LL | | drop(u.u);
@@ -79,55 +79,55 @@ LL | | }
7979
| |_____^
8080
|
8181
note: union field access occurs here
82-
--> tests/ui/multiple_unsafe_ops_per_block.rs:63:14
82+
--> tests/ui/multiple_unsafe_ops_per_block.rs:64:14
8383
|
8484
LL | drop(u.u);
8585
| ^^^
8686
note: access of a mutable static occurs here
87-
--> tests/ui/multiple_unsafe_ops_per_block.rs:64:14
87+
--> tests/ui/multiple_unsafe_ops_per_block.rs:65:14
8888
|
8989
LL | drop(STATIC);
9090
| ^^^^^^
9191
note: unsafe method call occurs here
92-
--> tests/ui/multiple_unsafe_ops_per_block.rs:65:9
92+
--> tests/ui/multiple_unsafe_ops_per_block.rs:66:9
9393
|
9494
LL | sample.not_very_safe();
9595
| ^^^^^^^^^^^^^^^^^^^^^^
9696
note: unsafe function call occurs here
97-
--> tests/ui/multiple_unsafe_ops_per_block.rs:66:9
97+
--> tests/ui/multiple_unsafe_ops_per_block.rs:67:9
9898
|
9999
LL | not_very_safe();
100100
| ^^^^^^^^^^^^^^^
101101
note: raw pointer dereference occurs here
102-
--> tests/ui/multiple_unsafe_ops_per_block.rs:67:9
102+
--> tests/ui/multiple_unsafe_ops_per_block.rs:68:9
103103
|
104104
LL | *raw_ptr();
105105
| ^^^^^^^^^^
106106
note: inline assembly used here
107-
--> tests/ui/multiple_unsafe_ops_per_block.rs:68:9
107+
--> tests/ui/multiple_unsafe_ops_per_block.rs:69:9
108108
|
109109
LL | asm!("nop");
110110
| ^^^^^^^^^^^
111111

112112
error: this `unsafe` block contains 2 unsafe operations, expected only one
113-
--> tests/ui/multiple_unsafe_ops_per_block.rs:106:5
113+
--> tests/ui/multiple_unsafe_ops_per_block.rs:107:5
114114
|
115115
LL | unsafe { char::from_u32_unchecked(*ptr.cast::<u32>()) }
116116
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
117117
|
118118
note: unsafe function call occurs here
119-
--> tests/ui/multiple_unsafe_ops_per_block.rs:106:14
119+
--> tests/ui/multiple_unsafe_ops_per_block.rs:107:14
120120
|
121121
LL | unsafe { char::from_u32_unchecked(*ptr.cast::<u32>()) }
122122
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
123123
note: raw pointer dereference occurs here
124-
--> tests/ui/multiple_unsafe_ops_per_block.rs:106:39
124+
--> tests/ui/multiple_unsafe_ops_per_block.rs:107:39
125125
|
126126
LL | unsafe { char::from_u32_unchecked(*ptr.cast::<u32>()) }
127127
| ^^^^^^^^^^^^^^^^^^
128128

129129
error: this `unsafe` block contains 2 unsafe operations, expected only one
130-
--> tests/ui/multiple_unsafe_ops_per_block.rs:124:5
130+
--> tests/ui/multiple_unsafe_ops_per_block.rs:125:5
131131
|
132132
LL | / unsafe {
133133
LL | | x();
@@ -136,18 +136,18 @@ LL | | }
136136
| |_____^
137137
|
138138
note: unsafe function call occurs here
139-
--> tests/ui/multiple_unsafe_ops_per_block.rs:125:9
139+
--> tests/ui/multiple_unsafe_ops_per_block.rs:126:9
140140
|
141141
LL | x();
142142
| ^^^
143143
note: unsafe function call occurs here
144-
--> tests/ui/multiple_unsafe_ops_per_block.rs:126:9
144+
--> tests/ui/multiple_unsafe_ops_per_block.rs:127:9
145145
|
146146
LL | x();
147147
| ^^^
148148

149149
error: this `unsafe` block contains 2 unsafe operations, expected only one
150-
--> tests/ui/multiple_unsafe_ops_per_block.rs:135:9
150+
--> tests/ui/multiple_unsafe_ops_per_block.rs:136:9
151151
|
152152
LL | / unsafe {
153153
LL | | T::X();
@@ -156,18 +156,18 @@ LL | | }
156156
| |_________^
157157
|
158158
note: unsafe function call occurs here
159-
--> tests/ui/multiple_unsafe_ops_per_block.rs:136:13
159+
--> tests/ui/multiple_unsafe_ops_per_block.rs:137:13
160160
|
161161
LL | T::X();
162162
| ^^^^^^
163163
note: unsafe function call occurs here
164-
--> tests/ui/multiple_unsafe_ops_per_block.rs:137:13
164+
--> tests/ui/multiple_unsafe_ops_per_block.rs:138:13
165165
|
166166
LL | T::X();
167167
| ^^^^^^
168168

169169
error: this `unsafe` block contains 2 unsafe operations, expected only one
170-
--> tests/ui/multiple_unsafe_ops_per_block.rs:145:5
170+
--> tests/ui/multiple_unsafe_ops_per_block.rs:146:5
171171
|
172172
LL | / unsafe {
173173
LL | | x.0();
@@ -176,12 +176,12 @@ LL | | }
176176
| |_____^
177177
|
178178
note: unsafe function call occurs here
179-
--> tests/ui/multiple_unsafe_ops_per_block.rs:146:9
179+
--> tests/ui/multiple_unsafe_ops_per_block.rs:147:9
180180
|
181181
LL | x.0();
182182
| ^^^^^
183183
note: unsafe function call occurs here
184-
--> tests/ui/multiple_unsafe_ops_per_block.rs:147:9
184+
--> tests/ui/multiple_unsafe_ops_per_block.rs:148:9
185185
|
186186
LL | x.0();
187187
| ^^^^^

tests/ui/must_use_candidates.fixed

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#![feature(never_type)]
2-
#![allow(unused_mut, clippy::redundant_allocation, clippy::needless_pass_by_ref_mut)]
2+
#![allow(
3+
unused_mut,
4+
clippy::redundant_allocation,
5+
clippy::needless_pass_by_ref_mut,
6+
static_mut_refs
7+
)]
38
#![warn(clippy::must_use_candidate)]
49
use std::rc::Rc;
510
use std::sync::atomic::{AtomicBool, Ordering};

tests/ui/must_use_candidates.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
#![feature(never_type)]
2-
#![allow(unused_mut, clippy::redundant_allocation, clippy::needless_pass_by_ref_mut)]
2+
#![allow(
3+
unused_mut,
4+
clippy::redundant_allocation,
5+
clippy::needless_pass_by_ref_mut,
6+
static_mut_refs
7+
)]
38
#![warn(clippy::must_use_candidate)]
49
use std::rc::Rc;
510
use std::sync::atomic::{AtomicBool, Ordering};

tests/ui/must_use_candidates.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: this function could have a `#[must_use]` attribute
2-
--> tests/ui/must_use_candidates.rs:11:1
2+
--> tests/ui/must_use_candidates.rs:16:1
33
|
44
LL | pub fn pure(i: u8) -> u8 {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn pure(i: u8) -> u8`
@@ -8,25 +8,25 @@ LL | pub fn pure(i: u8) -> u8 {
88
= help: to override `-D warnings` add `#[allow(clippy::must_use_candidate)]`
99

1010
error: this method could have a `#[must_use]` attribute
11-
--> tests/ui/must_use_candidates.rs:16:5
11+
--> tests/ui/must_use_candidates.rs:21:5
1212
|
1313
LL | pub fn inherent_pure(&self) -> u8 {
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn inherent_pure(&self) -> u8`
1515

1616
error: this function could have a `#[must_use]` attribute
17-
--> tests/ui/must_use_candidates.rs:47:1
17+
--> tests/ui/must_use_candidates.rs:52:1
1818
|
1919
LL | pub fn with_marker(_d: std::marker::PhantomData<&mut u32>) -> bool {
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn with_marker(_d: std::marker::PhantomData<&mut u32>) -> bool`
2121

2222
error: this function could have a `#[must_use]` attribute
23-
--> tests/ui/must_use_candidates.rs:59:1
23+
--> tests/ui/must_use_candidates.rs:64:1
2424
|
2525
LL | pub fn rcd(_x: Rc<u32>) -> bool {
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn rcd(_x: Rc<u32>) -> bool`
2727

2828
error: this function could have a `#[must_use]` attribute
29-
--> tests/ui/must_use_candidates.rs:67:1
29+
--> tests/ui/must_use_candidates.rs:72:1
3030
|
3131
LL | pub fn arcd(_x: Arc<u32>) -> bool {
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: add the attribute: `#[must_use] pub fn arcd(_x: Arc<u32>) -> bool`

tests/ui/redundant_static_lifetimes.fixed

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#![allow(unused)]
2+
// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
3+
#![allow(static_mut_refs)]
24

35
#[derive(Debug)]
46
struct Foo;

tests/ui/redundant_static_lifetimes.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#![allow(unused)]
2+
// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
3+
#![allow(static_mut_refs)]
24

35
#[derive(Debug)]
46
struct Foo;

0 commit comments

Comments
 (0)