]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/methods/useless_asref.rs
Fix adjacent code
[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, is_trait_method};
5 use if_chain::if_chain;
6 use rustc_errors::Applicability;
7 use rustc_hir as hir;
8 use rustc_lint::LateContext;
9 use rustc_span::sym;
10
11 use super::USELESS_ASREF;
12
13 /// Checks for the `USELESS_ASREF` lint.
14 pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str, recvr: &hir::Expr<'_>) {
15     // when we get here, we've already checked that the call name is "as_ref" or "as_mut"
16     // check if the call is to the actual `AsRef` or `AsMut` trait
17     if is_trait_method(cx, expr, sym::AsRef) || is_trait_method(cx, expr, sym::AsMut) {
18         // check if the type after `as_ref` or `as_mut` is the same as before
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(segment, ..) = parent.kind;
28                 if segment.ident.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 `{call_name}` does nothing"),
40                 "try this",
41                 snippet_with_applicability(cx, recvr.span, "..", &mut applicability).to_string(),
42                 applicability,
43             );
44         }
45     }
46 }