blob: 132673d5164abb83b9a86de76a843e80e070880c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
//@needs-asm-support
//@aux-build:proc_macros.rs
#![expect(
dropping_copy_types,
clippy::unnecessary_operation,
clippy::unnecessary_literal_unwrap
)]
#![warn(clippy::multiple_unsafe_ops_per_block)]
extern crate proc_macros;
use proc_macros::external;
use core::arch::asm;
fn raw_ptr() -> *const () {
core::ptr::null()
}
unsafe fn not_very_safe() {}
struct Sample;
impl Sample {
unsafe fn not_very_safe(&self) {}
}
#[allow(non_upper_case_globals)]
const sample: Sample = Sample;
union U {
i: i32,
u: u32,
}
static mut STATIC: i32 = 0;
fn test1() {
unsafe {
//~^ multiple_unsafe_ops_per_block
STATIC += 1;
not_very_safe();
}
}
fn test2() {
let u = U { i: 0 };
unsafe {
//~^ multiple_unsafe_ops_per_block
drop(u.u);
*raw_ptr();
}
}
fn test3() {
unsafe {
//~^ multiple_unsafe_ops_per_block
asm!("nop");
sample.not_very_safe();
STATIC = 0;
}
}
fn test_all() {
let u = U { i: 0 };
unsafe {
//~^ multiple_unsafe_ops_per_block
drop(u.u);
drop(STATIC);
sample.not_very_safe();
not_very_safe();
*raw_ptr();
asm!("nop");
}
}
// no lint
fn correct1() {
unsafe {
STATIC += 1;
}
}
// no lint
fn correct2() {
unsafe {
STATIC += 1;
}
unsafe {
*raw_ptr();
}
}
// no lint
fn correct3() {
let u = U { u: 0 };
unsafe {
not_very_safe();
}
unsafe {
drop(u.i);
}
}
fn issue10064() {
unsafe fn read_char_bad(ptr: *const u8) -> char {
unsafe { char::from_u32_unchecked(*ptr.cast::<u32>()) }
//~^ multiple_unsafe_ops_per_block
}
// no lint
unsafe fn read_char_good(ptr: *const u8) -> char {
let int_value = unsafe { *ptr.cast::<u32>() };
unsafe { core::char::from_u32_unchecked(int_value) }
}
}
// no lint
fn issue10259() {
external!(unsafe {
*core::ptr::null::<()>();
*core::ptr::null::<()>();
});
}
fn issue10367() {
fn fn_ptr(x: unsafe fn()) {
unsafe {
//~^ multiple_unsafe_ops_per_block
x();
x();
}
}
fn assoc_const() {
trait X {
const X: unsafe fn();
}
fn _f<T: X>() {
unsafe {
//~^ multiple_unsafe_ops_per_block
T::X();
T::X();
}
}
}
fn field_fn_ptr(x: unsafe fn()) {
struct X(unsafe fn());
let x = X(x);
unsafe {
//~^ multiple_unsafe_ops_per_block
x.0();
x.0();
}
}
}
// await expands to an unsafe block with several operations, but this is fine.
async fn issue11312() {
async fn helper() {}
helper().await;
}
async fn issue13879() {
async fn foo() {}
// no lint: nothing unsafe beyond the `await` which we ignore
unsafe {
foo().await;
}
// no lint: only one unsafe call beyond the `await`
unsafe {
not_very_safe();
foo().await;
}
// lint: two unsafe calls beyond the `await`
unsafe {
//~^ multiple_unsafe_ops_per_block
not_very_safe();
STATIC += 1;
foo().await;
}
async unsafe fn foo_unchecked() {}
// no lint: only one unsafe call in the `await`ed expr
unsafe {
foo_unchecked().await;
}
// lint: one unsafe call in the `await`ed expr, and one outside
unsafe {
//~^ multiple_unsafe_ops_per_block
not_very_safe();
foo_unchecked().await;
}
// lint: two unsafe calls in the `await`ed expr
unsafe {
//~^ multiple_unsafe_ops_per_block
Some(foo_unchecked()).unwrap_unchecked().await;
}
}
fn main() {}
|