2021-09-28 14:23:39 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2021-10-01 13:00:50 -05:00
|
|
|
use clippy_utils::source::snippet;
|
2021-09-23 02:51:10 -05:00
|
|
|
use clippy_utils::ty::{implements_trait, is_copy};
|
2021-12-03 18:11:40 -06:00
|
|
|
use clippy_utils::{is_lint_allowed, match_def_path, paths};
|
2021-09-23 02:51:10 -05:00
|
|
|
use rustc_ast::ImplPolarity;
|
2021-09-28 14:23:39 -05:00
|
|
|
use rustc_hir::def_id::DefId;
|
2021-10-01 13:00:50 -05:00
|
|
|
use rustc_hir::{FieldDef, Item, ItemKind, Node};
|
2021-09-23 02:51:10 -05:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2021-12-03 18:11:40 -06:00
|
|
|
use rustc_middle::lint::in_external_macro;
|
2021-09-23 02:51:10 -05:00
|
|
|
use rustc_middle::ty::{self, subst::GenericArgKind, Ty};
|
2021-09-28 15:55:36 -05:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2021-10-01 13:00:50 -05:00
|
|
|
use rustc_span::sym;
|
2021-09-23 02:51:10 -05:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
2021-12-03 19:28:14 -06:00
|
|
|
/// 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
|
2021-12-07 09:02:58 -06:00
|
|
|
/// that are expected to exist in a `Send` type, such as raw pointers.
|
2021-09-23 02:51:10 -05:00
|
|
|
///
|
|
|
|
/// ### Why is this bad?
|
2021-12-03 19:28:14 -06:00
|
|
|
/// 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.
|
2021-09-28 12:46:13 -05:00
|
|
|
///
|
|
|
|
/// See:
|
|
|
|
/// * [*The Rustonomicon* about *Send and Sync*](https://doc.rust-lang.org/nomicon/send-and-sync.html)
|
|
|
|
/// * [The documentation of `Send`](https://doc.rust-lang.org/std/marker/trait.Send.html)
|
2021-09-23 02:51:10 -05:00
|
|
|
///
|
|
|
|
/// ### Known Problems
|
2021-12-03 19:28:14 -06:00
|
|
|
/// 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.
|
2021-09-23 02:51:10 -05:00
|
|
|
///
|
|
|
|
/// ### Example
|
2021-09-28 12:46:13 -05:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// struct ExampleStruct<T> {
|
|
|
|
/// rc_is_not_send: Rc<String>,
|
|
|
|
/// unbounded_generic_field: T,
|
2021-09-23 02:51:10 -05:00
|
|
|
/// }
|
2021-09-28 12:46:13 -05:00
|
|
|
///
|
|
|
|
/// // This impl is unsound because it allows sending `!Send` types through `ExampleStruct`
|
|
|
|
/// unsafe impl<T> Send for ExampleStruct<T> {}
|
2021-09-23 02:51:10 -05:00
|
|
|
/// ```
|
2021-09-28 12:46:13 -05:00
|
|
|
/// Use thread-safe types like [`std::sync::Arc`](https://doc.rust-lang.org/std/sync/struct.Arc.html)
|
2021-10-01 11:47:54 -05:00
|
|
|
/// or specify correct bounds on generic type parameters (`T: Send`).
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 14:06:26 -05:00
|
|
|
#[clippy::version = "1.57.0"]
|
2021-10-02 18:22:37 -05:00
|
|
|
pub NON_SEND_FIELDS_IN_SEND_TY,
|
2021-12-07 09:45:59 -06:00
|
|
|
nursery,
|
2021-12-03 19:28:14 -06:00
|
|
|
"there is a field that is not safe to be sent to another thread in a `Send` struct"
|
2021-09-23 02:51:10 -05:00
|
|
|
}
|
|
|
|
|
2021-09-28 15:55:36 -05:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct NonSendFieldInSendTy {
|
|
|
|
enable_raw_pointer_heuristic: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NonSendFieldInSendTy {
|
|
|
|
pub fn new(enable_raw_pointer_heuristic: bool) -> Self {
|
|
|
|
Self {
|
|
|
|
enable_raw_pointer_heuristic,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 18:22:37 -05:00
|
|
|
impl_lint_pass!(NonSendFieldInSendTy => [NON_SEND_FIELDS_IN_SEND_TY]);
|
2021-09-23 02:51:10 -05:00
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for NonSendFieldInSendTy {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
|
2021-09-28 15:55:36 -05:00
|
|
|
let ty_allowed_in_send = if self.enable_raw_pointer_heuristic {
|
|
|
|
ty_allowed_with_raw_pointer_heuristic
|
|
|
|
} else {
|
2021-10-01 12:07:25 -05:00
|
|
|
ty_allowed_without_raw_pointer_heuristic
|
2021-09-28 15:55:36 -05:00
|
|
|
};
|
|
|
|
|
2021-09-23 05:45:24 -05:00
|
|
|
// Checks if we are in `Send` impl item.
|
|
|
|
// We start from `Send` impl instead of `check_field_def()` because
|
|
|
|
// single `AdtDef` may have multiple `Send` impls due to generic
|
|
|
|
// parameters, and the lint is much easier to implement in this way.
|
2021-09-23 02:51:10 -05:00
|
|
|
if_chain! {
|
2021-12-03 18:11:40 -06:00
|
|
|
if !in_external_macro(cx.tcx.sess, item.span);
|
2021-10-07 04:11:23 -05:00
|
|
|
if let Some(send_trait) = cx.tcx.get_diagnostic_item(sym::Send);
|
2021-09-23 02:51:10 -05:00
|
|
|
if let ItemKind::Impl(hir_impl) = &item.kind;
|
|
|
|
if let Some(trait_ref) = &hir_impl.of_trait;
|
|
|
|
if let Some(trait_id) = trait_ref.trait_def_id();
|
|
|
|
if send_trait == trait_id;
|
2021-10-04 01:33:40 -05:00
|
|
|
if hir_impl.polarity == ImplPolarity::Positive;
|
2021-09-23 02:51:10 -05:00
|
|
|
if let Some(ty_trait_ref) = cx.tcx.impl_trait_ref(item.def_id);
|
|
|
|
if let self_ty = ty_trait_ref.self_ty();
|
|
|
|
if let ty::Adt(adt_def, impl_trait_substs) = self_ty.kind();
|
|
|
|
then {
|
2021-09-28 14:23:39 -05:00
|
|
|
let mut non_send_fields = Vec::new();
|
|
|
|
|
|
|
|
let hir_map = cx.tcx.hir();
|
2021-09-23 02:51:10 -05:00
|
|
|
for variant in &adt_def.variants {
|
|
|
|
for field in &variant.fields {
|
2021-09-28 14:23:39 -05:00
|
|
|
if_chain! {
|
|
|
|
if let Some(field_hir_id) = field
|
|
|
|
.did
|
|
|
|
.as_local()
|
|
|
|
.map(|local_def_id| hir_map.local_def_id_to_hir_id(local_def_id));
|
2021-10-02 18:22:37 -05:00
|
|
|
if !is_lint_allowed(cx, NON_SEND_FIELDS_IN_SEND_TY, field_hir_id);
|
2021-09-28 14:23:39 -05:00
|
|
|
if let field_ty = field.ty(cx.tcx, impl_trait_substs);
|
|
|
|
if !ty_allowed_in_send(cx, field_ty, send_trait);
|
2021-10-01 13:00:50 -05:00
|
|
|
if let Node::Field(field_def) = hir_map.get(field_hir_id);
|
2021-09-28 14:23:39 -05:00
|
|
|
then {
|
|
|
|
non_send_fields.push(NonSendField {
|
2021-10-01 13:00:50 -05:00
|
|
|
def: field_def,
|
2021-09-28 14:23:39 -05:00
|
|
|
ty: field_ty,
|
|
|
|
generic_params: collect_generic_params(cx, field_ty),
|
|
|
|
})
|
|
|
|
}
|
2021-09-23 02:51:10 -05:00
|
|
|
}
|
2021-09-28 14:23:39 -05:00
|
|
|
}
|
|
|
|
}
|
2021-09-23 02:51:10 -05:00
|
|
|
|
2021-09-28 14:23:39 -05:00
|
|
|
if !non_send_fields.is_empty() {
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
2021-10-02 18:22:37 -05:00
|
|
|
NON_SEND_FIELDS_IN_SEND_TY,
|
2021-09-28 14:23:39 -05:00
|
|
|
item.span,
|
|
|
|
&format!(
|
2021-12-07 09:02:58 -06:00
|
|
|
"some fields in `{}` are not safe to be sent to another thread",
|
2021-10-01 13:00:50 -05:00
|
|
|
snippet(cx, hir_impl.self_ty.span, "Unknown")
|
2021-09-28 14:23:39 -05:00
|
|
|
),
|
|
|
|
|diag| {
|
|
|
|
for field in non_send_fields {
|
|
|
|
diag.span_note(
|
2021-10-01 13:00:50 -05:00
|
|
|
field.def.span,
|
2021-12-03 19:28:14 -06:00
|
|
|
&format!("it is not safe to send field `{}` to another thread", field.def.ident.name),
|
2021-09-23 05:45:24 -05:00
|
|
|
);
|
2021-09-28 14:23:39 -05:00
|
|
|
|
|
|
|
match field.generic_params.len() {
|
|
|
|
0 => diag.help("use a thread-safe type that implements `Send`"),
|
|
|
|
1 if is_ty_param(field.ty) => diag.help(&format!("add `{}: Send` bound in `Send` impl", field.ty)),
|
|
|
|
_ => diag.help(&format!(
|
|
|
|
"add bounds on type parameter{} `{}` that satisfy `{}: Send`",
|
|
|
|
if field.generic_params.len() > 1 { "s" } else { "" },
|
|
|
|
field.generic_params_string(),
|
2021-10-01 13:00:50 -05:00
|
|
|
snippet(cx, field.def.ty.span, "Unknown"),
|
2021-09-28 14:23:39 -05:00
|
|
|
)),
|
|
|
|
};
|
2021-09-23 02:51:10 -05:00
|
|
|
}
|
2021-09-28 14:23:39 -05:00
|
|
|
},
|
2021-09-28 15:42:27 -05:00
|
|
|
);
|
2021-09-23 02:51:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 14:23:39 -05:00
|
|
|
struct NonSendField<'tcx> {
|
2021-10-01 13:00:50 -05:00
|
|
|
def: &'tcx FieldDef<'tcx>,
|
2021-09-28 14:23:39 -05:00
|
|
|
ty: Ty<'tcx>,
|
|
|
|
generic_params: Vec<Ty<'tcx>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> NonSendField<'tcx> {
|
|
|
|
fn generic_params_string(&self) -> String {
|
|
|
|
self.generic_params
|
|
|
|
.iter()
|
|
|
|
.map(ToString::to_string)
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join(", ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 15:42:27 -05:00
|
|
|
/// Given a type, collect all of its generic parameters.
|
2021-10-01 11:47:54 -05:00
|
|
|
/// Example: `MyStruct<P, Box<Q, R>>` => `vec![P, Q, R]`
|
2021-09-28 14:23:39 -05:00
|
|
|
fn collect_generic_params<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Vec<Ty<'tcx>> {
|
|
|
|
ty.walk(cx.tcx)
|
|
|
|
.filter_map(|inner| match inner.unpack() {
|
|
|
|
GenericArgKind::Type(inner_ty) => Some(inner_ty),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.filter(|&inner_ty| is_ty_param(inner_ty))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
2021-10-01 12:07:25 -05:00
|
|
|
/// Be more strict when the heuristic is disabled
|
|
|
|
fn ty_allowed_without_raw_pointer_heuristic<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, send_trait: DefId) -> bool {
|
|
|
|
if implements_trait(cx, ty, send_trait, &[]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-03 18:11:40 -06:00
|
|
|
if is_copy(cx, ty) && !contains_pointer_like(cx, ty) {
|
2021-10-01 12:07:25 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
2021-09-28 15:42:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Heuristic to allow cases like `Vec<*const u8>`
|
|
|
|
fn ty_allowed_with_raw_pointer_heuristic<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, send_trait: DefId) -> bool {
|
2021-10-01 12:07:25 -05:00
|
|
|
if implements_trait(cx, ty, send_trait, &[]) || is_copy(cx, ty) {
|
2021-10-01 11:47:54 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The type is known to be `!Send` and `!Copy`
|
|
|
|
match ty.kind() {
|
|
|
|
ty::Tuple(_) => ty
|
|
|
|
.tuple_fields()
|
|
|
|
.all(|ty| ty_allowed_with_raw_pointer_heuristic(cx, ty, send_trait)),
|
|
|
|
ty::Array(ty, _) | ty::Slice(ty) => ty_allowed_with_raw_pointer_heuristic(cx, ty, send_trait),
|
|
|
|
ty::Adt(_, substs) => {
|
2021-12-03 18:11:40 -06:00
|
|
|
if contains_pointer_like(cx, ty) {
|
2021-10-01 11:47:54 -05:00
|
|
|
// descends only if ADT contains any raw pointers
|
|
|
|
substs.iter().all(|generic_arg| match generic_arg.unpack() {
|
|
|
|
GenericArgKind::Type(ty) => ty_allowed_with_raw_pointer_heuristic(cx, ty, send_trait),
|
|
|
|
// Lifetimes and const generics are not solid part of ADT and ignored
|
|
|
|
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => true,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// Raw pointers are `!Send` but allowed by the heuristic
|
|
|
|
ty::RawPtr(_) => true,
|
|
|
|
_ => false,
|
2021-09-28 15:42:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-03 18:11:40 -06:00
|
|
|
/// Checks if the type contains any pointer-like types in substs (including nested ones)
|
|
|
|
fn contains_pointer_like<'tcx>(cx: &LateContext<'tcx>, target_ty: Ty<'tcx>) -> bool {
|
2021-09-23 02:51:10 -05:00
|
|
|
for ty_node in target_ty.walk(cx.tcx) {
|
2021-12-03 18:11:40 -06:00
|
|
|
if let GenericArgKind::Type(inner_ty) = ty_node.unpack() {
|
|
|
|
match inner_ty.kind() {
|
|
|
|
ty::RawPtr(_) => {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
ty::Adt(adt_def, _) => {
|
|
|
|
if match_def_path(cx, adt_def.did, &paths::PTR_NON_NULL) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => (),
|
2021-09-23 02:51:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the type is a type parameter such as `T`.
|
2021-09-23 05:45:24 -05:00
|
|
|
fn is_ty_param(target_ty: Ty<'_>) -> bool {
|
2021-09-23 02:51:10 -05:00
|
|
|
matches!(target_ty.kind(), ty::Param(_))
|
|
|
|
}
|