use clippy_utils::source::snippet_opt;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::usage::local_used_after_expr;
-use clippy_utils::{get_enclosing_loop_or_closure, higher, path_to_local, path_to_local_id};
+use clippy_utils::{higher, is_adjusted, path_to_local, path_to_local_id};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::def_id::DefId;
-use rustc_hir::{Expr, ExprKind, Param, PatKind, Unsafety};
+use rustc_hir::{Closure, Expr, ExprKind, Param, PatKind, Unsafety};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
+use rustc_middle::ty::binding::BindingMode;
use rustc_middle::ty::subst::Subst;
-use rustc_middle::ty::{self, ClosureKind, Ty, TypeFoldable};
+use rustc_middle::ty::{self, ClosureKind, Ty, TypeVisitable};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::symbol::sym;
///
/// ### Example
/// ```rust,ignore
- /// // Bad
/// xs.map(|x| foo(x))
+ /// ```
///
- /// // Good
+ /// Use instead:
+ /// ```rust,ignore
+ /// // where `foo(_)` is a plain function that takes the exact argument type of `x`.
/// xs.map(foo)
/// ```
- /// where `foo(_)` is a plain function that takes the exact argument type of
- /// `x`.
#[clippy::version = "pre 1.29.0"]
pub REDUNDANT_CLOSURE,
style,
return;
}
let body = match expr.kind {
- ExprKind::Closure(_, _, id, _, _) => cx.tcx.hir().body(id),
+ ExprKind::Closure(&Closure { body, .. }) => cx.tcx.hir().body(body),
_ => return,
};
if body.value.span.from_expansion() {
let closure_ty = cx.typeck_results().expr_ty(expr);
if_chain!(
+ if !is_adjusted(cx, &body.value);
if let ExprKind::Call(callee, args) = body.value.kind;
if let ExprKind::Path(_) = callee.kind;
if check_inputs(cx, body.params, args);
if_chain! {
if let ty::Closure(_, substs) = callee_ty.peel_refs().kind();
if substs.as_closure().kind() == ClosureKind::FnMut;
- if get_enclosing_loop_or_closure(cx.tcx, expr).is_some()
- || path_to_local(callee).map_or(false, |l| local_used_after_expr(cx, l, callee));
+ if path_to_local(callee).map_or(false, |l| local_used_after_expr(cx, l, expr));
then {
// Mutable closure is used after current expr; we cannot consume it.
);
if_chain!(
- if let ExprKind::MethodCall(path, _, args, _) = body.value.kind;
+ if !is_adjusted(cx, &body.value);
+ if let ExprKind::MethodCall(path, args, _) = body.value.kind;
if check_inputs(cx, body.params, args);
let method_def_id = cx.typeck_results().type_dependent_def_id(body.value.hir_id).unwrap();
let substs = cx.typeck_results().node_substs(body.value.hir_id);
- let call_ty = cx.tcx.type_of(method_def_id).subst(cx.tcx, substs);
+ let call_ty = cx.tcx.bound_type_of(method_def_id).subst(cx.tcx, substs);
if check_sig(cx, closure_ty, call_ty);
then {
span_lint_and_then(cx, REDUNDANT_CLOSURE_FOR_METHOD_CALLS, expr.span, "redundant closure", |diag| {
if params.len() != call_args.len() {
return false;
}
+ let binding_modes = cx.typeck_results().pat_binding_modes();
std::iter::zip(params, call_args).all(|(param, arg)| {
match param.pat.kind {
PatKind::Binding(_, id, ..) if path_to_local_id(arg, id) => {},
_ => return false,
}
+ // checks that parameters are not bound as `ref` or `ref mut`
+ if let Some(BindingMode::BindByReference(_)) = binding_modes.get(param.pat.hir_id) {
+ return false;
+ }
+
match *cx.typeck_results().expr_adjustments(arg) {
[] => true,
[
ty::ImplContainer(def_id) => {
let ty = cx.tcx.type_of(def_id);
match ty.kind() {
- ty::Adt(adt, _) => cx.tcx.def_path_str(adt.did),
+ ty::Adt(adt, _) => cx.tcx.def_path_str(adt.did()),
_ => ty.to_string(),
}
},