]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/undocumented_unsafe_blocks.rs
Auto merge of #97944 - nikic:freebsd-update, r=Mark-Simulacrum
[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 // Invalid comments
254
255 #[rustfmt::skip]
256 fn inline_block_comment() {
257     /* Safety: */ unsafe {}
258 }
259
260 fn no_comment() {
261     unsafe {}
262 }
263
264 fn no_comment_array() {
265     let _ = [unsafe { 14 }, unsafe { 15 }, 42, unsafe { 16 }];
266 }
267
268 fn no_comment_tuple() {
269     let _ = (42, unsafe {}, "test", unsafe {});
270 }
271
272 fn no_comment_unary() {
273     let _ = *unsafe { &42 };
274 }
275
276 #[allow(clippy::match_single_binding)]
277 fn no_comment_match() {
278     let _ = match unsafe {} {
279         _ => {},
280     };
281 }
282
283 fn no_comment_addr_of() {
284     let _ = &unsafe {};
285 }
286
287 fn no_comment_repeat() {
288     let _ = [unsafe {}; 5];
289 }
290
291 fn local_no_comment() {
292     let _ = unsafe {};
293 }
294
295 fn no_comment_macro_call() {
296     macro_rules! t {
297         ($b:expr) => {
298             $b
299         };
300     }
301
302     t!(unsafe {});
303 }
304
305 fn no_comment_macro_def() {
306     macro_rules! t {
307         () => {
308             unsafe {}
309         };
310     }
311
312     t!();
313 }
314
315 fn trailing_comment() {
316     unsafe {} // SAFETY:
317 }
318
319 fn internal_comment() {
320     unsafe {
321         // SAFETY:
322     }
323 }
324
325 fn interference() {
326     // SAFETY
327
328     let _ = 42;
329
330     unsafe {};
331 }
332
333 pub fn print_binary_tree() {
334     println!("{}", unsafe { String::from_utf8_unchecked(vec![]) });
335 }
336
337 mod unsafe_impl_smoke_test {
338     unsafe trait A {}
339
340     // error: no safety comment
341     unsafe impl A for () {}
342
343     // Safety: ok
344     unsafe impl A for (i32) {}
345
346     mod sub_mod {
347         // error:
348         unsafe impl B for (u32) {}
349         unsafe trait B {}
350     }
351
352     #[rustfmt::skip]
353     mod sub_mod2 {
354         // 
355         // SAFETY: ok
356         // 
357
358         unsafe impl B for (u32) {}
359         unsafe trait B {}
360     }
361 }
362
363 mod unsafe_impl_from_macro {
364     unsafe trait T {}
365
366     // error
367     macro_rules! no_safety_comment {
368         ($t:ty) => {
369             unsafe impl T for $t {}
370         };
371     }
372
373     // ok
374     no_safety_comment!(());
375
376     // ok
377     macro_rules! with_safety_comment {
378         ($t:ty) => {
379             // SAFETY:
380             unsafe impl T for $t {}
381         };
382     }
383
384     // ok
385     with_safety_comment!((i32));
386 }
387
388 mod unsafe_impl_macro_and_not_macro {
389     unsafe trait T {}
390
391     // error
392     macro_rules! no_safety_comment {
393         ($t:ty) => {
394             unsafe impl T for $t {}
395         };
396     }
397
398     // ok
399     no_safety_comment!(());
400
401     // error
402     unsafe impl T for (i32) {}
403
404     // ok
405     no_safety_comment!(u32);
406
407     // error
408     unsafe impl T for (bool) {}
409 }
410
411 #[rustfmt::skip]
412 mod unsafe_impl_valid_comment {
413     unsafe trait SaFety {}
414     // SaFety:
415     unsafe impl SaFety for () {}
416
417     unsafe trait MultiLineComment {}
418     // The following impl is safe
419     // ...
420     // Safety: reason
421     unsafe impl MultiLineComment for () {}
422
423     unsafe trait NoAscii {}
424     // 安全 SAFETY: 以下のコードは安全です
425     unsafe impl NoAscii for () {}
426
427     unsafe trait InlineAndPrecedingComment {}
428     // SAFETY:
429     /* comment */ unsafe impl InlineAndPrecedingComment for () {}
430
431     unsafe trait BuriedSafety {}
432     // Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
433     // incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
434     // ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in
435     // reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
436     // occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
437     // laborum. Safety:
438     // Tellus elementum sagittis vitae et leo duis ut diam quam. Sit amet nulla facilisi
439     // morbi tempus iaculis urna. Amet luctus venenatis lectus magna. At quis risus sed vulputate odio
440     // ut. Luctus venenatis lectus magna fringilla urna. Tortor id aliquet lectus proin nibh nisl
441     // condimentum id venenatis. Vulputate dignissim suspendisse in est ante in nibh mauris cursus.
442     unsafe impl BuriedSafety for () {}
443
444     unsafe trait MultiLineBlockComment {}
445     /* This is a description
446      * Safety: */
447     unsafe impl MultiLineBlockComment for () {}
448 }
449
450 #[rustfmt::skip]
451 mod unsafe_impl_invalid_comment {
452     unsafe trait NoComment {}
453
454     unsafe impl NoComment for () {}
455
456     unsafe trait InlineComment {}
457
458     /* SAFETY: */ unsafe impl InlineComment for () {}
459
460     unsafe trait TrailingComment {}
461
462     unsafe impl TrailingComment for () {} // SAFETY:
463
464     unsafe trait Interference {}
465     // SAFETY:
466     const BIG_NUMBER: i32 = 1000000;
467     unsafe impl Interference for () {}
468 }
469
470 unsafe trait ImplInFn {}
471
472 fn impl_in_fn() {
473     // error
474     unsafe impl ImplInFn for () {}
475
476     // SAFETY: ok
477     unsafe impl ImplInFn for (i32) {}
478 }
479
480 unsafe trait CrateRoot {}
481
482 // error
483 unsafe impl CrateRoot for () {}
484
485 // SAFETY: ok
486 unsafe impl CrateRoot for (i32) {}
487
488 fn main() {}