]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/non_send_fields_in_send_ty.rs
Auto merge of #99963 - cjgillot:iter-submodule, r=compiler-errors
[rust.git] / src / tools / clippy / tests / ui / non_send_fields_in_send_ty.rs
1 #![warn(clippy::non_send_fields_in_send_ty)]
2 #![allow(suspicious_auto_trait_impls)]
3 #![feature(extern_types)]
4
5 use std::cell::UnsafeCell;
6 use std::ptr::NonNull;
7 use std::rc::Rc;
8 use std::sync::{Arc, Mutex, MutexGuard};
9
10 // disrustor / RUSTSEC-2020-0150
11 pub struct RingBuffer<T> {
12     data: Vec<UnsafeCell<T>>,
13     capacity: usize,
14     mask: usize,
15 }
16
17 unsafe impl<T> Send for RingBuffer<T> {}
18
19 // noise_search / RUSTSEC-2020-0141
20 pub struct MvccRwLock<T> {
21     raw: *const T,
22     lock: Mutex<Box<T>>,
23 }
24
25 unsafe impl<T> Send for MvccRwLock<T> {}
26
27 // async-coap / RUSTSEC-2020-0124
28 pub struct ArcGuard<RC, T> {
29     inner: T,
30     head: Arc<RC>,
31 }
32
33 unsafe impl<RC, T: Send> Send for ArcGuard<RC, T> {}
34
35 // rusb / RUSTSEC-2020-0098
36 extern "C" {
37     type libusb_device_handle;
38 }
39
40 pub trait UsbContext {
41     // some user trait that does not guarantee `Send`
42 }
43
44 pub struct DeviceHandle<T: UsbContext> {
45     context: T,
46     handle: NonNull<libusb_device_handle>,
47 }
48
49 unsafe impl<T: UsbContext> Send for DeviceHandle<T> {}
50
51 // Other basic tests
52 pub struct NoGeneric {
53     rc_is_not_send: Rc<String>,
54 }
55
56 unsafe impl Send for NoGeneric {}
57
58 pub struct MultiField<T> {
59     field1: T,
60     field2: T,
61     field3: T,
62 }
63
64 unsafe impl<T> Send for MultiField<T> {}
65
66 pub enum MyOption<T> {
67     MySome(T),
68     MyNone,
69 }
70
71 unsafe impl<T> Send for MyOption<T> {}
72
73 // Test types that contain `NonNull` instead of raw pointers (#8045)
74 pub struct WrappedNonNull(UnsafeCell<NonNull<()>>);
75
76 unsafe impl Send for WrappedNonNull {}
77
78 // Multiple type parameters
79 pub struct MultiParam<A, B> {
80     vec: Vec<(A, B)>,
81 }
82
83 unsafe impl<A, B> Send for MultiParam<A, B> {}
84
85 // Tests for raw pointer heuristic
86 extern "C" {
87     type NonSend;
88 }
89
90 pub struct HeuristicTest {
91     // raw pointers are allowed
92     field1: Vec<*const NonSend>,
93     field2: [*const NonSend; 3],
94     field3: (*const NonSend, *const NonSend, *const NonSend),
95     // not allowed when it contains concrete `!Send` field
96     field4: (*const NonSend, Rc<u8>),
97     // nested raw pointer is also allowed
98     field5: Vec<Vec<*const NonSend>>,
99 }
100
101 unsafe impl Send for HeuristicTest {}
102
103 // Test attributes
104 #[allow(clippy::non_send_fields_in_send_ty)]
105 pub struct AttrTest1<T>(T);
106
107 pub struct AttrTest2<T> {
108     #[allow(clippy::non_send_fields_in_send_ty)]
109     field: T,
110 }
111
112 pub enum AttrTest3<T> {
113     #[allow(clippy::non_send_fields_in_send_ty)]
114     Enum1(T),
115     Enum2(T),
116 }
117
118 unsafe impl<T> Send for AttrTest1<T> {}
119 unsafe impl<T> Send for AttrTest2<T> {}
120 unsafe impl<T> Send for AttrTest3<T> {}
121
122 // Multiple non-overlapping `Send` for a single type
123 pub struct Complex<A, B> {
124     field1: A,
125     field2: B,
126 }
127
128 unsafe impl<P> Send for Complex<P, u32> {}
129
130 // `MutexGuard` is non-Send
131 unsafe impl<Q: Send> Send for Complex<Q, MutexGuard<'static, bool>> {}
132
133 fn main() {}