]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/undocumented_unsafe_blocks.rs
Auto merge of #99174 - scottmcm:reoptimize-layout-array, r=joshtriplett
[rust.git] / src / tools / clippy / tests / ui / undocumented_unsafe_blocks.rs
1 // aux-build:proc_macro_unsafe.rs
2
3 #![warn(clippy::undocumented_unsafe_blocks)]
4 #![allow(clippy::let_unit_value, clippy::missing_safety_doc)]
5
6 extern crate proc_macro_unsafe;
7
8 // Valid comments
9
10 fn nested_local() {
11     let _ = {
12         let _ = {
13             // SAFETY:
14             let _ = unsafe {};
15         };
16     };
17 }
18
19 fn deep_nest() {
20     let _ = {
21         let _ = {
22             // SAFETY:
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
36                                 // SAFETY:
37                                 unsafe {};
38                             };
39                         };
40                     };
41
42                     // Safety:
43                     unsafe {};
44                 };
45             };
46         };
47
48         // Safety:
49         unsafe {};
50     };
51
52     // SAFETY:
53     unsafe {};
54 }
55
56 fn local_tuple_expression() {
57     // Safety:
58     let _ = (42, unsafe {});
59 }
60
61 fn line_comment() {
62     // Safety:
63     unsafe {}
64 }
65
66 fn line_comment_newlines() {
67     // SAFETY:
68
69     unsafe {}
70 }
71
72 fn line_comment_empty() {
73     // Safety:
74     //
75     //
76     //
77     unsafe {}
78 }
79
80 fn line_comment_with_extras() {
81     // This is a description
82     // Safety:
83     unsafe {}
84 }
85
86 fn block_comment() {
87     /* Safety: */
88     unsafe {}
89 }
90
91 fn block_comment_newlines() {
92     /* SAFETY: */
93
94     unsafe {}
95 }
96
97 fn block_comment_with_extras() {
98     /* This is a description
99      * SAFETY:
100      */
101     unsafe {}
102 }
103
104 fn block_comment_terminator_same_line() {
105     /* This is a description
106      * Safety: */
107     unsafe {}
108 }
109
110 fn 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
124 fn safety_with_prepended_text() {
125     // This is a test. safety:
126     unsafe {}
127 }
128
129 fn local_line_comment() {
130     // Safety:
131     let _ = unsafe {};
132 }
133
134 fn local_block_comment() {
135     /* SAFETY: */
136     let _ = unsafe {};
137 }
138
139 fn comment_array() {
140     // Safety:
141     let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }];
142 }
143
144 fn comment_tuple() {
145     // sAFETY:
146     let _ = (42, unsafe {}, "test", unsafe {});
147 }
148
149 fn comment_unary() {
150     // SAFETY:
151     let _ = *unsafe { &42 };
152 }
153
154 #[allow(clippy::match_single_binding)]
155 fn comment_match() {
156     // SAFETY:
157     let _ = match unsafe {} {
158         _ => {},
159     };
160 }
161
162 fn comment_addr_of() {
163     // Safety:
164     let _ = &unsafe {};
165 }
166
167 fn comment_repeat() {
168     // Safety:
169     let _ = [unsafe {}; 5];
170 }
171
172 fn comment_macro_call() {
173     macro_rules! t {
174         ($b:expr) => {
175             $b
176         };
177     }
178
179     t!(
180         // SAFETY:
181         unsafe {}
182     );
183 }
184
185 fn comment_macro_def() {
186     macro_rules! t {
187         () => {
188             // Safety:
189             unsafe {}
190         };
191     }
192
193     t!();
194 }
195
196 fn non_ascii_comment() {
197     // ॐ᧻໒ SaFeTy: ௵∰
198     unsafe {};
199 }
200
201 fn local_commented_block() {
202     let _ =
203         // safety:
204         unsafe {};
205 }
206
207 fn local_nest() {
208     // safety:
209     let _ = [(42, unsafe {}, unsafe {}), (52, unsafe {}, unsafe {})];
210 }
211
212 fn in_fn_call(x: *const u32) {
213     fn f(x: u32) {}
214
215     // Safety: reason
216     f(unsafe { *x });
217 }
218
219 fn 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
226 fn 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
236 fn in_macro_call(x: *const u32) {
237     // Safety: reason
238     println!("{}", unsafe { *x });
239 }
240
241 fn in_multiline_macro_call(x: *const u32) {
242     println!(
243         "{}",
244         // Safety: reason
245         unsafe { *x },
246     );
247 }
248
249 fn from_proc_macro() {
250     proc_macro_unsafe::unsafe_block!(token);
251 }
252
253 fn in_closure(x: *const u32) {
254     // Safety: reason
255     let _ = || unsafe { *x };
256 }
257
258 // Invalid comments
259
260 #[rustfmt::skip]
261 fn inline_block_comment() {
262     /* Safety: */ unsafe {}
263 }
264
265 fn no_comment() {
266     unsafe {}
267 }
268
269 fn no_comment_array() {
270     let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }];
271 }
272
273 fn no_comment_tuple() {
274     let _ = (42, unsafe {}, "test", unsafe {});
275 }
276
277 fn no_comment_unary() {
278     let _ = *unsafe { &42 };
279 }
280
281 #[allow(clippy::match_single_binding)]
282 fn no_comment_match() {
283     let _ = match unsafe {} {
284         _ => {},
285     };
286 }
287
288 fn no_comment_addr_of() {
289     let _ = &unsafe {};
290 }
291
292 fn no_comment_repeat() {
293     let _ = [unsafe {}; 5];
294 }
295
296 fn local_no_comment() {
297     let _ = unsafe {};
298 }
299
300 fn no_comment_macro_call() {
301     macro_rules! t {
302         ($b:expr) => {
303             $b
304         };
305     }
306
307     t!(unsafe {});
308 }
309
310 fn no_comment_macro_def() {
311     macro_rules! t {
312         () => {
313             unsafe {}
314         };
315     }
316
317     t!();
318 }
319
320 fn trailing_comment() {
321     unsafe {} // SAFETY:
322 }
323
324 fn internal_comment() {
325     unsafe {
326         // SAFETY:
327     }
328 }
329
330 fn interference() {
331     // SAFETY
332
333     let _ = 42;
334
335     unsafe {};
336 }
337
338 pub fn print_binary_tree() {
339     println!("{}", unsafe { String::from_utf8_unchecked(vec![]) });
340 }
341
342 mod 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 {
359         //
360         // SAFETY: ok
361         //
362
363         unsafe impl B for (u32) {}
364         unsafe trait B {}
365     }
366 }
367
368 mod 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
393 mod 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]
417 mod 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]
456 mod 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
475 unsafe trait ImplInFn {}
476
477 fn impl_in_fn() {
478     // error
479     unsafe impl ImplInFn for () {}
480
481     // SAFETY: ok
482     unsafe impl ImplInFn for (i32) {}
483 }
484
485 unsafe trait CrateRoot {}
486
487 // error
488 unsafe impl CrateRoot for () {}
489
490 // SAFETY: ok
491 unsafe impl CrateRoot for (i32) {}
492
493 fn main() {}