about summary refs log tree commit diff
path: root/clippy_lints/src/methods/useless_asref.rs
diff options
context:
space:
mode:
authorflip1995 <philipp.krones@embecosm.com>2021-03-12 15:30:50 +0100
committerflip1995 <philipp.krones@embecosm.com>2021-03-12 15:30:50 +0100
commitf2f2a005b4efd3e44ac6a02ea2b9660d28401679 (patch)
treec4ece65dffee2aa79eaa3b7f190765a95055f815 /clippy_lints/src/methods/useless_asref.rs
parent36a27ecaacad74f69b21a12bc66b826f11f2d44e (diff)
downloadrust-f2f2a005b4efd3e44ac6a02ea2b9660d28401679.tar.gz
rust-f2f2a005b4efd3e44ac6a02ea2b9660d28401679.zip
Merge commit '6ed6f1e6a1a8f414ba7e6d9b8222e7e5a1686e42' into clippyup
Diffstat (limited to 'clippy_lints/src/methods/useless_asref.rs')
-rw-r--r--clippy_lints/src/methods/useless_asref.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/clippy_lints/src/methods/useless_asref.rs b/clippy_lints/src/methods/useless_asref.rs
new file mode 100644
index 00000000000..e4554f8d489
--- /dev/null
+++ b/clippy_lints/src/methods/useless_asref.rs
@@ -0,0 +1,45 @@
+use crate::utils::{
+    get_parent_expr, match_trait_method, paths, snippet_with_applicability, span_lint_and_sugg, walk_ptrs_ty_depth,
+};
+use if_chain::if_chain;
+use rustc_errors::Applicability;
+use rustc_hir as hir;
+use rustc_lint::LateContext;
+
+use super::USELESS_ASREF;
+
+/// Checks for the `USELESS_ASREF` lint.
+pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str, as_ref_args: &[hir::Expr<'_>]) {
+    // when we get here, we've already checked that the call name is "as_ref" or "as_mut"
+    // check if the call is to the actual `AsRef` or `AsMut` trait
+    if match_trait_method(cx, expr, &paths::ASREF_TRAIT) || match_trait_method(cx, expr, &paths::ASMUT_TRAIT) {
+        // check if the type after `as_ref` or `as_mut` is the same as before
+        let recvr = &as_ref_args[0];
+        let rcv_ty = cx.typeck_results().expr_ty(recvr);
+        let res_ty = cx.typeck_results().expr_ty(expr);
+        let (base_res_ty, res_depth) = walk_ptrs_ty_depth(res_ty);
+        let (base_rcv_ty, rcv_depth) = walk_ptrs_ty_depth(rcv_ty);
+        if base_rcv_ty == base_res_ty && rcv_depth >= res_depth {
+            // allow the `as_ref` or `as_mut` if it is followed by another method call
+            if_chain! {
+                if let Some(parent) = get_parent_expr(cx, expr);
+                if let hir::ExprKind::MethodCall(_, ref span, _, _) = parent.kind;
+                if span != &expr.span;
+                then {
+                    return;
+                }
+            }
+
+            let mut applicability = Applicability::MachineApplicable;
+            span_lint_and_sugg(
+                cx,
+                USELESS_ASREF,
+                expr.span,
+                &format!("this call to `{}` does nothing", call_name),
+                "try this",
+                snippet_with_applicability(cx, recvr.span, "..", &mut applicability).to_string(),
+                applicability,
+            );
+        }
+    }
+}