2020-05-17 15:36:26 +00:00
|
|
|
use super::{contains_return, BIND_INSTEAD_OF_MAP};
|
2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::{multispan_sugg_with_applicability, span_lint_and_sugg, span_lint_and_then};
|
|
|
|
use clippy_utils::source::{snippet, snippet_with_macro_callsite};
|
2021-04-08 15:50:13 +00:00
|
|
|
use clippy_utils::{in_macro, remove_blocks, visitors::find_all_ret_expressions};
|
2020-05-17 15:36:26 +00:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir as hir;
|
2021-04-08 15:50:13 +00:00
|
|
|
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
|
|
|
|
use rustc_hir::{LangItem, QPath};
|
2020-05-17 15:36:26 +00:00
|
|
|
use rustc_lint::LateContext;
|
2021-04-08 15:50:13 +00:00
|
|
|
use rustc_middle::ty::DefIdTree;
|
2020-05-17 15:36:26 +00:00
|
|
|
use rustc_span::Span;
|
|
|
|
|
|
|
|
pub(crate) struct OptionAndThenSome;
|
2020-09-24 12:49:22 +00:00
|
|
|
|
2020-05-17 15:36:26 +00:00
|
|
|
impl BindInsteadOfMap for OptionAndThenSome {
|
2021-04-08 15:50:13 +00:00
|
|
|
const VARIANT_LANG_ITEM: LangItem = LangItem::OptionSome;
|
2020-05-17 15:36:26 +00:00
|
|
|
const BAD_METHOD_NAME: &'static str = "and_then";
|
|
|
|
const GOOD_METHOD_NAME: &'static str = "map";
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct ResultAndThenOk;
|
2020-09-24 12:49:22 +00:00
|
|
|
|
2020-05-17 15:36:26 +00:00
|
|
|
impl BindInsteadOfMap for ResultAndThenOk {
|
2021-04-08 15:50:13 +00:00
|
|
|
const VARIANT_LANG_ITEM: LangItem = LangItem::ResultOk;
|
2020-05-17 15:36:26 +00:00
|
|
|
const BAD_METHOD_NAME: &'static str = "and_then";
|
|
|
|
const GOOD_METHOD_NAME: &'static str = "map";
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct ResultOrElseErrInfo;
|
2020-09-24 12:49:22 +00:00
|
|
|
|
2020-05-17 15:36:26 +00:00
|
|
|
impl BindInsteadOfMap for ResultOrElseErrInfo {
|
2021-04-08 15:50:13 +00:00
|
|
|
const VARIANT_LANG_ITEM: LangItem = LangItem::ResultErr;
|
2020-05-17 15:36:26 +00:00
|
|
|
const BAD_METHOD_NAME: &'static str = "or_else";
|
|
|
|
const GOOD_METHOD_NAME: &'static str = "map_err";
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) trait BindInsteadOfMap {
|
2021-04-08 15:50:13 +00:00
|
|
|
const VARIANT_LANG_ITEM: LangItem;
|
2020-05-17 15:36:26 +00:00
|
|
|
const BAD_METHOD_NAME: &'static str;
|
|
|
|
const GOOD_METHOD_NAME: &'static str;
|
|
|
|
|
2021-04-08 15:50:13 +00:00
|
|
|
fn no_op_msg(cx: &LateContext<'_>) -> Option<String> {
|
|
|
|
let variant_id = cx.tcx.lang_items().require(Self::VARIANT_LANG_ITEM).ok()?;
|
|
|
|
let item_id = cx.tcx.parent(variant_id)?;
|
|
|
|
Some(format!(
|
2020-05-17 15:36:26 +00:00
|
|
|
"using `{}.{}({})`, which is a no-op",
|
2021-04-08 15:50:13 +00:00
|
|
|
cx.tcx.item_name(item_id),
|
2020-05-17 15:36:26 +00:00
|
|
|
Self::BAD_METHOD_NAME,
|
2021-04-08 15:50:13 +00:00
|
|
|
cx.tcx.item_name(variant_id),
|
|
|
|
))
|
2020-05-17 15:36:26 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 15:50:13 +00:00
|
|
|
fn lint_msg(cx: &LateContext<'_>) -> Option<String> {
|
|
|
|
let variant_id = cx.tcx.lang_items().require(Self::VARIANT_LANG_ITEM).ok()?;
|
|
|
|
let item_id = cx.tcx.parent(variant_id)?;
|
|
|
|
Some(format!(
|
2020-05-17 15:36:26 +00:00
|
|
|
"using `{}.{}(|x| {}(y))`, which is more succinctly expressed as `{}(|x| y)`",
|
2021-04-08 15:50:13 +00:00
|
|
|
cx.tcx.item_name(item_id),
|
2020-05-17 15:36:26 +00:00
|
|
|
Self::BAD_METHOD_NAME,
|
2021-04-08 15:50:13 +00:00
|
|
|
cx.tcx.item_name(variant_id),
|
2020-05-17 15:36:26 +00:00
|
|
|
Self::GOOD_METHOD_NAME
|
2021-04-08 15:50:13 +00:00
|
|
|
))
|
2020-05-17 15:36:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn lint_closure_autofixable(
|
2020-06-25 20:41:36 +00:00
|
|
|
cx: &LateContext<'_>,
|
2020-05-17 15:36:26 +00:00
|
|
|
expr: &hir::Expr<'_>,
|
2021-04-08 15:50:13 +00:00
|
|
|
recv: &hir::Expr<'_>,
|
2020-05-17 15:36:26 +00:00
|
|
|
closure_expr: &hir::Expr<'_>,
|
|
|
|
closure_args_span: Span,
|
|
|
|
) -> bool {
|
|
|
|
if_chain! {
|
2021-04-08 15:50:13 +00:00
|
|
|
if let hir::ExprKind::Call(some_expr, [inner_expr]) = closure_expr.kind;
|
|
|
|
if let hir::ExprKind::Path(QPath::Resolved(_, path)) = some_expr.kind;
|
|
|
|
if Self::is_variant(cx, path.res);
|
|
|
|
if !contains_return(inner_expr);
|
|
|
|
if let Some(msg) = Self::lint_msg(cx);
|
2020-05-17 15:36:26 +00:00
|
|
|
then {
|
|
|
|
let some_inner_snip = if inner_expr.span.from_expansion() {
|
|
|
|
snippet_with_macro_callsite(cx, inner_expr.span, "_")
|
|
|
|
} else {
|
|
|
|
snippet(cx, inner_expr.span, "_")
|
|
|
|
};
|
|
|
|
|
|
|
|
let closure_args_snip = snippet(cx, closure_args_span, "..");
|
2021-04-08 15:50:13 +00:00
|
|
|
let option_snip = snippet(cx, recv.span, "..");
|
2020-05-17 15:36:26 +00:00
|
|
|
let note = format!("{}.{}({} {})", option_snip, Self::GOOD_METHOD_NAME, closure_args_snip, some_inner_snip);
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
BIND_INSTEAD_OF_MAP,
|
|
|
|
expr.span,
|
2021-04-08 15:50:13 +00:00
|
|
|
&msg,
|
2020-05-17 15:36:26 +00:00
|
|
|
"try this",
|
|
|
|
note,
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 12:49:22 +00:00
|
|
|
fn lint_closure(cx: &LateContext<'_>, expr: &hir::Expr<'_>, closure_expr: &hir::Expr<'_>) -> bool {
|
2020-05-17 15:36:26 +00:00
|
|
|
let mut suggs = Vec::new();
|
2020-09-24 12:49:22 +00:00
|
|
|
let can_sugg: bool = find_all_ret_expressions(cx, closure_expr, |ret_expr| {
|
2020-05-17 15:36:26 +00:00
|
|
|
if_chain! {
|
|
|
|
if !in_macro(ret_expr.span);
|
2021-04-08 15:50:13 +00:00
|
|
|
if let hir::ExprKind::Call(func_path, [arg]) = ret_expr.kind;
|
|
|
|
if let hir::ExprKind::Path(QPath::Resolved(_, path)) = func_path.kind;
|
|
|
|
if Self::is_variant(cx, path.res);
|
|
|
|
if !contains_return(arg);
|
2020-05-17 15:36:26 +00:00
|
|
|
then {
|
2021-04-08 15:50:13 +00:00
|
|
|
suggs.push((ret_expr.span, arg.span.source_callsite()));
|
2020-05-17 15:36:26 +00:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2021-04-08 15:50:13 +00:00
|
|
|
let (span, msg) = if_chain! {
|
|
|
|
if can_sugg;
|
|
|
|
if let hir::ExprKind::MethodCall(_, span, ..) = expr.kind;
|
|
|
|
if let Some(msg) = Self::lint_msg(cx);
|
|
|
|
then { (span, msg) } else { return false; }
|
|
|
|
};
|
|
|
|
span_lint_and_then(cx, BIND_INSTEAD_OF_MAP, expr.span, &msg, |diag| {
|
|
|
|
multispan_sugg_with_applicability(
|
|
|
|
diag,
|
|
|
|
"try this",
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
std::iter::once((span, Self::GOOD_METHOD_NAME.into())).chain(
|
|
|
|
suggs
|
|
|
|
.into_iter()
|
|
|
|
.map(|(span1, span2)| (span1, snippet(cx, span2, "_").into())),
|
|
|
|
),
|
2021-06-03 06:41:37 +00:00
|
|
|
);
|
2021-04-08 15:50:13 +00:00
|
|
|
});
|
|
|
|
true
|
2020-05-17 15:36:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Lint use of `_.and_then(|x| Some(y))` for `Option`s
|
2021-04-08 15:50:13 +00:00
|
|
|
fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>) -> bool {
|
|
|
|
if_chain! {
|
|
|
|
if let Some(adt) = cx.typeck_results().expr_ty(recv).ty_adt_def();
|
|
|
|
if let Ok(vid) = cx.tcx.lang_items().require(Self::VARIANT_LANG_ITEM);
|
|
|
|
if Some(adt.did) == cx.tcx.parent(vid);
|
|
|
|
then {} else { return false; }
|
2020-05-17 15:36:26 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 15:50:13 +00:00
|
|
|
match arg.kind {
|
2020-05-17 15:36:26 +00:00
|
|
|
hir::ExprKind::Closure(_, _, body_id, closure_args_span, _) => {
|
|
|
|
let closure_body = cx.tcx.hir().body(body_id);
|
|
|
|
let closure_expr = remove_blocks(&closure_body.value);
|
|
|
|
|
2021-04-08 15:50:13 +00:00
|
|
|
if Self::lint_closure_autofixable(cx, expr, recv, closure_expr, closure_args_span) {
|
2020-09-24 12:49:22 +00:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
Self::lint_closure(cx, expr, closure_expr)
|
2020-05-17 15:36:26 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
// `_.and_then(Some)` case, which is no-op.
|
2021-04-08 15:50:13 +00:00
|
|
|
hir::ExprKind::Path(QPath::Resolved(_, path)) if Self::is_variant(cx, path.res) => {
|
|
|
|
if let Some(msg) = Self::no_op_msg(cx) {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
BIND_INSTEAD_OF_MAP,
|
|
|
|
expr.span,
|
|
|
|
&msg,
|
|
|
|
"use the expression directly",
|
|
|
|
snippet(cx, recv.span, "..").into(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
2020-09-24 12:49:22 +00:00
|
|
|
true
|
2020-05-17 15:36:26 +00:00
|
|
|
},
|
2020-09-24 12:49:22 +00:00
|
|
|
_ => false,
|
2020-05-17 15:36:26 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-08 15:50:13 +00:00
|
|
|
|
|
|
|
fn is_variant(cx: &LateContext<'_>, res: Res) -> bool {
|
|
|
|
if let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Fn), id) = res {
|
|
|
|
if let Ok(variant_id) = cx.tcx.lang_items().require(Self::VARIANT_LANG_ITEM) {
|
|
|
|
return cx.tcx.parent(id) == Some(variant_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2020-05-17 15:36:26 +00:00
|
|
|
}
|