Auto merge of #7709 - Qwaz:drop_non_send, r=xFrednet

Implement `non_send_field_in_send_ty` lint

changelog: Implement [`non_send_fields_in_send_ty`] lint

Fixes #7703
This commit is contained in:
bors
2021-10-03 08:43:32 +00:00
13 changed files with 682 additions and 1 deletions
@@ -0,0 +1 @@
enable-raw-pointer-heuristic-for-send = false
@@ -0,0 +1,43 @@
#![warn(clippy::non_send_fields_in_send_ty)]
#![feature(extern_types)]
use std::rc::Rc;
// Basic tests should not be affected
pub struct NoGeneric {
rc_is_not_send: Rc<String>,
}
unsafe impl Send for NoGeneric {}
pub struct MultiField<T> {
field1: T,
field2: T,
field3: T,
}
unsafe impl<T> Send for MultiField<T> {}
pub enum MyOption<T> {
MySome(T),
MyNone,
}
unsafe impl<T> Send for MyOption<T> {}
// All fields are disallowed when raw pointer heuristic is off
extern "C" {
type NonSend;
}
pub struct HeuristicTest {
field1: Vec<*const NonSend>,
field2: [*const NonSend; 3],
field3: (*const NonSend, *const NonSend, *const NonSend),
field4: (*const NonSend, Rc<u8>),
field5: Vec<Vec<*const NonSend>>,
}
unsafe impl Send for HeuristicTest {}
fn main() {}
@@ -0,0 +1,91 @@
error: this implementation is unsound, as some fields in `NoGeneric` are `!Send`
--> $DIR/test.rs:11:1
|
LL | unsafe impl Send for NoGeneric {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::non-send-fields-in-send-ty` implied by `-D warnings`
note: the type of field `rc_is_not_send` is `!Send`
--> $DIR/test.rs:8:5
|
LL | rc_is_not_send: Rc<String>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: use a thread-safe type that implements `Send`
error: this implementation is unsound, as some fields in `MultiField<T>` are `!Send`
--> $DIR/test.rs:19:1
|
LL | unsafe impl<T> Send for MultiField<T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the type of field `field1` is `!Send`
--> $DIR/test.rs:14:5
|
LL | field1: T,
| ^^^^^^^^^
= help: add `T: Send` bound in `Send` impl
note: the type of field `field2` is `!Send`
--> $DIR/test.rs:15:5
|
LL | field2: T,
| ^^^^^^^^^
= help: add `T: Send` bound in `Send` impl
note: the type of field `field3` is `!Send`
--> $DIR/test.rs:16:5
|
LL | field3: T,
| ^^^^^^^^^
= help: add `T: Send` bound in `Send` impl
error: this implementation is unsound, as some fields in `MyOption<T>` are `!Send`
--> $DIR/test.rs:26:1
|
LL | unsafe impl<T> Send for MyOption<T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the type of field `0` is `!Send`
--> $DIR/test.rs:22:12
|
LL | MySome(T),
| ^
= help: add `T: Send` bound in `Send` impl
error: this implementation is unsound, as some fields in `HeuristicTest` are `!Send`
--> $DIR/test.rs:41:1
|
LL | unsafe impl Send for HeuristicTest {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the type of field `field1` is `!Send`
--> $DIR/test.rs:34:5
|
LL | field1: Vec<*const NonSend>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: use a thread-safe type that implements `Send`
note: the type of field `field2` is `!Send`
--> $DIR/test.rs:35:5
|
LL | field2: [*const NonSend; 3],
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: use a thread-safe type that implements `Send`
note: the type of field `field3` is `!Send`
--> $DIR/test.rs:36:5
|
LL | field3: (*const NonSend, *const NonSend, *const NonSend),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: use a thread-safe type that implements `Send`
note: the type of field `field4` is `!Send`
--> $DIR/test.rs:37:5
|
LL | field4: (*const NonSend, Rc<u8>),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: use a thread-safe type that implements `Send`
note: the type of field `field5` is `!Send`
--> $DIR/test.rs:38:5
|
LL | field5: Vec<Vec<*const NonSend>>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: use a thread-safe type that implements `Send`
error: aborting due to 4 previous errors
@@ -1,4 +1,4 @@
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `avoid-breaking-exported-api`, `msrv`, `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `pass-by-value-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-trait-bounds`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `disallowed-methods`, `disallowed-types`, `unreadable-literal-lint-fractions`, `upper-case-acronyms-aggressive`, `cargo-ignore-publish`, `standard-macro-braces`, `enforced-import-renames`, `allowed-scripts`, `third-party` at line 5 column 1
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `avoid-breaking-exported-api`, `msrv`, `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `pass-by-value-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `vec-box-size-threshold`, `max-trait-bounds`, `max-struct-bools`, `max-fn-params-bools`, `warn-on-all-wildcard-imports`, `disallowed-methods`, `disallowed-types`, `unreadable-literal-lint-fractions`, `upper-case-acronyms-aggressive`, `cargo-ignore-publish`, `standard-macro-braces`, `enforced-import-renames`, `allowed-scripts`, `enable-raw-pointer-heuristic-for-send`, `third-party` at line 5 column 1
error: aborting due to previous error
+127
View File
@@ -0,0 +1,127 @@
#![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<T> {
data: Vec<UnsafeCell<T>>,
capacity: usize,
mask: usize,
}
unsafe impl<T> Send for RingBuffer<T> {}
// noise_search / RUSTSEC-2020-0141
pub struct MvccRwLock<T> {
raw: *const T,
lock: Mutex<Box<T>>,
}
unsafe impl<T> Send for MvccRwLock<T> {}
// async-coap / RUSTSEC-2020-0124
pub struct ArcGuard<RC, T> {
inner: T,
head: Arc<RC>,
}
unsafe impl<RC, T: Send> Send for ArcGuard<RC, T> {}
// rusb / RUSTSEC-2020-0098
extern "C" {
type libusb_device_handle;
}
pub trait UsbContext {
// some user trait that does not guarantee `Send`
}
pub struct DeviceHandle<T: UsbContext> {
context: T,
handle: NonNull<libusb_device_handle>,
}
unsafe impl<T: UsbContext> Send for DeviceHandle<T> {}
// Other basic tests
pub struct NoGeneric {
rc_is_not_send: Rc<String>,
}
unsafe impl Send for NoGeneric {}
pub struct MultiField<T> {
field1: T,
field2: T,
field3: T,
}
unsafe impl<T> Send for MultiField<T> {}
pub enum MyOption<T> {
MySome(T),
MyNone,
}
unsafe impl<T> Send for MyOption<T> {}
// Multiple type parameters
pub struct MultiParam<A, B> {
vec: Vec<(A, B)>,
}
unsafe impl<A, B> Send for MultiParam<A, B> {}
// 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<u8>),
// nested raw pointer is also allowed
field5: Vec<Vec<*const NonSend>>,
}
unsafe impl Send for HeuristicTest {}
// Test attributes
#[allow(clippy::non_send_fields_in_send_ty)]
pub struct AttrTest1<T>(T);
pub struct AttrTest2<T> {
#[allow(clippy::non_send_fields_in_send_ty)]
field: T,
}
pub enum AttrTest3<T> {
#[allow(clippy::non_send_fields_in_send_ty)]
Enum1(T),
Enum2(T),
}
unsafe impl<T> Send for AttrTest1<T> {}
unsafe impl<T> Send for AttrTest2<T> {}
unsafe impl<T> Send for AttrTest3<T> {}
// Multiple non-overlapping `Send` for a single type
pub struct Complex<A, B> {
field1: A,
field2: B,
}
unsafe impl<P> Send for Complex<P, u32> {}
// `MutexGuard` is non-Send
unsafe impl<Q: Send> Send for Complex<Q, MutexGuard<'static, bool>> {}
fn main() {}
+171
View File
@@ -0,0 +1,171 @@
error: this implementation is unsound, as some fields in `RingBuffer<T>` are `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:16:1
|
LL | unsafe impl<T> Send for RingBuffer<T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::non-send-fields-in-send-ty` implied by `-D warnings`
note: the type of field `data` is `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:11:5
|
LL | data: Vec<UnsafeCell<T>>,
| ^^^^^^^^^^^^^^^^^^^^^^^^
= help: add bounds on type parameter `T` that satisfy `Vec<UnsafeCell<T>>: Send`
error: this implementation is unsound, as some fields in `MvccRwLock<T>` are `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:24:1
|
LL | unsafe impl<T> Send for MvccRwLock<T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the type of field `lock` is `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:21:5
|
LL | lock: Mutex<Box<T>>,
| ^^^^^^^^^^^^^^^^^^^
= help: add bounds on type parameter `T` that satisfy `Mutex<Box<T>>: Send`
error: this implementation is unsound, as some fields in `ArcGuard<RC, T>` are `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:32:1
|
LL | unsafe impl<RC, T: Send> Send for ArcGuard<RC, T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the type of field `head` is `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:29:5
|
LL | head: Arc<RC>,
| ^^^^^^^^^^^^^
= help: add bounds on type parameter `RC` that satisfy `Arc<RC>: Send`
error: this implementation is unsound, as some fields in `DeviceHandle<T>` are `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:48:1
|
LL | unsafe impl<T: UsbContext> Send for DeviceHandle<T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the type of field `context` is `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:44:5
|
LL | context: T,
| ^^^^^^^^^^
= help: add `T: Send` bound in `Send` impl
error: this implementation is unsound, as some fields in `NoGeneric` are `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:55:1
|
LL | unsafe impl Send for NoGeneric {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the type of field `rc_is_not_send` is `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:52:5
|
LL | rc_is_not_send: Rc<String>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: use a thread-safe type that implements `Send`
error: this implementation is unsound, as some fields in `MultiField<T>` are `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:63:1
|
LL | unsafe impl<T> Send for MultiField<T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the type of field `field1` is `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:58:5
|
LL | field1: T,
| ^^^^^^^^^
= help: add `T: Send` bound in `Send` impl
note: the type of field `field2` is `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:59:5
|
LL | field2: T,
| ^^^^^^^^^
= help: add `T: Send` bound in `Send` impl
note: the type of field `field3` is `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:60:5
|
LL | field3: T,
| ^^^^^^^^^
= help: add `T: Send` bound in `Send` impl
error: this implementation is unsound, as some fields in `MyOption<T>` are `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:70:1
|
LL | unsafe impl<T> Send for MyOption<T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the type of field `0` is `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:66:12
|
LL | MySome(T),
| ^
= help: add `T: Send` bound in `Send` impl
error: this implementation is unsound, as some fields in `MultiParam<A, B>` are `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:77:1
|
LL | unsafe impl<A, B> Send for MultiParam<A, B> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the type of field `vec` is `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:74:5
|
LL | vec: Vec<(A, B)>,
| ^^^^^^^^^^^^^^^^
= help: add bounds on type parameters `A, B` that satisfy `Vec<(A, B)>: Send`
error: this implementation is unsound, as some fields in `HeuristicTest` are `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:95:1
|
LL | unsafe impl Send for HeuristicTest {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the type of field `field4` is `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:90:5
|
LL | field4: (*const NonSend, Rc<u8>),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= help: use a thread-safe type that implements `Send`
error: this implementation is unsound, as some fields in `AttrTest3<T>` are `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:114:1
|
LL | unsafe impl<T> Send for AttrTest3<T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the type of field `0` is `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:109:11
|
LL | Enum2(T),
| ^
= help: add `T: Send` bound in `Send` impl
error: this implementation is unsound, as some fields in `Complex<P, u32>` are `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:122:1
|
LL | unsafe impl<P> Send for Complex<P, u32> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the type of field `field1` is `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:118:5
|
LL | field1: A,
| ^^^^^^^^^
= help: add `P: Send` bound in `Send` impl
error: this implementation is unsound, as some fields in `Complex<Q, MutexGuard<'static, bool>>` are `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:125:1
|
LL | unsafe impl<Q: Send> Send for Complex<Q, MutexGuard<'static, bool>> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the type of field `field2` is `!Send`
--> $DIR/non_send_fields_in_send_ty.rs:119:5
|
LL | field2: B,
| ^^^^^^^^^
= help: use a thread-safe type that implements `Send`
error: aborting due to 12 previous errors