]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/ptr.rs
Auto merge of #4809 - iankronquist:patch-1, r=flip1995
[rust.git] / clippy_lints / src / ptr.rs
index 68fb5b0b20f1346f711001971a569e6b98f5776e..445f065aaced22c33aa7a6c9f99a4e1823507249 100644 (file)
@@ -1,17 +1,20 @@
 //! Checks for usage of  `&Vec[_]` and `&String`.
 
 use crate::utils::ptr::get_spans;
-use crate::utils::{match_qpath, match_type, paths, snippet_opt, span_lint, span_lint_and_then, walk_ptrs_hir_ty};
+use crate::utils::{
+    is_type_diagnostic_item, match_qpath, match_type, paths, snippet_opt, span_lint, span_lint_and_then,
+    walk_ptrs_hir_ty,
+};
 use if_chain::if_chain;
-use rustc::hir::QPath;
-use rustc::hir::*;
-use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
 use rustc::ty;
-use rustc::{declare_tool_lint, lint_array};
 use rustc_errors::Applicability;
+use rustc_hir::QPath;
+use rustc_hir::*;
+use rustc_lint::{LateContext, LateLintPass};
+use rustc_session::{declare_lint_pass, declare_tool_lint};
+use rustc_span::source_map::Span;
+use rustc_span::{MultiSpan, Symbol};
 use std::borrow::Cow;
-use syntax::source_map::Span;
-use syntax_pos::MultiSpan;
 
 declare_clippy_lint! {
     /// **What it does:** This lint checks for function arguments of type `&String`
@@ -41,7 +44,7 @@
     /// function before applying the lint suggestions in this case.
     ///
     /// **Example:**
-    /// ```rust
+    /// ```ignore
     /// fn foo(&Vec<u32>) { .. }
     /// ```
     pub PTR_ARG,
@@ -59,7 +62,7 @@
     /// **Known problems:** None.
     ///
     /// **Example:**
-    /// ```rust
+    /// ```ignore
     /// if x == ptr::null {
     ///     ..
     /// }
@@ -86,7 +89,7 @@
     /// case is unlikely anyway.
     ///
     /// **Example:**
-    /// ```rust
+    /// ```ignore
     /// fn foo(&Foo) -> &mut Bar { .. }
     /// ```
     pub MUT_FROM_REF,
     "fns that create mutable refs from immutable ref args"
 }
 
-#[derive(Copy, Clone)]
-pub struct PointerPass;
+declare_lint_pass!(Ptr => [PTR_ARG, CMP_NULL, MUT_FROM_REF]);
 
-impl LintPass for PointerPass {
-    fn get_lints(&self) -> LintArray {
-        lint_array!(PTR_ARG, CMP_NULL, MUT_FROM_REF)
-    }
-
-    fn name(&self) -> &'static str {
-        "Ptr"
-    }
-}
-
-impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PointerPass {
-    fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
-        if let ItemKind::Fn(ref decl, _, _, body_id) = item.node {
-            check_fn(cx, decl, item.hir_id, Some(body_id));
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Ptr {
+    fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'_>) {
+        if let ItemKind::Fn(ref sig, _, body_id) = item.kind {
+            check_fn(cx, &sig.decl, item.hir_id, Some(body_id));
         }
     }
 
-    fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem) {
-        if let ImplItemKind::Method(ref sig, body_id) = item.node {
+    fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem<'_>) {
+        if let ImplItemKind::Method(ref sig, body_id) = item.kind {
             let parent_item = cx.tcx.hir().get_parent_item(item.hir_id);
-            if let Some(Node::Item(it)) = cx.tcx.hir().find_by_hir_id(parent_item) {
-                if let ItemKind::Impl(_, _, _, _, Some(_), _, _) = it.node {
+            if let Some(Node::Item(it)) = cx.tcx.hir().find(parent_item) {
+                if let ItemKind::Impl { of_trait: Some(_), .. } = it.kind {
                     return; // ignore trait impls
                 }
             }
@@ -126,8 +118,8 @@ fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem)
         }
     }
 
-    fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) {
-        if let TraitItemKind::Method(ref sig, ref trait_method) = item.node {
+    fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem<'_>) {
+        if let TraitItemKind::Method(ref sig, ref trait_method) = item.kind {
             let body_id = if let TraitMethod::Provided(b) = *trait_method {
                 Some(b)
             } else {
@@ -137,14 +129,14 @@ fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem
         }
     }
 
-    fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
-        if let ExprKind::Binary(ref op, ref l, ref r) = expr.node {
+    fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
+        if let ExprKind::Binary(ref op, ref l, ref r) = expr.kind {
             if (op.node == BinOpKind::Eq || op.node == BinOpKind::Ne) && (is_null_path(l) || is_null_path(r)) {
                 span_lint(
                     cx,
                     CMP_NULL,
                     expr.span,
-                    "Comparing with null is better expressed by the .is_null() method",
+                    "Comparing with null is better expressed by the `.is_null()` method",
                 );
             }
         }
@@ -152,17 +144,17 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
 }
 
 #[allow(clippy::too_many_lines)]
-fn check_fn(cx: &LateContext<'_, '_>, decl: &FnDecl, fn_id: HirId, opt_body_id: Option<BodyId>) {
-    let fn_def_id = cx.tcx.hir().local_def_id_from_hir_id(fn_id);
+fn check_fn(cx: &LateContext<'_, '_>, decl: &FnDecl<'_>, fn_id: HirId, opt_body_id: Option<BodyId>) {
+    let fn_def_id = cx.tcx.hir().local_def_id(fn_id);
     let sig = cx.tcx.fn_sig(fn_def_id);
     let fn_ty = sig.skip_binder();
 
     for (idx, (arg, ty)) in decl.inputs.iter().zip(fn_ty.inputs()).enumerate() {
-        if let ty::Ref(_, ty, MutImmutable) = ty.sty {
-            if match_type(cx, ty, &paths::VEC) {
+        if let ty::Ref(_, ty, Mutability::Not) = ty.kind {
+            if is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")) {
                 let mut ty_snippet = None;
                 if_chain! {
-                    if let TyKind::Path(QPath::Resolved(_, ref path)) = walk_ptrs_hir_ty(arg).node;
+                    if let TyKind::Path(QPath::Resolved(_, ref path)) = walk_ptrs_hir_ty(arg).kind;
                     if let Some(&PathSegment{args: Some(ref parameters), ..}) = path.segments.last();
                     then {
                         let types: Vec<_> = parameters.args.iter().filter_map(|arg| match arg {
@@ -227,8 +219,8 @@ fn check_fn(cx: &LateContext<'_, '_>, decl: &FnDecl, fn_id: HirId, opt_body_id:
                 }
             } else if match_type(cx, ty, &paths::COW) {
                 if_chain! {
-                    if let TyKind::Rptr(_, MutTy { ref ty, ..} ) = arg.node;
-                    if let TyKind::Path(ref path) = ty.node;
+                    if let TyKind::Rptr(_, MutTy { ref ty, ..} ) = arg.kind;
+                    if let TyKind::Path(ref path) = ty.kind;
                     if let QPath::Resolved(None, ref pp) = *path;
                     if let [ref bx] = *pp.segments;
                     if let Some(ref params) = bx.args;
@@ -262,7 +254,7 @@ fn check_fn(cx: &LateContext<'_, '_>, decl: &FnDecl, fn_id: HirId, opt_body_id:
     }
 
     if let FunctionRetTy::Return(ref ty) = decl.output {
-        if let Some((out, MutMutable, _)) = get_rptr_lm(ty) {
+        if let Some((out, Mutability::Mut, _)) = get_rptr_lm(ty) {
             let mut immutables = vec![];
             for (_, ref mutbl, ref argspan) in decl
                 .inputs
@@ -270,7 +262,7 @@ fn check_fn(cx: &LateContext<'_, '_>, decl: &FnDecl, fn_id: HirId, opt_body_id:
                 .filter_map(|ty| get_rptr_lm(ty))
                 .filter(|&(lt, _, _)| lt.name == out.name)
             {
-                if *mutbl == MutMutable {
+                if *mutbl == Mutability::Mut {
                     return;
                 }
                 immutables.push(*argspan);
@@ -292,18 +284,18 @@ fn check_fn(cx: &LateContext<'_, '_>, decl: &FnDecl, fn_id: HirId, opt_body_id:
     }
 }
 
-fn get_rptr_lm(ty: &Ty) -> Option<(&Lifetime, Mutability, Span)> {
-    if let TyKind::Rptr(ref lt, ref m) = ty.node {
+fn get_rptr_lm<'tcx>(ty: &'tcx Ty<'tcx>) -> Option<(&'tcx Lifetime, Mutability, Span)> {
+    if let TyKind::Rptr(ref lt, ref m) = ty.kind {
         Some((lt, m.mutbl, ty.span))
     } else {
         None
     }
 }
 
-fn is_null_path(expr: &Expr) -> bool {
-    if let ExprKind::Call(ref pathexp, ref args) = expr.node {
+fn is_null_path(expr: &Expr<'_>) -> bool {
+    if let ExprKind::Call(ref pathexp, ref args) = expr.kind {
         if args.is_empty() {
-            if let ExprKind::Path(ref path) = pathexp.node {
+            if let ExprKind::Path(ref path) = pathexp.kind {
                 return match_qpath(path, &paths::PTR_NULL) || match_qpath(path, &paths::PTR_NULL_MUT);
             }
         }