add lint futures_not_send
This commit is contained in:
parent
3ea8e5e856
commit
d2cbbff217
6 changed files with 329 additions and 0 deletions
113
clippy_lints/src/future_not_send.rs
Normal file
113
clippy_lints/src/future_not_send.rs
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
use crate::utils;
|
||||
use rustc_hir::intravisit::FnKind;
|
||||
use rustc_hir::{Body, FnDecl, HirId};
|
||||
use rustc_infer::infer::TyCtxtInferExt;
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_middle::ty::{Opaque, Predicate::Trait, ToPolyTraitRef};
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
use rustc_span::{sym, Span};
|
||||
use rustc_trait_selection::traits::error_reporting::suggestions::InferCtxtExt;
|
||||
use rustc_trait_selection::traits::{self, FulfillmentError, TraitEngine};
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// **What it does:** This lint requires Future implementations returned from
|
||||
/// functions and methods to implement the `Send` marker trait. It is mostly
|
||||
/// used by library authors (public and internal) that target an audience where
|
||||
/// multithreaded executors are likely to be used for running these Futures.
|
||||
///
|
||||
/// **Why is this bad?** A Future implementation captures some state that it
|
||||
/// needs to eventually produce its final value. When targeting a multithreaded
|
||||
/// executor (which is the norm on non-embedded devices) this means that this
|
||||
/// state may need to be transported to other threads, in other words the
|
||||
/// whole Future needs to implement the `Send` marker trait. If it does not,
|
||||
/// then the resulting Future cannot be submitted to a thread pool in the
|
||||
/// end user’s code.
|
||||
///
|
||||
/// Especially for generic functions it can be confusing to leave the
|
||||
/// discovery of this problem to the end user: the reported error location
|
||||
/// will be far from its cause and can in many cases not even be fixed without
|
||||
/// modifying the library where the offending Future implementation is
|
||||
/// produced.
|
||||
///
|
||||
/// **Known problems:** None.
|
||||
///
|
||||
/// **Example:**
|
||||
///
|
||||
/// ```rust
|
||||
/// async fn not_send(bytes: std::rc::Rc<[u8]>) {}
|
||||
/// ```
|
||||
/// Use instead:
|
||||
/// ```rust
|
||||
/// async fn is_send(bytes: std::sync::Arc<[u8]>) {}
|
||||
/// ```
|
||||
pub FUTURE_NOT_SEND,
|
||||
nursery,
|
||||
"public Futures must be Send"
|
||||
}
|
||||
|
||||
declare_lint_pass!(FutureNotSend => [FUTURE_NOT_SEND]);
|
||||
|
||||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FutureNotSend {
|
||||
fn check_fn(
|
||||
&mut self,
|
||||
cx: &LateContext<'a, 'tcx>,
|
||||
kind: FnKind<'tcx>,
|
||||
decl: &'tcx FnDecl<'tcx>,
|
||||
_: &'tcx Body<'tcx>,
|
||||
_: Span,
|
||||
hir_id: HirId,
|
||||
) {
|
||||
if let FnKind::Closure(_) = kind {
|
||||
return;
|
||||
}
|
||||
let def_id = cx.tcx.hir().local_def_id(hir_id);
|
||||
let fn_sig = cx.tcx.fn_sig(def_id);
|
||||
let fn_sig = cx.tcx.erase_late_bound_regions(&fn_sig);
|
||||
let ret_ty = fn_sig.output();
|
||||
if let Opaque(id, subst) = ret_ty.kind {
|
||||
let preds = cx.tcx.predicates_of(id).instantiate(cx.tcx, subst);
|
||||
let mut is_future = false;
|
||||
for p in preds.predicates {
|
||||
if let Some(trait_ref) = p.to_opt_poly_trait_ref() {
|
||||
if Some(trait_ref.def_id()) == cx.tcx.lang_items().future_trait() {
|
||||
is_future = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if is_future {
|
||||
let send_trait = cx.tcx.get_diagnostic_item(sym::send_trait).unwrap();
|
||||
let span = decl.output.span();
|
||||
let send_result = cx.tcx.infer_ctxt().enter(|infcx| {
|
||||
let cause = traits::ObligationCause::misc(span, hir_id);
|
||||
let mut fulfillment_cx = traits::FulfillmentContext::new();
|
||||
fulfillment_cx.register_bound(&infcx, cx.param_env, ret_ty, send_trait, cause);
|
||||
fulfillment_cx.select_all_or_error(&infcx)
|
||||
});
|
||||
if let Err(send_errors) = send_result {
|
||||
utils::span_lint_and_then(
|
||||
cx,
|
||||
FUTURE_NOT_SEND,
|
||||
span,
|
||||
"future cannot be sent between threads safely",
|
||||
|db| {
|
||||
cx.tcx.infer_ctxt().enter(|infcx| {
|
||||
for FulfillmentError { obligation, .. } in send_errors {
|
||||
infcx.maybe_note_obligation_cause_for_async_await(db, &obligation);
|
||||
if let Trait(trait_pred, _) = obligation.predicate {
|
||||
let trait_ref = trait_pred.to_poly_trait_ref();
|
||||
db.note(&*format!(
|
||||
"`{}` doesn't implement `{}`",
|
||||
trait_ref.self_ty(),
|
||||
trait_ref.print_only_trait_path(),
|
||||
));
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -218,6 +218,7 @@ mod floating_point_arithmetic;
|
|||
mod format;
|
||||
mod formatting;
|
||||
mod functions;
|
||||
mod future_not_send;
|
||||
mod get_last_with_len;
|
||||
mod identity_conversion;
|
||||
mod identity_op;
|
||||
|
|
@ -566,6 +567,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
|||
&functions::NOT_UNSAFE_PTR_ARG_DEREF,
|
||||
&functions::TOO_MANY_ARGUMENTS,
|
||||
&functions::TOO_MANY_LINES,
|
||||
&future_not_send::FUTURE_NOT_SEND,
|
||||
&get_last_with_len::GET_LAST_WITH_LEN,
|
||||
&identity_conversion::IDENTITY_CONVERSION,
|
||||
&identity_op::IDENTITY_OP,
|
||||
|
|
@ -1045,6 +1047,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
|||
store.register_late_pass(|| box redundant_pub_crate::RedundantPubCrate::default());
|
||||
store.register_late_pass(|| box unnamed_address::UnnamedAddress);
|
||||
store.register_late_pass(|| box dereference::Dereferencing);
|
||||
store.register_late_pass(|| box future_not_send::FutureNotSend);
|
||||
|
||||
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
|
||||
LintId::of(&arithmetic::FLOAT_ARITHMETIC),
|
||||
|
|
@ -1689,6 +1692,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
|||
LintId::of(&fallible_impl_from::FALLIBLE_IMPL_FROM),
|
||||
LintId::of(&floating_point_arithmetic::IMPRECISE_FLOPS),
|
||||
LintId::of(&floating_point_arithmetic::SUBOPTIMAL_FLOPS),
|
||||
LintId::of(&future_not_send::FUTURE_NOT_SEND),
|
||||
LintId::of(&missing_const_for_fn::MISSING_CONST_FOR_FN),
|
||||
LintId::of(&mutable_debug_assertion::DEBUG_ASSERT_WITH_MUT_CALL),
|
||||
LintId::of(&mutex_atomic::MUTEX_INTEGER),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue