2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
|
|
|
|
use clippy_utils::higher::VecArgs;
|
|
|
|
use clippy_utils::source::snippet_opt;
|
2021-07-15 08:44:10 +00:00
|
|
|
use clippy_utils::usage::UsedAfterExprVisitor;
|
2021-09-28 17:03:12 +00:00
|
|
|
use clippy_utils::{get_enclosing_loop_or_closure, higher, path_to_local_id};
|
2019-02-10 11:58:51 +00:00
|
|
|
use if_chain::if_chain;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_errors::Applicability;
|
2021-09-28 17:03:12 +00:00
|
|
|
use rustc_hir::def_id::DefId;
|
|
|
|
use rustc_hir::{Expr, ExprKind, Param, PatKind, Unsafety};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
|
|
|
|
use rustc_middle::ty::subst::Subst;
|
|
|
|
use rustc_middle::ty::{self, ClosureKind, Ty, TypeFoldable};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2015-07-26 14:53:11 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for closures which just call another function where
|
2019-03-05 16:50:33 +00:00
|
|
|
/// the function can be called directly. `unsafe` functions or calls where types
|
|
|
|
/// get adjusted are ignored.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Needlessly creating a closure adds code for no benefit
|
2019-03-05 16:50:33 +00:00
|
|
|
/// and gives the optimizer more work.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Known problems
|
|
|
|
/// If creating the closure inside the closure has a side-
|
2019-03-05 16:50:33 +00:00
|
|
|
/// effect then moving the closure creation out will change when that side-
|
|
|
|
/// effect runs.
|
2021-01-15 09:56:44 +00:00
|
|
|
/// See [#1439](https://github.com/rust-lang/rust-clippy/issues/1439) for more details.
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2019-03-10 22:01:56 +00:00
|
|
|
/// ```rust,ignore
|
2020-06-09 14:36:01 +00:00
|
|
|
/// // Bad
|
2019-03-05 16:50:33 +00:00
|
|
|
/// xs.map(|x| foo(x))
|
2020-06-09 14:36:01 +00:00
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// xs.map(foo)
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
|
|
|
/// where `foo(_)` is a plain function that takes the exact argument type of
|
|
|
|
/// `x`.
|
2016-08-06 08:18:36 +00:00
|
|
|
pub REDUNDANT_CLOSURE,
|
2018-03-28 13:24:26 +00:00
|
|
|
style,
|
2019-01-31 01:15:29 +00:00
|
|
|
"redundant closures, i.e., `|a| foo(a)` (which can be written as just `foo`)"
|
2016-02-05 23:13:29 +00:00
|
|
|
}
|
2015-05-10 05:09:04 +00:00
|
|
|
|
2019-05-16 06:25:39 +00:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for closures which only invoke a method on the closure
|
2019-05-16 06:25:39 +00:00
|
|
|
/// argument and can be replaced by referencing the method directly.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// It's unnecessary to create the closure.
|
2019-05-16 06:25:39 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2019-05-16 06:25:39 +00:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// Some('a').map(|s| s.to_uppercase());
|
|
|
|
/// ```
|
|
|
|
/// may be rewritten as
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// Some('a').map(char::to_uppercase);
|
|
|
|
/// ```
|
2019-05-16 16:18:15 +00:00
|
|
|
pub REDUNDANT_CLOSURE_FOR_METHOD_CALLS,
|
2019-05-16 06:25:39 +00:00
|
|
|
pedantic,
|
|
|
|
"redundant closures for method calls"
|
|
|
|
}
|
|
|
|
|
2019-05-16 16:18:15 +00:00
|
|
|
declare_lint_pass!(EtaReduction => [REDUNDANT_CLOSURE, REDUNDANT_CLOSURE_FOR_METHOD_CALLS]);
|
2015-05-10 05:09:04 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for EtaReduction {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2021-09-28 17:03:12 +00:00
|
|
|
if expr.span.from_expansion() {
|
2019-02-25 12:40:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-09-28 17:03:12 +00:00
|
|
|
let body = match expr.kind {
|
|
|
|
ExprKind::Closure(_, _, id, _, _) => cx.tcx.hir().body(id),
|
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
if body.value.span.from_expansion() {
|
|
|
|
if body.params.is_empty() {
|
|
|
|
if let Some(VecArgs::Vec(&[])) = higher::VecArgs::hir(cx, &body.value) {
|
2021-06-03 06:41:37 +00:00
|
|
|
// replace `|| vec![]` with `Vec::new`
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
REDUNDANT_CLOSURE,
|
|
|
|
expr.span,
|
|
|
|
"redundant closure",
|
|
|
|
"replace the closure with `Vec::new`",
|
|
|
|
"std::vec::Vec::new".into(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
2021-03-12 14:30:50 +00:00
|
|
|
}
|
|
|
|
// skip `foo(|| macro!())`
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-28 17:03:12 +00:00
|
|
|
let closure_ty = cx.typeck_results().expr_ty(expr);
|
2019-02-10 11:58:51 +00:00
|
|
|
|
2021-09-28 17:03:12 +00:00
|
|
|
if_chain!(
|
|
|
|
if let ExprKind::Call(callee, args) = body.value.kind;
|
|
|
|
if let ExprKind::Path(_) = callee.kind;
|
|
|
|
if check_inputs(cx, body.params, args);
|
|
|
|
let callee_ty = cx.typeck_results().expr_ty_adjusted(callee);
|
|
|
|
let call_ty = cx.typeck_results().type_dependent_def_id(body.value.hir_id)
|
|
|
|
.map_or(callee_ty, |id| cx.tcx.type_of(id));
|
|
|
|
if check_sig(cx, closure_ty, call_ty);
|
|
|
|
let substs = cx.typeck_results().node_substs(callee.hir_id);
|
|
|
|
// This fixes some false positives that I don't entirely understand
|
|
|
|
if substs.is_empty() || !cx.typeck_results().expr_ty(expr).has_late_bound_regions();
|
|
|
|
// A type param function ref like `T::f` is not 'static, however
|
|
|
|
// it is if cast like `T::f as fn()`. This seems like a rustc bug.
|
|
|
|
if !substs.types().any(|t| matches!(t.kind(), ty::Param(_)));
|
2019-02-10 11:58:51 +00:00
|
|
|
then {
|
2021-03-12 14:30:50 +00:00
|
|
|
span_lint_and_then(cx, REDUNDANT_CLOSURE, expr.span, "redundant closure", |diag| {
|
2021-09-28 17:03:12 +00:00
|
|
|
if let Some(mut snippet) = snippet_opt(cx, callee.span) {
|
2021-07-15 08:44:10 +00:00
|
|
|
if_chain! {
|
2021-09-28 17:03:12 +00:00
|
|
|
if let ty::Closure(_, substs) = callee_ty.peel_refs().kind();
|
2021-10-07 09:21:30 +00:00
|
|
|
if substs.as_closure().kind() == ClosureKind::FnMut;
|
2021-09-28 17:03:12 +00:00
|
|
|
if get_enclosing_loop_or_closure(cx.tcx, expr).is_some()
|
|
|
|
|| UsedAfterExprVisitor::is_found(cx, callee);
|
2021-07-15 08:44:10 +00:00
|
|
|
|
|
|
|
then {
|
|
|
|
// Mutable closure is used after current expr; we cannot consume it.
|
|
|
|
snippet = format!("&mut {}", snippet);
|
|
|
|
}
|
|
|
|
}
|
2020-04-17 06:08:00 +00:00
|
|
|
diag.span_suggestion(
|
2019-02-10 11:58:51 +00:00
|
|
|
expr.span,
|
2021-03-12 14:30:50 +00:00
|
|
|
"replace the closure with the function itself",
|
2019-02-10 11:58:51 +00:00
|
|
|
snippet,
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
2016-11-16 20:57:56 +00:00
|
|
|
}
|
2019-02-10 11:58:51 +00:00
|
|
|
});
|
2016-11-16 20:57:56 +00:00
|
|
|
}
|
2019-02-10 11:58:51 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if_chain!(
|
2021-09-28 17:03:12 +00:00
|
|
|
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);
|
|
|
|
if check_sig(cx, closure_ty, call_ty);
|
2019-02-10 11:58:51 +00:00
|
|
|
then {
|
2021-09-28 17:03:12 +00:00
|
|
|
span_lint_and_then(cx, REDUNDANT_CLOSURE_FOR_METHOD_CALLS, expr.span, "redundant closure", |diag| {
|
|
|
|
let name = get_ufcs_type_name(cx, method_def_id);
|
|
|
|
diag.span_suggestion(
|
|
|
|
expr.span,
|
|
|
|
"replace the closure with the method itself",
|
|
|
|
format!("{}::{}", name, path.ident.name),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
})
|
2019-02-10 11:58:51 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 17:03:12 +00:00
|
|
|
fn check_inputs(cx: &LateContext<'_>, params: &[Param<'_>], call_args: &[Expr<'_>]) -> bool {
|
|
|
|
if params.len() != call_args.len() {
|
|
|
|
return false;
|
2019-02-10 11:58:51 +00:00
|
|
|
}
|
2021-09-28 17:03:12 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
match *cx.typeck_results().expr_adjustments(arg) {
|
|
|
|
[] => true,
|
|
|
|
[Adjustment {
|
|
|
|
kind: Adjust::Deref(None),
|
|
|
|
..
|
|
|
|
}, Adjustment {
|
|
|
|
kind: Adjust::Borrow(AutoBorrow::Ref(_, mu2)),
|
|
|
|
..
|
|
|
|
}] => {
|
|
|
|
// re-borrow with the same mutability is allowed
|
|
|
|
let ty = cx.typeck_results().expr_ty(arg);
|
|
|
|
matches!(*ty.kind(), ty::Ref(.., mu1) if mu1 == mu2.into())
|
|
|
|
},
|
|
|
|
_ => false,
|
2019-02-10 11:58:51 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-28 17:03:12 +00:00
|
|
|
fn check_sig<'tcx>(cx: &LateContext<'tcx>, closure_ty: Ty<'tcx>, call_ty: Ty<'tcx>) -> bool {
|
|
|
|
let call_sig = call_ty.fn_sig(cx.tcx);
|
|
|
|
if call_sig.unsafety() == Unsafety::Unsafe {
|
|
|
|
return false;
|
2019-02-10 11:58:51 +00:00
|
|
|
}
|
2021-09-28 17:03:12 +00:00
|
|
|
if !closure_ty.has_late_bound_regions() {
|
|
|
|
return true;
|
2019-02-10 11:58:51 +00:00
|
|
|
}
|
2021-09-28 17:03:12 +00:00
|
|
|
let substs = match closure_ty.kind() {
|
|
|
|
ty::Closure(_, substs) => substs,
|
|
|
|
_ => return false,
|
|
|
|
};
|
|
|
|
let closure_sig = cx.tcx.signature_unclosure(substs.as_closure().sig(), Unsafety::Normal);
|
|
|
|
cx.tcx.erase_late_bound_regions(closure_sig) == cx.tcx.erase_late_bound_regions(call_sig)
|
2019-02-10 11:58:51 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 17:03:12 +00:00
|
|
|
fn get_ufcs_type_name(cx: &LateContext<'_>, method_def_id: DefId) -> String {
|
|
|
|
match cx.tcx.associated_item(method_def_id).container {
|
|
|
|
ty::TraitContainer(def_id) => cx.tcx.def_path_str(def_id),
|
|
|
|
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.to_string(),
|
2019-02-10 11:58:51 +00:00
|
|
|
}
|
2021-09-28 17:03:12 +00:00
|
|
|
},
|
2015-05-10 05:09:04 +00:00
|
|
|
}
|
|
|
|
}
|