Skip to content

Commit e74d96b

Browse files
authored
Rollup merge of rust-lang#148407 - Urgau:suspicious_int_mutable_consts, r=JonathanBrouwer
Warn against calls which mutate an interior mutable `const`-item ## `const_item_interior_mutations` ~~`interior_mutable_const_item_mutations`~~ ~~`suspicious_mutation_of_interior_mutable_consts`~~ *warn-by-default* The `const_item_interior_mutations` lint checks for calls which mutates an interior mutable const-item. ### Example ```rust use std::sync::Once; const INIT: Once = Once::new(); // using `INIT` will always create a temporary and // never modify it-self on use, should be a `static` // instead for shared use fn init() { INIT.call_once(|| { println!("Once::call_once first call"); }); } ``` ```text warning: mutation of an interior mutable `const` item with call to `call_once` --> a.rs:11:5 | 11 | INIT.call_once(|| { | ^--- | | | _____`INIT` is a interior mutable `const` item of type `std::sync::Once` | | 12 | | println!("Once::call_once first call"); 13 | | }); | |______^ | = note: each usage of a `const` item creates a new temporary = note: only the temporaries and never the original `const INIT` will be modified = help: for more details on interior mutability see <https://doc.rust-lang.org/reference/interior-mutability.html> = note: `#[warn(const_item_interior_mutations)]` on by default help: for a shared instance of `INIT`, consider making it a `static` item instead | 6 - const INIT: Once = Once::new(); // using `INIT` will always create a temporary and 6 + static INIT: Once = Once::new(); // using `INIT` will always create a temporary and | ``` ### Explanation Calling a method which mutates an interior mutable type has no effect as const-item are essentially inlined wherever they are used, meaning that they are copied directly into the relevant context when used rendering modification through interior mutability ineffective across usage of that const-item. The current implementation of this lint only warns on significant `std` and `core` interior mutable types, like `Once`, `AtomicI32`, ... this is done out of prudence and may be extended in the future. ---- This PR is an targeted alternative to rust-lang#132146. It avoids false-positives by adding an internal-only attribute `#[rustc_should_not_be_called_on_const_items]` on methods and functions that mutates an interior mutale type through a shared reference (mutable refrences are already linted by the `const_item_mutation` lint). It should also be noted that this is NOT an uplift of the more general [`clippy::borrow_interior_mutable_const`](https://rust-lang.github.io/rust-clippy/master/index.html#/borrow_interior_mutable_const) lint, which is a much more general lint regarding borrow of interior mutable types, but has false-positives that are completly avoided by this lint. A simple [GitHub Search](https://github.com/search?q=lang%3Arust+%2F%28%3F-i%29const+%5Ba-zA-Z0-9_%5D*%3A+Once%2F&type=code) reveals many instance where the user probably wanted to use a `static`-item instead. ---- ````@rustbot```` labels +I-lang-nominated +T-lang cc ````@traviscross```` r? compiler Fixes [IRLO - Forbidding creation of constant mutexes, etc](https://internals.rust-lang.org/t/forbidding-creation-of-constant-mutexes-etc/19005) Fixes rust-lang#132028 Fixes rust-lang#40543
2 parents 1f7ac87 + bf0e3d9 commit e74d96b

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

tests/ui/borrow_interior_mutable_const.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![allow(
55
clippy::declare_interior_mutable_const,
66
clippy::out_of_bounds_indexing,
7+
const_item_interior_mutations,
78
const_item_mutation,
89
unconditional_panic
910
)]

tests/ui/borrow_interior_mutable_const.stderr

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: borrow of a named constant with interior mutability
2-
--> tests/ui/borrow_interior_mutable_const.rs:35:17
2+
--> tests/ui/borrow_interior_mutable_const.rs:36:17
33
|
44
LL | let _ = &C;
55
| ^^
@@ -12,7 +12,7 @@ LL | #![deny(clippy::borrow_interior_mutable_const)]
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1313

1414
error: borrow of a named constant with interior mutability
15-
--> tests/ui/borrow_interior_mutable_const.rs:37:17
15+
--> tests/ui/borrow_interior_mutable_const.rs:38:17
1616
|
1717
LL | let _ = C.get();
1818
| ^
@@ -21,23 +21,23 @@ LL | let _ = C.get();
2121
= help: this lint can be silenced by assigning the value to a local variable before borrowing
2222

2323
error: borrow of a named constant with interior mutability
24-
--> tests/ui/borrow_interior_mutable_const.rs:42:17
24+
--> tests/ui/borrow_interior_mutable_const.rs:43:17
2525
|
2626
LL | let _ = &C;
2727
| ^^
2828
|
2929
= help: this lint can be silenced by assigning the value to a local variable before borrowing
3030

3131
error: borrow of a named constant with interior mutability
32-
--> tests/ui/borrow_interior_mutable_const.rs:43:17
32+
--> tests/ui/borrow_interior_mutable_const.rs:44:17
3333
|
3434
LL | let _ = &mut C;
3535
| ^^^^^^
3636
|
3737
= help: this lint can be silenced by assigning the value to a local variable before borrowing
3838

3939
error: borrow of a named constant with interior mutability
40-
--> tests/ui/borrow_interior_mutable_const.rs:47:9
40+
--> tests/ui/borrow_interior_mutable_const.rs:48:9
4141
|
4242
LL | C.swap(&local)
4343
| ^
@@ -46,31 +46,31 @@ LL | C.swap(&local)
4646
= help: this lint can be silenced by assigning the value to a local variable before borrowing
4747

4848
error: borrow of a named constant with interior mutability
49-
--> tests/ui/borrow_interior_mutable_const.rs:52:17
49+
--> tests/ui/borrow_interior_mutable_const.rs:53:17
5050
|
5151
LL | let _ = &C;
5252
| ^^
5353
|
5454
= help: this lint can be silenced by assigning the value to a local variable before borrowing
5555

5656
error: borrow of a named constant with interior mutability
57-
--> tests/ui/borrow_interior_mutable_const.rs:53:17
57+
--> tests/ui/borrow_interior_mutable_const.rs:54:17
5858
|
5959
LL | let _ = &C[0];
6060
| ^^^^^
6161
|
6262
= help: this lint can be silenced by assigning the value to a local variable before borrowing
6363

6464
error: borrow of a named constant with interior mutability
65-
--> tests/ui/borrow_interior_mutable_const.rs:54:17
65+
--> tests/ui/borrow_interior_mutable_const.rs:55:17
6666
|
6767
LL | let _ = &C[0].0;
6868
| ^^^^^^^
6969
|
7070
= help: this lint can be silenced by assigning the value to a local variable before borrowing
7171

7272
error: borrow of a named constant with interior mutability
73-
--> tests/ui/borrow_interior_mutable_const.rs:55:9
73+
--> tests/ui/borrow_interior_mutable_const.rs:56:9
7474
|
7575
LL | C[0].0.set(1);
7676
| ^^^^^^
@@ -79,23 +79,23 @@ LL | C[0].0.set(1);
7979
= help: this lint can be silenced by assigning the value to a local variable before borrowing
8080

8181
error: borrow of a named constant with interior mutability
82-
--> tests/ui/borrow_interior_mutable_const.rs:70:17
82+
--> tests/ui/borrow_interior_mutable_const.rs:71:17
8383
|
8484
LL | let _ = &S::C;
8585
| ^^^^^
8686
|
8787
= help: this lint can be silenced by assigning the value to a local variable before borrowing
8888

8989
error: borrow of a named constant with interior mutability
90-
--> tests/ui/borrow_interior_mutable_const.rs:71:17
90+
--> tests/ui/borrow_interior_mutable_const.rs:72:17
9191
|
9292
LL | let _ = &S::C.0;
9393
| ^^^^^^^
9494
|
9595
= help: this lint can be silenced by assigning the value to a local variable before borrowing
9696

9797
error: borrow of a named constant with interior mutability
98-
--> tests/ui/borrow_interior_mutable_const.rs:72:9
98+
--> tests/ui/borrow_interior_mutable_const.rs:73:9
9999
|
100100
LL | S::C.set(1);
101101
| ^^^^
@@ -104,7 +104,7 @@ LL | S::C.set(1);
104104
= help: this lint can be silenced by assigning the value to a local variable before borrowing
105105

106106
error: borrow of a named constant with interior mutability
107-
--> tests/ui/borrow_interior_mutable_const.rs:73:18
107+
--> tests/ui/borrow_interior_mutable_const.rs:74:18
108108
|
109109
LL | let _ = &*S::C;
110110
| ^^^^^
@@ -113,7 +113,7 @@ LL | let _ = &*S::C;
113113
= help: this lint can be silenced by assigning the value to a local variable before borrowing
114114

115115
error: borrow of a named constant with interior mutability
116-
--> tests/ui/borrow_interior_mutable_const.rs:74:9
116+
--> tests/ui/borrow_interior_mutable_const.rs:75:9
117117
|
118118
LL | (*S::C).set(1);
119119
| ^^^^^^^
@@ -122,39 +122,39 @@ LL | (*S::C).set(1);
122122
= help: this lint can be silenced by assigning the value to a local variable before borrowing
123123

124124
error: borrow of a named constant with interior mutability
125-
--> tests/ui/borrow_interior_mutable_const.rs:85:17
125+
--> tests/ui/borrow_interior_mutable_const.rs:86:17
126126
|
127127
LL | let _ = &CELL;
128128
| ^^^^^
129129
|
130130
= help: this lint can be silenced by assigning the value to a local variable before borrowing
131131

132132
error: borrow of a named constant with interior mutability
133-
--> tests/ui/borrow_interior_mutable_const.rs:109:25
133+
--> tests/ui/borrow_interior_mutable_const.rs:110:25
134134
|
135135
LL | let _ = &Self::C;
136136
| ^^^^^^^^
137137
|
138138
= help: this lint can be silenced by assigning the value to a local variable before borrowing
139139

140140
error: borrow of a named constant with interior mutability
141-
--> tests/ui/borrow_interior_mutable_const.rs:112:25
141+
--> tests/ui/borrow_interior_mutable_const.rs:113:25
142142
|
143143
LL | let _ = &Self::C.cell;
144144
| ^^^^^^^^^^^^^
145145
|
146146
= help: this lint can be silenced by assigning the value to a local variable before borrowing
147147

148148
error: borrow of a named constant with interior mutability
149-
--> tests/ui/borrow_interior_mutable_const.rs:113:25
149+
--> tests/ui/borrow_interior_mutable_const.rs:114:25
150150
|
151151
LL | let _ = &Self::C.cell.0;
152152
| ^^^^^^^^^^^^^^^
153153
|
154154
= help: this lint can be silenced by assigning the value to a local variable before borrowing
155155

156156
error: borrow of a named constant with interior mutability
157-
--> tests/ui/borrow_interior_mutable_const.rs:114:17
157+
--> tests/ui/borrow_interior_mutable_const.rs:115:17
158158
|
159159
LL | Self::C.cell.0.set(T::DEFAULT);
160160
| ^^^^^^^^^^^^^^
@@ -163,31 +163,31 @@ LL | Self::C.cell.0.set(T::DEFAULT);
163163
= help: this lint can be silenced by assigning the value to a local variable before borrowing
164164

165165
error: borrow of a named constant with interior mutability
166-
--> tests/ui/borrow_interior_mutable_const.rs:128:17
166+
--> tests/ui/borrow_interior_mutable_const.rs:129:17
167167
|
168168
LL | let _ = &u32::VALUE;
169169
| ^^^^^^^^^^^
170170
|
171171
= help: this lint can be silenced by assigning the value to a local variable before borrowing
172172

173173
error: borrow of a named constant with interior mutability
174-
--> tests/ui/borrow_interior_mutable_const.rs:145:21
174+
--> tests/ui/borrow_interior_mutable_const.rs:146:21
175175
|
176176
LL | let _ = &<u32 as Trait<T>>::VALUE;
177177
| ^^^^^^^^^^^^^^^^^^^^^^^^^
178178
|
179179
= help: this lint can be silenced by assigning the value to a local variable before borrowing
180180

181181
error: borrow of a named constant with interior mutability
182-
--> tests/ui/borrow_interior_mutable_const.rs:172:17
182+
--> tests/ui/borrow_interior_mutable_const.rs:173:17
183183
|
184184
LL | let _ = &C;
185185
| ^^
186186
|
187187
= help: this lint can be silenced by assigning the value to a local variable before borrowing
188188

189189
error: borrow of a named constant with interior mutability
190-
--> tests/ui/borrow_interior_mutable_const.rs:173:18
190+
--> tests/ui/borrow_interior_mutable_const.rs:174:18
191191
|
192192
LL | let _ = &C[0];
193193
| ^^^^
@@ -196,47 +196,47 @@ LL | let _ = &C[0];
196196
= help: this lint can be silenced by assigning the value to a local variable before borrowing
197197

198198
error: borrow of a named constant with interior mutability
199-
--> tests/ui/borrow_interior_mutable_const.rs:174:17
199+
--> tests/ui/borrow_interior_mutable_const.rs:175:17
200200
|
201201
LL | let _ = &C.0[0];
202202
| ^^^^^^^
203203
|
204204
= help: this lint can be silenced by assigning the value to a local variable before borrowing
205205

206206
error: borrow of a named constant with interior mutability
207-
--> tests/ui/borrow_interior_mutable_const.rs:190:17
207+
--> tests/ui/borrow_interior_mutable_const.rs:191:17
208208
|
209209
LL | let _ = &C[1];
210210
| ^^^^^
211211
|
212212
= help: this lint can be silenced by assigning the value to a local variable before borrowing
213213

214214
error: borrow of a named constant with interior mutability
215-
--> tests/ui/borrow_interior_mutable_const.rs:194:21
215+
--> tests/ui/borrow_interior_mutable_const.rs:195:21
216216
|
217217
LL | let _ = &C[i];
218218
| ^^^^^
219219
|
220220
= help: this lint can be silenced by assigning the value to a local variable before borrowing
221221

222222
error: borrow of a named constant with interior mutability
223-
--> tests/ui/borrow_interior_mutable_const.rs:198:17
223+
--> tests/ui/borrow_interior_mutable_const.rs:199:17
224224
|
225225
LL | let _ = &interior_mutable_const::WRAPPED_PRIVATE_UNFROZEN_VARIANT;
226226
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
227227
|
228228
= help: this lint can be silenced by assigning the value to a local variable before borrowing
229229

230230
error: borrow of a named constant with interior mutability
231-
--> tests/ui/borrow_interior_mutable_const.rs:216:17
231+
--> tests/ui/borrow_interior_mutable_const.rs:217:17
232232
|
233233
LL | let _ = &S::VALUE;
234234
| ^^^^^^^^^
235235
|
236236
= help: this lint can be silenced by assigning the value to a local variable before borrowing
237237

238238
error: borrow of a named constant with interior mutability
239-
--> tests/ui/borrow_interior_mutable_const.rs:218:17
239+
--> tests/ui/borrow_interior_mutable_const.rs:219:17
240240
|
241241
LL | let _ = &S::VALUE.1;
242242
| ^^^^^^^^^^^

0 commit comments

Comments
 (0)