Clippy lints / Nursery
Clippy lint · Nursery · checked with Clippy 0.1.99
clippy::non_send_fields_in_send_ty: This lint warns about a Send implementation for a type that contains fields that are not safe to be sent across threads. It tries to detect fields that can cause a soundness issue when sent to another thread (e.g., Rc) while allowing !Send fields that are expected to exist in a Send type, such as raw pointers
What it meansThis lint warns about a Send implementation for a type that contains fields that are not safe to be sent across threads. It tries to detect fields that can cause a soundness issue when sent to another thread (e.g., Rc) while allowing !Send fields that are expected to exist in a Send type, such as raw pointers. It is in the nursery group and off by default.
Does it matter?
Sending the struct to another thread effectively sends all of its fields, and the fields that do not implement Send can lead to soundness bugs such as data races when accessed in a thread that is different from the thread that created it. See: The Rustonomicon about Send and Sync The documentation of Send
New lints still being finished. Off by default and may have false positives.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
struct ExampleStruct<T> {
rc_is_not_send: Rc<String>,
unbounded_generic_field: T,
}
// This impl is unsound because it allows sending `!Send` types through `ExampleStruct`
unsafe impl<T> Send for ExampleStruct<T> {}
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
non_send_fields_in_send_ty = "warn"In code
#![warn(clippy::non_send_fields_in_send_ty)]One run
cargo clippy -- -W clippy::non-send-fields-in-send-tyThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Settings in clippy.toml
enable-raw-pointer-heuristic-for-send: Whether to apply the raw pointer heuristic to determine if a type is Send.
Clippy's own documentation
What it does
This lint warns about a Send implementation for a type that
contains fields that are not safe to be sent across threads.
It tries to detect fields that can cause a soundness issue
when sent to another thread (e.g., Rc) while allowing !Send fields
that are expected to exist in a Send type, such as raw pointers.
Why is this bad?
Sending the struct to another thread effectively sends all of its fields,
and the fields that do not implement Send can lead to soundness bugs
such as data races when accessed in a thread
that is different from the thread that created it.
See:
Known Problems
This lint relies on heuristics to distinguish types that are actually
unsafe to be sent across threads and !Send types that are expected to
exist in Send type. Its rule can filter out basic cases such as
Vec<*const T>, but it’s not perfect. Feel free to create an issue if
you have a suggestion on how this heuristic can be improved.
Example
struct ExampleStruct<T> {
rc_is_not_send: Rc<String>,
unbounded_generic_field: T,
}
// This impl is unsound because it allows sending `!Send` types through `ExampleStruct`
unsafe impl<T> Send for ExampleStruct<T> {}
Use thread-safe types like std::sync::Arc
or specify correct bounds on generic type parameters (T: Send).
Configuration
-
enable-raw-pointer-heuristic-for-send: Whether to apply the raw pointer heuristic to determine if a type isSend.(default:
true)
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further use
- needless_collectChecks for functions collecting an iterator when collect is not needed
- needless_pass_by_ref_mutthis parameter is a mutable reference but is not used mutably
- needless_type_castChecks for bindings (constants, statics, or let bindings) that are defined with one numeric type but are consistently cast to a different type in all usages
- option_if_let_elseLints usage of if let Some(v) =
- or_fun_callChecks for calls to .or(foo(..)), .unwrap_or(foo(..)), .or_insert(foo(..)) etc., and suggests to use .or_else(|| foo(..)), .unwrap_or_else(|| foo(..)), .unwrap_or_default() or .or_default() etc
- path_buf_push_overwritecalling `push` with '/' or '\' (file system root) will overwrite the previous path definition
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further useRead it next →Did this answer your question?