2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::source::snippet;
|
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
2021-12-17 06:40:22 -06:00
|
|
|
use clippy_utils::{match_def_path, meets_msrv, msrvs, path_to_local_id, paths, peel_blocks};
|
2021-03-12 08:30:50 -06:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_lint::LateContext;
|
|
|
|
use rustc_middle::ty;
|
|
|
|
use rustc_semver::RustcVersion;
|
|
|
|
use rustc_span::sym;
|
|
|
|
|
|
|
|
use super::OPTION_AS_REF_DEREF;
|
|
|
|
|
|
|
|
/// lint use of `_.as_ref().map(Deref::deref)` for `Option`s
|
|
|
|
pub(super) fn check<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
expr: &hir::Expr<'_>,
|
2021-04-08 10:50:13 -05:00
|
|
|
as_ref_recv: &hir::Expr<'_>,
|
|
|
|
map_arg: &hir::Expr<'_>,
|
2021-03-12 08:30:50 -06:00
|
|
|
is_mut: bool,
|
2022-05-21 06:24:00 -05:00
|
|
|
msrv: Option<RustcVersion>,
|
2021-03-12 08:30:50 -06:00
|
|
|
) {
|
2022-05-21 06:24:00 -05:00
|
|
|
if !meets_msrv(msrv, msrvs::OPTION_AS_DEREF) {
|
2021-03-12 08:30:50 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let same_mutability = |m| (is_mut && m == &hir::Mutability::Mut) || (!is_mut && m == &hir::Mutability::Not);
|
|
|
|
|
2021-04-08 10:50:13 -05:00
|
|
|
let option_ty = cx.typeck_results().expr_ty(as_ref_recv);
|
2021-10-02 18:51:01 -05:00
|
|
|
if !is_type_diagnostic_item(cx, option_ty, sym::Option) {
|
2021-03-12 08:30:50 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let deref_aliases: [&[&str]; 9] = [
|
|
|
|
&paths::DEREF_TRAIT_METHOD,
|
|
|
|
&paths::DEREF_MUT_TRAIT_METHOD,
|
|
|
|
&paths::CSTRING_AS_C_STR,
|
|
|
|
&paths::OS_STRING_AS_OS_STR,
|
|
|
|
&paths::PATH_BUF_AS_PATH,
|
|
|
|
&paths::STRING_AS_STR,
|
|
|
|
&paths::STRING_AS_MUT_STR,
|
|
|
|
&paths::VEC_AS_SLICE,
|
|
|
|
&paths::VEC_AS_MUT_SLICE,
|
|
|
|
];
|
|
|
|
|
2021-04-08 10:50:13 -05:00
|
|
|
let is_deref = match map_arg.kind {
|
2021-03-12 08:30:50 -06:00
|
|
|
hir::ExprKind::Path(ref expr_qpath) => cx
|
2021-04-08 10:50:13 -05:00
|
|
|
.qpath_res(expr_qpath, map_arg.hir_id)
|
2021-03-12 08:30:50 -06:00
|
|
|
.opt_def_id()
|
|
|
|
.map_or(false, |fun_def_id| {
|
|
|
|
deref_aliases.iter().any(|path| match_def_path(cx, fun_def_id, path))
|
|
|
|
}),
|
2022-06-11 14:25:25 -05:00
|
|
|
hir::ExprKind::Closure { body, .. } => {
|
|
|
|
let closure_body = cx.tcx.hir().body(body);
|
2021-12-17 06:40:22 -06:00
|
|
|
let closure_expr = peel_blocks(&closure_body.value);
|
2021-03-12 08:30:50 -06:00
|
|
|
|
|
|
|
match &closure_expr.kind {
|
2021-12-01 11:17:50 -06:00
|
|
|
hir::ExprKind::MethodCall(_, args, _) => {
|
2021-03-12 08:30:50 -06:00
|
|
|
if_chain! {
|
|
|
|
if args.len() == 1;
|
|
|
|
if path_to_local_id(&args[0], closure_body.params[0].pat.hir_id);
|
|
|
|
let adj = cx
|
|
|
|
.typeck_results()
|
|
|
|
.expr_adjustments(&args[0])
|
|
|
|
.iter()
|
|
|
|
.map(|x| &x.kind)
|
|
|
|
.collect::<Box<[_]>>();
|
|
|
|
if let [ty::adjustment::Adjust::Deref(None), ty::adjustment::Adjust::Borrow(_)] = *adj;
|
|
|
|
then {
|
|
|
|
let method_did = cx.typeck_results().type_dependent_def_id(closure_expr.hir_id).unwrap();
|
|
|
|
deref_aliases.iter().any(|path| match_def_path(cx, method_did, path))
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-04-08 10:50:13 -05:00
|
|
|
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, m, inner) if same_mutability(m) => {
|
2021-03-12 08:30:50 -06:00
|
|
|
if_chain! {
|
2021-04-08 10:50:13 -05:00
|
|
|
if let hir::ExprKind::Unary(hir::UnOp::Deref, inner1) = inner.kind;
|
|
|
|
if let hir::ExprKind::Unary(hir::UnOp::Deref, inner2) = inner1.kind;
|
2021-03-12 08:30:50 -06:00
|
|
|
then {
|
|
|
|
path_to_local_id(inner2, closure_body.params[0].pat.hir_id)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
if is_deref {
|
|
|
|
let current_method = if is_mut {
|
2021-04-08 10:50:13 -05:00
|
|
|
format!(".as_mut().map({})", snippet(cx, map_arg.span, ".."))
|
2021-03-12 08:30:50 -06:00
|
|
|
} else {
|
2021-04-08 10:50:13 -05:00
|
|
|
format!(".as_ref().map({})", snippet(cx, map_arg.span, ".."))
|
2021-03-12 08:30:50 -06:00
|
|
|
};
|
|
|
|
let method_hint = if is_mut { "as_deref_mut" } else { "as_deref" };
|
2021-04-08 10:50:13 -05:00
|
|
|
let hint = format!("{}.{}()", snippet(cx, as_ref_recv.span, ".."), method_hint);
|
2021-03-12 08:30:50 -06:00
|
|
|
let suggestion = format!("try using {} instead", method_hint);
|
|
|
|
|
|
|
|
let msg = format!(
|
|
|
|
"called `{0}` on an Option value. This can be done more directly \
|
|
|
|
by calling `{1}` instead",
|
|
|
|
current_method, hint
|
|
|
|
);
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
OPTION_AS_REF_DEREF,
|
|
|
|
expr.span,
|
|
|
|
&msg,
|
|
|
|
&suggestion,
|
|
|
|
hint,
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|