Skip to content

Commit cc985ec

Browse files
committed
Clarify the purpose of the lint
1 parent 3cd151d commit cc985ec

File tree

3 files changed

+57
-53
lines changed

3 files changed

+57
-53
lines changed

clippy_lints/src/non_send_fields_in_send_ty.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,28 @@ use rustc_span::sym;
1212

1313
declare_clippy_lint! {
1414
/// ### What it does
15-
/// Warns about fields in struct implementing `Send` that are neither `Send` nor `Copy`.
15+
/// This lint warns about a `Send` implementation for a type that
16+
/// contains fields that are not safe to be sent across threads.
17+
/// It tries to detect fields that can cause a soundness issue
18+
/// when sent to another thread (e.g., `Rc`) while allowing `!Send` fields
19+
/// that are expected to exist in a `Send` type such as raw pointers.
1620
///
1721
/// ### Why is this bad?
18-
/// Sending the struct to another thread will transfer the ownership to
19-
/// the new thread by dropping in the current thread during the transfer.
20-
/// This causes soundness issues for non-`Send` fields, as they are also
21-
/// dropped and might not be set up to handle this.
22+
/// Sending the struct to another thread effectively sends all of its fields,
23+
/// and the fields that do not implement `Send` can lead to soundness bugs
24+
/// such as data races when accessed in a thread
25+
/// that is different from the thread that created it.
2226
///
2327
/// See:
2428
/// * [*The Rustonomicon* about *Send and Sync*](https://doc.rust-lang.org/nomicon/send-and-sync.html)
2529
/// * [The documentation of `Send`](https://doc.rust-lang.org/std/marker/trait.Send.html)
2630
///
2731
/// ### Known Problems
28-
/// Data structures that contain raw pointers may cause false positives.
29-
/// They are sometimes safe to be sent across threads but do not implement
30-
/// the `Send` trait. This lint has a heuristic to filter out basic cases
31-
/// such as `Vec<*const T>`, but it's not perfect. Feel free to create an
32-
/// issue if you have a suggestion on how this heuristic can be improved.
32+
/// This lint relies on heuristics to distinguish types that are actually
33+
/// unsafe to be sent across threads and `!Send` types that are expected to
34+
/// exist in `Send` type. Its rule can filter out basic cases such as
35+
/// `Vec<*const T>`, but it's not perfect. Feel free to create an issue if
36+
/// you have a suggestion on how this heuristic can be improved.
3337
///
3438
/// ### Example
3539
/// ```rust,ignore
@@ -46,7 +50,7 @@ declare_clippy_lint! {
4650
#[clippy::version = "1.57.0"]
4751
pub NON_SEND_FIELDS_IN_SEND_TY,
4852
suspicious,
49-
"there is field that does not implement `Send` in a `Send` struct"
53+
"there is a field that is not safe to be sent to another thread in a `Send` struct"
5054
}
5155

5256
#[derive(Copy, Clone)]
@@ -118,14 +122,14 @@ impl<'tcx> LateLintPass<'tcx> for NonSendFieldInSendTy {
118122
NON_SEND_FIELDS_IN_SEND_TY,
119123
item.span,
120124
&format!(
121-
"this implementation is unsound, as some fields in `{}` are `!Send`",
125+
"there are some fields in `{}` are not safe to be sent to another thread",
122126
snippet(cx, hir_impl.self_ty.span, "Unknown")
123127
),
124128
|diag| {
125129
for field in non_send_fields {
126130
diag.span_note(
127131
field.def.span,
128-
&format!("the type of field `{}` is `!Send`", field.def.ident.name),
132+
&format!("it is not safe to send field `{}` to another thread", field.def.ident.name),
129133
);
130134

131135
match field.generic_params.len() {

tests/ui-toml/strict_non_send_fields_in_send_ty/test.stderr

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,86 @@
1-
error: this implementation is unsound, as some fields in `NoGeneric` are `!Send`
1+
error: there are some fields in `NoGeneric` are not safe to be sent to another thread
22
--> $DIR/test.rs:11:1
33
|
44
LL | unsafe impl Send for NoGeneric {}
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::non-send-fields-in-send-ty` implied by `-D warnings`
8-
note: the type of field `rc_is_not_send` is `!Send`
8+
note: it is not safe to send field `rc_is_not_send` to another thread
99
--> $DIR/test.rs:8:5
1010
|
1111
LL | rc_is_not_send: Rc<String>,
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1313
= help: use a thread-safe type that implements `Send`
1414

15-
error: this implementation is unsound, as some fields in `MultiField<T>` are `!Send`
15+
error: there are some fields in `MultiField<T>` are not safe to be sent to another thread
1616
--> $DIR/test.rs:19:1
1717
|
1818
LL | unsafe impl<T> Send for MultiField<T> {}
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2020
|
21-
note: the type of field `field1` is `!Send`
21+
note: it is not safe to send field `field1` to another thread
2222
--> $DIR/test.rs:14:5
2323
|
2424
LL | field1: T,
2525
| ^^^^^^^^^
2626
= help: add `T: Send` bound in `Send` impl
27-
note: the type of field `field2` is `!Send`
27+
note: it is not safe to send field `field2` to another thread
2828
--> $DIR/test.rs:15:5
2929
|
3030
LL | field2: T,
3131
| ^^^^^^^^^
3232
= help: add `T: Send` bound in `Send` impl
33-
note: the type of field `field3` is `!Send`
33+
note: it is not safe to send field `field3` to another thread
3434
--> $DIR/test.rs:16:5
3535
|
3636
LL | field3: T,
3737
| ^^^^^^^^^
3838
= help: add `T: Send` bound in `Send` impl
3939

40-
error: this implementation is unsound, as some fields in `MyOption<T>` are `!Send`
40+
error: there are some fields in `MyOption<T>` are not safe to be sent to another thread
4141
--> $DIR/test.rs:26:1
4242
|
4343
LL | unsafe impl<T> Send for MyOption<T> {}
4444
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4545
|
46-
note: the type of field `0` is `!Send`
46+
note: it is not safe to send field `0` to another thread
4747
--> $DIR/test.rs:22:12
4848
|
4949
LL | MySome(T),
5050
| ^
5151
= help: add `T: Send` bound in `Send` impl
5252

53-
error: this implementation is unsound, as some fields in `HeuristicTest` are `!Send`
53+
error: there are some fields in `HeuristicTest` are not safe to be sent to another thread
5454
--> $DIR/test.rs:41:1
5555
|
5656
LL | unsafe impl Send for HeuristicTest {}
5757
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5858
|
59-
note: the type of field `field1` is `!Send`
59+
note: it is not safe to send field `field1` to another thread
6060
--> $DIR/test.rs:34:5
6161
|
6262
LL | field1: Vec<*const NonSend>,
6363
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
6464
= help: use a thread-safe type that implements `Send`
65-
note: the type of field `field2` is `!Send`
65+
note: it is not safe to send field `field2` to another thread
6666
--> $DIR/test.rs:35:5
6767
|
6868
LL | field2: [*const NonSend; 3],
6969
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
7070
= help: use a thread-safe type that implements `Send`
71-
note: the type of field `field3` is `!Send`
71+
note: it is not safe to send field `field3` to another thread
7272
--> $DIR/test.rs:36:5
7373
|
7474
LL | field3: (*const NonSend, *const NonSend, *const NonSend),
7575
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7676
= help: use a thread-safe type that implements `Send`
77-
note: the type of field `field4` is `!Send`
77+
note: it is not safe to send field `field4` to another thread
7878
--> $DIR/test.rs:37:5
7979
|
8080
LL | field4: (*const NonSend, Rc<u8>),
8181
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8282
= help: use a thread-safe type that implements `Send`
83-
note: the type of field `field5` is `!Send`
83+
note: it is not safe to send field `field5` to another thread
8484
--> $DIR/test.rs:38:5
8585
|
8686
LL | field5: Vec<Vec<*const NonSend>>,

tests/ui/non_send_fields_in_send_ty.stderr

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,166 +1,166 @@
1-
error: this implementation is unsound, as some fields in `RingBuffer<T>` are `!Send`
1+
error: there are some fields in `RingBuffer<T>` are not safe to be sent to another thread
22
--> $DIR/non_send_fields_in_send_ty.rs:16:1
33
|
44
LL | unsafe impl<T> Send for RingBuffer<T> {}
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::non-send-fields-in-send-ty` implied by `-D warnings`
8-
note: the type of field `data` is `!Send`
8+
note: it is not safe to send field `data` to another thread
99
--> $DIR/non_send_fields_in_send_ty.rs:11:5
1010
|
1111
LL | data: Vec<UnsafeCell<T>>,
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^
1313
= help: add bounds on type parameter `T` that satisfy `Vec<UnsafeCell<T>>: Send`
1414

15-
error: this implementation is unsound, as some fields in `MvccRwLock<T>` are `!Send`
15+
error: there are some fields in `MvccRwLock<T>` are not safe to be sent to another thread
1616
--> $DIR/non_send_fields_in_send_ty.rs:24:1
1717
|
1818
LL | unsafe impl<T> Send for MvccRwLock<T> {}
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2020
|
21-
note: the type of field `lock` is `!Send`
21+
note: it is not safe to send field `lock` to another thread
2222
--> $DIR/non_send_fields_in_send_ty.rs:21:5
2323
|
2424
LL | lock: Mutex<Box<T>>,
2525
| ^^^^^^^^^^^^^^^^^^^
2626
= help: add bounds on type parameter `T` that satisfy `Mutex<Box<T>>: Send`
2727

28-
error: this implementation is unsound, as some fields in `ArcGuard<RC, T>` are `!Send`
28+
error: there are some fields in `ArcGuard<RC, T>` are not safe to be sent to another thread
2929
--> $DIR/non_send_fields_in_send_ty.rs:32:1
3030
|
3131
LL | unsafe impl<RC, T: Send> Send for ArcGuard<RC, T> {}
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3333
|
34-
note: the type of field `head` is `!Send`
34+
note: it is not safe to send field `head` to another thread
3535
--> $DIR/non_send_fields_in_send_ty.rs:29:5
3636
|
3737
LL | head: Arc<RC>,
3838
| ^^^^^^^^^^^^^
3939
= help: add bounds on type parameter `RC` that satisfy `Arc<RC>: Send`
4040

41-
error: this implementation is unsound, as some fields in `DeviceHandle<T>` are `!Send`
41+
error: there are some fields in `DeviceHandle<T>` are not safe to be sent to another thread
4242
--> $DIR/non_send_fields_in_send_ty.rs:48:1
4343
|
4444
LL | unsafe impl<T: UsbContext> Send for DeviceHandle<T> {}
4545
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4646
|
47-
note: the type of field `context` is `!Send`
47+
note: it is not safe to send field `context` to another thread
4848
--> $DIR/non_send_fields_in_send_ty.rs:44:5
4949
|
5050
LL | context: T,
5151
| ^^^^^^^^^^
5252
= help: add `T: Send` bound in `Send` impl
5353

54-
error: this implementation is unsound, as some fields in `NoGeneric` are `!Send`
54+
error: there are some fields in `NoGeneric` are not safe to be sent to another thread
5555
--> $DIR/non_send_fields_in_send_ty.rs:55:1
5656
|
5757
LL | unsafe impl Send for NoGeneric {}
5858
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5959
|
60-
note: the type of field `rc_is_not_send` is `!Send`
60+
note: it is not safe to send field `rc_is_not_send` to another thread
6161
--> $DIR/non_send_fields_in_send_ty.rs:52:5
6262
|
6363
LL | rc_is_not_send: Rc<String>,
6464
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6565
= help: use a thread-safe type that implements `Send`
6666

67-
error: this implementation is unsound, as some fields in `MultiField<T>` are `!Send`
67+
error: there are some fields in `MultiField<T>` are not safe to be sent to another thread
6868
--> $DIR/non_send_fields_in_send_ty.rs:63:1
6969
|
7070
LL | unsafe impl<T> Send for MultiField<T> {}
7171
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7272
|
73-
note: the type of field `field1` is `!Send`
73+
note: it is not safe to send field `field1` to another thread
7474
--> $DIR/non_send_fields_in_send_ty.rs:58:5
7575
|
7676
LL | field1: T,
7777
| ^^^^^^^^^
7878
= help: add `T: Send` bound in `Send` impl
79-
note: the type of field `field2` is `!Send`
79+
note: it is not safe to send field `field2` to another thread
8080
--> $DIR/non_send_fields_in_send_ty.rs:59:5
8181
|
8282
LL | field2: T,
8383
| ^^^^^^^^^
8484
= help: add `T: Send` bound in `Send` impl
85-
note: the type of field `field3` is `!Send`
85+
note: it is not safe to send field `field3` to another thread
8686
--> $DIR/non_send_fields_in_send_ty.rs:60:5
8787
|
8888
LL | field3: T,
8989
| ^^^^^^^^^
9090
= help: add `T: Send` bound in `Send` impl
9191

92-
error: this implementation is unsound, as some fields in `MyOption<T>` are `!Send`
92+
error: there are some fields in `MyOption<T>` are not safe to be sent to another thread
9393
--> $DIR/non_send_fields_in_send_ty.rs:70:1
9494
|
9595
LL | unsafe impl<T> Send for MyOption<T> {}
9696
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9797
|
98-
note: the type of field `0` is `!Send`
98+
note: it is not safe to send field `0` to another thread
9999
--> $DIR/non_send_fields_in_send_ty.rs:66:12
100100
|
101101
LL | MySome(T),
102102
| ^
103103
= help: add `T: Send` bound in `Send` impl
104104

105-
error: this implementation is unsound, as some fields in `MultiParam<A, B>` are `!Send`
105+
error: there are some fields in `MultiParam<A, B>` are not safe to be sent to another thread
106106
--> $DIR/non_send_fields_in_send_ty.rs:77:1
107107
|
108108
LL | unsafe impl<A, B> Send for MultiParam<A, B> {}
109109
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
110110
|
111-
note: the type of field `vec` is `!Send`
111+
note: it is not safe to send field `vec` to another thread
112112
--> $DIR/non_send_fields_in_send_ty.rs:74:5
113113
|
114114
LL | vec: Vec<(A, B)>,
115115
| ^^^^^^^^^^^^^^^^
116116
= help: add bounds on type parameters `A, B` that satisfy `Vec<(A, B)>: Send`
117117

118-
error: this implementation is unsound, as some fields in `HeuristicTest` are `!Send`
118+
error: there are some fields in `HeuristicTest` are not safe to be sent to another thread
119119
--> $DIR/non_send_fields_in_send_ty.rs:95:1
120120
|
121121
LL | unsafe impl Send for HeuristicTest {}
122122
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
123123
|
124-
note: the type of field `field4` is `!Send`
124+
note: it is not safe to send field `field4` to another thread
125125
--> $DIR/non_send_fields_in_send_ty.rs:90:5
126126
|
127127
LL | field4: (*const NonSend, Rc<u8>),
128128
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
129129
= help: use a thread-safe type that implements `Send`
130130

131-
error: this implementation is unsound, as some fields in `AttrTest3<T>` are `!Send`
131+
error: there are some fields in `AttrTest3<T>` are not safe to be sent to another thread
132132
--> $DIR/non_send_fields_in_send_ty.rs:114:1
133133
|
134134
LL | unsafe impl<T> Send for AttrTest3<T> {}
135135
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
136136
|
137-
note: the type of field `0` is `!Send`
137+
note: it is not safe to send field `0` to another thread
138138
--> $DIR/non_send_fields_in_send_ty.rs:109:11
139139
|
140140
LL | Enum2(T),
141141
| ^
142142
= help: add `T: Send` bound in `Send` impl
143143

144-
error: this implementation is unsound, as some fields in `Complex<P, u32>` are `!Send`
144+
error: there are some fields in `Complex<P, u32>` are not safe to be sent to another thread
145145
--> $DIR/non_send_fields_in_send_ty.rs:122:1
146146
|
147147
LL | unsafe impl<P> Send for Complex<P, u32> {}
148148
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
149149
|
150-
note: the type of field `field1` is `!Send`
150+
note: it is not safe to send field `field1` to another thread
151151
--> $DIR/non_send_fields_in_send_ty.rs:118:5
152152
|
153153
LL | field1: A,
154154
| ^^^^^^^^^
155155
= help: add `P: Send` bound in `Send` impl
156156

157-
error: this implementation is unsound, as some fields in `Complex<Q, MutexGuard<'static, bool>>` are `!Send`
157+
error: there are some fields in `Complex<Q, MutexGuard<'static, bool>>` are not safe to be sent to another thread
158158
--> $DIR/non_send_fields_in_send_ty.rs:125:1
159159
|
160160
LL | unsafe impl<Q: Send> Send for Complex<Q, MutexGuard<'static, bool>> {}
161161
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
162162
|
163-
note: the type of field `field2` is `!Send`
163+
note: it is not safe to send field `field2` to another thread
164164
--> $DIR/non_send_fields_in_send_ty.rs:119:5
165165
|
166166
LL | field2: B,

0 commit comments

Comments
 (0)