mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 07:04:18 +00:00
Factor out check_closure function
Also get the receiver T in `T.method(|| f())`.
This commit is contained in:
parent
32dc02a6ec
commit
e53182a2b4
1 changed files with 162 additions and 152 deletions
|
@ -9,8 +9,8 @@ use rustc_hir::{BindingMode, Expr, ExprKind, FnRetTy, Param, PatKind, QPath, Saf
|
|||
use rustc_infer::infer::TyCtxtInferExt;
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_middle::ty::{
|
||||
self, Binder, ClosureArgs, ClosureKind, FnSig, GenericArg, GenericArgKind, List, Region, RegionKind, Ty, TyCtxt,
|
||||
TypeVisitableExt, TypeckResults,
|
||||
self, Binder, ClosureKind, FnSig, GenericArg, GenericArgKind, List, Region, RegionKind, Ty, TypeVisitableExt,
|
||||
TypeckResults,
|
||||
};
|
||||
use rustc_session::declare_lint_pass;
|
||||
use rustc_span::symbol::sym;
|
||||
|
@ -74,8 +74,23 @@ declare_clippy_lint! {
|
|||
declare_lint_pass!(EtaReduction => [REDUNDANT_CLOSURE, REDUNDANT_CLOSURE_FOR_METHOD_CALLS]);
|
||||
|
||||
impl<'tcx> LateLintPass<'tcx> for EtaReduction {
|
||||
#[allow(clippy::too_many_lines)]
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
|
||||
if let ExprKind::MethodCall(_method, receiver, args, _) = expr.kind {
|
||||
for arg in args {
|
||||
check_clousure(cx, Some(receiver), arg);
|
||||
}
|
||||
}
|
||||
if let ExprKind::Call(func, args) = expr.kind {
|
||||
check_clousure(cx, None, func);
|
||||
for arg in args {
|
||||
check_clousure(cx, None, arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_lines)]
|
||||
fn check_clousure<'tcx>(cx: &LateContext<'tcx>, outer_receiver: Option<&Expr<'tcx>>, expr: &Expr<'tcx>) {
|
||||
let body = if let ExprKind::Closure(c) = expr.kind
|
||||
&& c.fn_decl.inputs.iter().all(|ty| matches!(ty.kind, TyKind::Infer))
|
||||
&& matches!(c.fn_decl.output, FnRetTy::DefaultReturn(_))
|
||||
|
@ -105,17 +120,17 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction {
|
|||
return;
|
||||
}
|
||||
|
||||
if is_adjusted(cx, body.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
let typeck = cx.typeck_results();
|
||||
let closure = if let ty::Closure(_, closure_subs) = typeck.expr_ty(expr).kind() {
|
||||
closure_subs.as_closure()
|
||||
} else {
|
||||
return;
|
||||
};
|
||||
|
||||
if is_adjusted(cx, body.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
let closure_sig = cx.tcx.signature_unclosure(closure.sig(), Safety::Safe).skip_binder();
|
||||
match body.value.kind {
|
||||
ExprKind::Call(callee, args)
|
||||
if matches!(
|
||||
|
@ -161,7 +176,7 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction {
|
|||
}
|
||||
},
|
||||
};
|
||||
if check_sig(cx, closure, sig)
|
||||
if check_sig(closure_sig, sig)
|
||||
&& let generic_args = typeck.node_args(callee.hir_id)
|
||||
// Given some trait fn `fn f() -> ()` and some type `T: Trait`, `T::f` is not
|
||||
// `'static` unless `T: 'static`. The cast `T::f as fn()` will, however, result
|
||||
|
@ -204,7 +219,7 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction {
|
|||
ExprKind::MethodCall(path, self_, args, _) if check_inputs(typeck, body.params, Some(self_), args) => {
|
||||
if let Some(method_def_id) = typeck.type_dependent_def_id(body.value.hir_id)
|
||||
&& !cx.tcx.has_attr(method_def_id, sym::track_caller)
|
||||
&& check_sig(cx, closure, cx.tcx.fn_sig(method_def_id).skip_binder().skip_binder())
|
||||
&& check_sig(closure_sig, cx.tcx.fn_sig(method_def_id).skip_binder().skip_binder())
|
||||
{
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
|
@ -227,7 +242,6 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction {
|
|||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_inputs(
|
||||
|
@ -251,12 +265,8 @@ fn check_inputs(
|
|||
})
|
||||
}
|
||||
|
||||
fn check_sig<'tcx>(cx: &LateContext<'tcx>, closure: ClosureArgs<TyCtxt<'tcx>>, call_sig: FnSig<'_>) -> bool {
|
||||
call_sig.safety == Safety::Safe
|
||||
&& !has_late_bound_to_non_late_bound_regions(
|
||||
cx.tcx.signature_unclosure(closure.sig(), Safety::Safe).skip_binder(),
|
||||
call_sig,
|
||||
)
|
||||
fn check_sig<'tcx>(closure_sig: FnSig<'tcx>, call_sig: FnSig<'tcx>) -> bool {
|
||||
call_sig.safety == Safety::Safe && !has_late_bound_to_non_late_bound_regions(closure_sig, call_sig)
|
||||
}
|
||||
|
||||
/// This walks through both signatures and checks for any time a late-bound region is expected by an
|
||||
|
|
Loading…
Reference in a new issue