]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/undocumented_unsafe_blocks.rs
New upstream version 1.67.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / undocumented_unsafe_blocks.rs
CommitLineData
04454e1e
FG
1// aux-build:proc_macro_unsafe.rs
2
487cf647 3#![warn(clippy::undocumented_unsafe_blocks, clippy::unnecessary_safety_comment)]
923072b8 4#![allow(clippy::let_unit_value, clippy::missing_safety_doc)]
04454e1e
FG
5
6extern crate proc_macro_unsafe;
3c0e092e
XL
7
8// Valid comments
9
10fn nested_local() {
11 let _ = {
12 let _ = {
a2a8927a 13 // SAFETY:
3c0e092e
XL
14 let _ = unsafe {};
15 };
16 };
17}
18
19fn deep_nest() {
20 let _ = {
21 let _ = {
a2a8927a 22 // SAFETY:
3c0e092e
XL
23 let _ = unsafe {};
24
25 // Safety:
26 unsafe {};
27
28 let _ = {
29 let _ = {
30 let _ = {
31 let _ = {
32 let _ = {
33 // Safety:
34 let _ = unsafe {};
35
a2a8927a 36 // SAFETY:
3c0e092e
XL
37 unsafe {};
38 };
39 };
40 };
41
42 // Safety:
43 unsafe {};
44 };
45 };
46 };
47
48 // Safety:
49 unsafe {};
50 };
51
a2a8927a 52 // SAFETY:
3c0e092e
XL
53 unsafe {};
54}
55
56fn local_tuple_expression() {
57 // Safety:
58 let _ = (42, unsafe {});
59}
60
61fn line_comment() {
62 // Safety:
63 unsafe {}
64}
65
66fn line_comment_newlines() {
a2a8927a 67 // SAFETY:
3c0e092e
XL
68
69 unsafe {}
70}
71
72fn line_comment_empty() {
73 // Safety:
74 //
75 //
76 //
77 unsafe {}
78}
79
80fn line_comment_with_extras() {
81 // This is a description
82 // Safety:
83 unsafe {}
84}
85
86fn block_comment() {
87 /* Safety: */
88 unsafe {}
89}
90
91fn block_comment_newlines() {
a2a8927a 92 /* SAFETY: */
3c0e092e
XL
93
94 unsafe {}
95}
96
3c0e092e
XL
97fn block_comment_with_extras() {
98 /* This is a description
a2a8927a 99 * SAFETY:
3c0e092e
XL
100 */
101 unsafe {}
102}
103
104fn block_comment_terminator_same_line() {
105 /* This is a description
106 * Safety: */
107 unsafe {}
108}
109
110fn buried_safety() {
111 // Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
112 // incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
113 // ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
114 // reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
115 // occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
116 // laborum. Safety:
117 // Tellus elementum sagittis vitae et leo duis ut diam quam. Sit amet nulla facilisi
118 // morbi tempus iaculis urna. Amet luctus venenatis lectus magna. At quis risus sed vulputate odio
119 // ut. Luctus venenatis lectus magna fringilla urna. Tortor id aliquet lectus proin nibh nisl
120 // condimentum id venenatis. Vulputate dignissim suspendisse in est ante in nibh mauris cursus.
121 unsafe {}
122}
123
124fn safety_with_prepended_text() {
a2a8927a 125 // This is a test. safety:
3c0e092e
XL
126 unsafe {}
127}
128
129fn local_line_comment() {
130 // Safety:
131 let _ = unsafe {};
132}
133
134fn local_block_comment() {
a2a8927a 135 /* SAFETY: */
3c0e092e
XL
136 let _ = unsafe {};
137}
138
139fn comment_array() {
140 // Safety:
141 let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }];
142}
143
144fn comment_tuple() {
a2a8927a 145 // sAFETY:
3c0e092e
XL
146 let _ = (42, unsafe {}, "test", unsafe {});
147}
148
149fn comment_unary() {
a2a8927a 150 // SAFETY:
3c0e092e
XL
151 let _ = *unsafe { &42 };
152}
153
154#[allow(clippy::match_single_binding)]
155fn comment_match() {
a2a8927a 156 // SAFETY:
3c0e092e
XL
157 let _ = match unsafe {} {
158 _ => {},
159 };
160}
161
162fn comment_addr_of() {
163 // Safety:
164 let _ = &unsafe {};
165}
166
167fn comment_repeat() {
168 // Safety:
169 let _ = [unsafe {}; 5];
170}
171
172fn comment_macro_call() {
173 macro_rules! t {
174 ($b:expr) => {
175 $b
176 };
177 }
178
179 t!(
a2a8927a 180 // SAFETY:
3c0e092e
XL
181 unsafe {}
182 );
183}
184
185fn comment_macro_def() {
186 macro_rules! t {
187 () => {
188 // Safety:
189 unsafe {}
190 };
191 }
192
193 t!();
194}
195
196fn non_ascii_comment() {
a2a8927a 197 // ॐ᧻໒ SaFeTy: ௵∰
3c0e092e
XL
198 unsafe {};
199}
200
201fn local_commented_block() {
202 let _ =
a2a8927a 203 // safety:
3c0e092e
XL
204 unsafe {};
205}
206
207fn local_nest() {
a2a8927a 208 // safety:
3c0e092e
XL
209 let _ = [(42, unsafe {}, unsafe {}), (52, unsafe {}, unsafe {})];
210}
211
04454e1e
FG
212fn in_fn_call(x: *const u32) {
213 fn f(x: u32) {}
214
215 // Safety: reason
216 f(unsafe { *x });
217}
218
219fn multi_in_fn_call(x: *const u32) {
220 fn f(x: u32, y: u32) {}
221
222 // Safety: reason
223 f(unsafe { *x }, unsafe { *x });
224}
225
226fn in_multiline_fn_call(x: *const u32) {
227 fn f(x: u32, y: u32) {}
228
229 f(
230 // Safety: reason
231 unsafe { *x },
232 0,
233 );
234}
235
236fn in_macro_call(x: *const u32) {
237 // Safety: reason
238 println!("{}", unsafe { *x });
239}
240
241fn in_multiline_macro_call(x: *const u32) {
242 println!(
243 "{}",
244 // Safety: reason
245 unsafe { *x },
246 );
247}
248
249fn from_proc_macro() {
250 proc_macro_unsafe::unsafe_block!(token);
251}
252
064997fb
FG
253fn in_closure(x: *const u32) {
254 // Safety: reason
255 let _ = || unsafe { *x };
256}
257
3c0e092e
XL
258// Invalid comments
259
04454e1e
FG
260#[rustfmt::skip]
261fn inline_block_comment() {
262 /* Safety: */ unsafe {}
263}
264
3c0e092e
XL
265fn no_comment() {
266 unsafe {}
267}
268
269fn no_comment_array() {
270 let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }];
271}
272
273fn no_comment_tuple() {
274 let _ = (42, unsafe {}, "test", unsafe {});
275}
276
277fn no_comment_unary() {
278 let _ = *unsafe { &42 };
279}
280
281#[allow(clippy::match_single_binding)]
282fn no_comment_match() {
283 let _ = match unsafe {} {
284 _ => {},
285 };
286}
287
288fn no_comment_addr_of() {
289 let _ = &unsafe {};
290}
291
292fn no_comment_repeat() {
293 let _ = [unsafe {}; 5];
294}
295
296fn local_no_comment() {
297 let _ = unsafe {};
298}
299
300fn no_comment_macro_call() {
301 macro_rules! t {
302 ($b:expr) => {
303 $b
304 };
305 }
306
307 t!(unsafe {});
308}
309
310fn no_comment_macro_def() {
311 macro_rules! t {
312 () => {
313 unsafe {}
314 };
315 }
316
317 t!();
318}
319
320fn trailing_comment() {
a2a8927a 321 unsafe {} // SAFETY:
3c0e092e
XL
322}
323
324fn internal_comment() {
325 unsafe {
a2a8927a 326 // SAFETY:
3c0e092e
XL
327 }
328}
329
330fn interference() {
a2a8927a 331 // SAFETY
3c0e092e
XL
332
333 let _ = 42;
334
335 unsafe {};
336}
337
a2a8927a
XL
338pub fn print_binary_tree() {
339 println!("{}", unsafe { String::from_utf8_unchecked(vec![]) });
340}
341
923072b8
FG
342mod unsafe_impl_smoke_test {
343 unsafe trait A {}
344
345 // error: no safety comment
346 unsafe impl A for () {}
347
348 // Safety: ok
349 unsafe impl A for (i32) {}
350
351 mod sub_mod {
352 // error:
353 unsafe impl B for (u32) {}
354 unsafe trait B {}
355 }
356
357 #[rustfmt::skip]
358 mod sub_mod2 {
064997fb 359 //
923072b8 360 // SAFETY: ok
064997fb 361 //
923072b8
FG
362
363 unsafe impl B for (u32) {}
364 unsafe trait B {}
365 }
366}
367
368mod unsafe_impl_from_macro {
369 unsafe trait T {}
370
371 // error
372 macro_rules! no_safety_comment {
373 ($t:ty) => {
374 unsafe impl T for $t {}
375 };
376 }
377
378 // ok
379 no_safety_comment!(());
380
381 // ok
382 macro_rules! with_safety_comment {
383 ($t:ty) => {
384 // SAFETY:
385 unsafe impl T for $t {}
386 };
387 }
388
389 // ok
390 with_safety_comment!((i32));
391}
392
393mod unsafe_impl_macro_and_not_macro {
394 unsafe trait T {}
395
396 // error
397 macro_rules! no_safety_comment {
398 ($t:ty) => {
399 unsafe impl T for $t {}
400 };
401 }
402
403 // ok
404 no_safety_comment!(());
405
406 // error
407 unsafe impl T for (i32) {}
408
409 // ok
410 no_safety_comment!(u32);
411
412 // error
413 unsafe impl T for (bool) {}
414}
415
416#[rustfmt::skip]
417mod unsafe_impl_valid_comment {
418 unsafe trait SaFety {}
419 // SaFety:
420 unsafe impl SaFety for () {}
421
422 unsafe trait MultiLineComment {}
423 // The following impl is safe
424 // ...
425 // Safety: reason
426 unsafe impl MultiLineComment for () {}
427
428 unsafe trait NoAscii {}
429 // 安全 SAFETY: 以下のコードは安全です
430 unsafe impl NoAscii for () {}
431
432 unsafe trait InlineAndPrecedingComment {}
433 // SAFETY:
434 /* comment */ unsafe impl InlineAndPrecedingComment for () {}
435
436 unsafe trait BuriedSafety {}
437 // Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
438 // incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
439 // ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
440 // reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
441 // occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
442 // laborum. Safety:
443 // Tellus elementum sagittis vitae et leo duis ut diam quam. Sit amet nulla facilisi
444 // morbi tempus iaculis urna. Amet luctus venenatis lectus magna. At quis risus sed vulputate odio
445 // ut. Luctus venenatis lectus magna fringilla urna. Tortor id aliquet lectus proin nibh nisl
446 // condimentum id venenatis. Vulputate dignissim suspendisse in est ante in nibh mauris cursus.
447 unsafe impl BuriedSafety for () {}
448
449 unsafe trait MultiLineBlockComment {}
450 /* This is a description
451 * Safety: */
452 unsafe impl MultiLineBlockComment for () {}
453}
454
455#[rustfmt::skip]
456mod unsafe_impl_invalid_comment {
457 unsafe trait NoComment {}
458
459 unsafe impl NoComment for () {}
460
461 unsafe trait InlineComment {}
462
463 /* SAFETY: */ unsafe impl InlineComment for () {}
464
465 unsafe trait TrailingComment {}
466
467 unsafe impl TrailingComment for () {} // SAFETY:
468
469 unsafe trait Interference {}
470 // SAFETY:
471 const BIG_NUMBER: i32 = 1000000;
472 unsafe impl Interference for () {}
473}
474
475unsafe trait ImplInFn {}
476
477fn impl_in_fn() {
478 // error
479 unsafe impl ImplInFn for () {}
480
481 // SAFETY: ok
482 unsafe impl ImplInFn for (i32) {}
483}
484
485unsafe trait CrateRoot {}
486
487// error
488unsafe impl CrateRoot for () {}
489
490// SAFETY: ok
491unsafe impl CrateRoot for (i32) {}
492
487cf647
FG
493fn issue_9142() {
494 // SAFETY: ok
495 let _ =
496 // we need this comment to avoid rustfmt putting
497 // it all on one line
498 unsafe {};
499
500 // SAFETY: this is more than one level away, so it should warn
501 let _ = {
502 if unsafe { true } {
503 todo!();
504 } else {
505 let bar = unsafe {};
506 todo!();
507 bar
508 }
509 };
510}
511
3c0e092e 512fn main() {}