]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/methods/useless_asref.rs
Auto merge of #6957 - camsteffen:eq-ty-kind, r=flip1995
[rust.git] / clippy_lints / src / methods / useless_asref.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::source::snippet_with_applicability;
3 use clippy_utils::ty::walk_ptrs_ty_depth;
4 use clippy_utils::{get_parent_expr, match_trait_method, paths};
5 use if_chain::if_chain;
6 use rustc_errors::Applicability;
7 use rustc_hir as hir;
8 use rustc_lint::LateContext;
9
10 use super::USELESS_ASREF;
11
12 /// Checks for the `USELESS_ASREF` lint.
13 pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str, as_ref_args: &[hir::Expr<'_>]) {
14     // when we get here, we've already checked that the call name is "as_ref" or "as_mut"
15     // check if the call is to the actual `AsRef` or `AsMut` trait
16     if match_trait_method(cx, expr, &paths::ASREF_TRAIT) || match_trait_method(cx, expr, &paths::ASMUT_TRAIT) {
17         // check if the type after `as_ref` or `as_mut` is the same as before
18         let recvr = &as_ref_args[0];
19         let rcv_ty = cx.typeck_results().expr_ty(recvr);
20         let res_ty = cx.typeck_results().expr_ty(expr);
21         let (base_res_ty, res_depth) = walk_ptrs_ty_depth(res_ty);
22         let (base_rcv_ty, rcv_depth) = walk_ptrs_ty_depth(rcv_ty);
23         if base_rcv_ty == base_res_ty && rcv_depth >= res_depth {
24             // allow the `as_ref` or `as_mut` if it is followed by another method call
25             if_chain! {
26                 if let Some(parent) = get_parent_expr(cx, expr);
27                 if let hir::ExprKind::MethodCall(_, ref span, _, _) = parent.kind;
28                 if span != &expr.span;
29                 then {
30                     return;
31                 }
32             }
33
34             let mut applicability = Applicability::MachineApplicable;
35             span_lint_and_sugg(
36                 cx,
37                 USELESS_ASREF,
38                 expr.span,
39                 &format!("this call to `{}` does nothing", call_name),
40                 "try this",
41                 snippet_with_applicability(cx, recvr.span, "..", &mut applicability).to_string(),
42                 applicability,
43             );
44         }
45     }
46 }