2 is_type_diagnostic_item, match_def_path, meets_msrv, path_to_local_id, paths, remove_blocks, snippet,
5 use if_chain::if_chain;
6 use rustc_errors::Applicability;
8 use rustc_lint::LateContext;
10 use rustc_semver::RustcVersion;
13 use super::OPTION_AS_REF_DEREF;
15 const OPTION_AS_REF_DEREF_MSRV: RustcVersion = RustcVersion::new(1, 40, 0);
17 /// lint use of `_.as_ref().map(Deref::deref)` for `Option`s
18 pub(super) fn check<'tcx>(
19 cx: &LateContext<'tcx>,
21 as_ref_args: &[hir::Expr<'_>],
22 map_args: &[hir::Expr<'_>],
24 msrv: Option<&RustcVersion>,
26 if !meets_msrv(msrv, &OPTION_AS_REF_DEREF_MSRV) {
30 let same_mutability = |m| (is_mut && m == &hir::Mutability::Mut) || (!is_mut && m == &hir::Mutability::Not);
32 let option_ty = cx.typeck_results().expr_ty(&as_ref_args[0]);
33 if !is_type_diagnostic_item(cx, option_ty, sym::option_type) {
37 let deref_aliases: [&[&str]; 9] = [
38 &paths::DEREF_TRAIT_METHOD,
39 &paths::DEREF_MUT_TRAIT_METHOD,
40 &paths::CSTRING_AS_C_STR,
41 &paths::OS_STRING_AS_OS_STR,
42 &paths::PATH_BUF_AS_PATH,
43 &paths::STRING_AS_STR,
44 &paths::STRING_AS_MUT_STR,
46 &paths::VEC_AS_MUT_SLICE,
49 let is_deref = match map_args[1].kind {
50 hir::ExprKind::Path(ref expr_qpath) => cx
51 .qpath_res(expr_qpath, map_args[1].hir_id)
53 .map_or(false, |fun_def_id| {
54 deref_aliases.iter().any(|path| match_def_path(cx, fun_def_id, path))
56 hir::ExprKind::Closure(_, _, body_id, _, _) => {
57 let closure_body = cx.tcx.hir().body(body_id);
58 let closure_expr = remove_blocks(&closure_body.value);
60 match &closure_expr.kind {
61 hir::ExprKind::MethodCall(_, _, args, _) => {
64 if path_to_local_id(&args[0], closure_body.params[0].pat.hir_id);
67 .expr_adjustments(&args[0])
70 .collect::<Box<[_]>>();
71 if let [ty::adjustment::Adjust::Deref(None), ty::adjustment::Adjust::Borrow(_)] = *adj;
73 let method_did = cx.typeck_results().type_dependent_def_id(closure_expr.hir_id).unwrap();
74 deref_aliases.iter().any(|path| match_def_path(cx, method_did, path))
80 hir::ExprKind::AddrOf(hir::BorrowKind::Ref, m, ref inner) if same_mutability(m) => {
82 if let hir::ExprKind::Unary(hir::UnOp::Deref, ref inner1) = inner.kind;
83 if let hir::ExprKind::Unary(hir::UnOp::Deref, ref inner2) = inner1.kind;
85 path_to_local_id(inner2, closure_body.params[0].pat.hir_id)
98 let current_method = if is_mut {
99 format!(".as_mut().map({})", snippet(cx, map_args[1].span, ".."))
101 format!(".as_ref().map({})", snippet(cx, map_args[1].span, ".."))
103 let method_hint = if is_mut { "as_deref_mut" } else { "as_deref" };
104 let hint = format!("{}.{}()", snippet(cx, as_ref_args[0].span, ".."), method_hint);
105 let suggestion = format!("try using {} instead", method_hint);
108 "called `{0}` on an Option value. This can be done more directly \
109 by calling `{1}` instead",
119 Applicability::MachineApplicable,