#![warn(clippy::non_send_fields_in_send_ty)] #![feature(extern_types)] use std::cell::UnsafeCell; use std::ptr::NonNull; use std::rc::Rc; use std::sync::{Arc, Mutex, MutexGuard}; // disrustor / RUSTSEC-2020-0150 pub struct RingBuffer { data: Vec>, capacity: usize, mask: usize, } unsafe impl Send for RingBuffer {} //~^ ERROR: some fields in `RingBuffer` are not safe to be sent to another thread // noise_search / RUSTSEC-2020-0141 pub struct MvccRwLock { raw: *const T, lock: Mutex>, } unsafe impl Send for MvccRwLock {} //~^ ERROR: some fields in `MvccRwLock` are not safe to be sent to another thread // async-coap / RUSTSEC-2020-0124 pub struct ArcGuard { inner: T, head: Arc, } unsafe impl Send for ArcGuard {} //~^ ERROR: some fields in `ArcGuard` are not safe to be sent to another thread // rusb / RUSTSEC-2020-0098 extern "C" { type libusb_device_handle; } pub trait UsbContext { // some user trait that does not guarantee `Send` } pub struct DeviceHandle { context: T, handle: NonNull, } unsafe impl Send for DeviceHandle {} //~^ ERROR: some fields in `DeviceHandle` are not safe to be sent to another thread // Other basic tests pub struct NoGeneric { rc_is_not_send: Rc, } unsafe impl Send for NoGeneric {} //~^ ERROR: some fields in `NoGeneric` are not safe to be sent to another thread pub struct MultiField { field1: T, field2: T, field3: T, } unsafe impl Send for MultiField {} //~^ ERROR: some fields in `MultiField` are not safe to be sent to another thread pub enum MyOption { MySome(T), MyNone, } unsafe impl Send for MyOption {} //~^ ERROR: some fields in `MyOption` are not safe to be sent to another thread // Test types that contain `NonNull` instead of raw pointers (#8045) pub struct WrappedNonNull(UnsafeCell>); unsafe impl Send for WrappedNonNull {} // Multiple type parameters pub struct MultiParam { vec: Vec<(A, B)>, } unsafe impl Send for MultiParam {} //~^ ERROR: some fields in `MultiParam` are not safe to be sent to another thread // Tests for raw pointer heuristic extern "C" { type NonSend; } pub struct HeuristicTest { // raw pointers are allowed field1: Vec<*const NonSend>, field2: [*const NonSend; 3], field3: (*const NonSend, *const NonSend, *const NonSend), // not allowed when it contains concrete `!Send` field field4: (*const NonSend, Rc), // nested raw pointer is also allowed field5: Vec>, } unsafe impl Send for HeuristicTest {} //~^ ERROR: some fields in `HeuristicTest` are not safe to be sent to another thread // Test attributes #[allow(clippy::non_send_fields_in_send_ty)] pub struct AttrTest1(T); pub struct AttrTest2 { #[allow(clippy::non_send_fields_in_send_ty)] field: T, } pub enum AttrTest3 { #[allow(clippy::non_send_fields_in_send_ty)] Enum1(T), Enum2(T), } unsafe impl Send for AttrTest1 {} unsafe impl Send for AttrTest2 {} unsafe impl Send for AttrTest3 {} //~^ ERROR: some fields in `AttrTest3` are not safe to be sent to another thread // Multiple non-overlapping `Send` for a single type pub struct Complex { field1: A, field2: B, } unsafe impl

Send for Complex {} //~^ ERROR: some fields in `Complex` are not safe to be sent to another thread // `MutexGuard` is non-Send unsafe impl Send for Complex> {} //~^ ERROR: some fields in `Complex>` are not safe to be sent fn main() {}