]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/types.rs
Make vec_box MachineApplicable
[rust.git] / clippy_lints / src / types.rs
index 898fd5a98089bcec33ec46f323146658b4657c2a..6ee7d10155cd602d96fcd1489d1462c4ec29b447 100644 (file)
@@ -168,6 +168,10 @@ impl LintPass for TypePass {
     fn get_lints(&self) -> LintArray {
         lint_array!(BOX_VEC, VEC_BOX, OPTION_OPTION, LINKEDLIST, BORROWED_BOX)
     }
+
+    fn name(&self) -> &'static str {
+        "Types"
+    }
 }
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypePass {
@@ -182,7 +186,7 @@ fn check_fn(&mut self, cx: &LateContext<'_, '_>, _: FnKind<'_>, decl: &FnDecl, _
         check_fn_decl(cx, decl);
     }
 
-    fn check_struct_field(&mut self, cx: &LateContext<'_, '_>, field: &StructField) {
+    fn check_struct_field(&mut self, cx: &LateContext<'_, '_>, field: &hir::StructField) {
         check_ty(cx, &field.ty, false);
     }
 
@@ -236,13 +240,13 @@ fn match_type_parameter(cx: &LateContext<'_, '_>, qpath: &QPath, path: &[&str])
 ///
 /// The parameter `is_local` distinguishes the context of the type; types from
 /// local bindings should only be checked for the `BORROWED_BOX` lint.
-fn check_ty(cx: &LateContext<'_, '_>, ast_ty: &hir::Ty, is_local: bool) {
-    if in_macro(ast_ty.span) {
+fn check_ty(cx: &LateContext<'_, '_>, hir_ty: &hir::Ty, is_local: bool) {
+    if in_macro(hir_ty.span) {
         return;
     }
-    match ast_ty.node {
+    match hir_ty.node {
         TyKind::Path(ref qpath) if !is_local => {
-            let hir_id = cx.tcx.hir().node_to_hir_id(ast_ty.id);
+            let hir_id = cx.tcx.hir().node_to_hir_id(hir_ty.id);
             let def = cx.tables.qpath_def(qpath, hir_id);
             if let Some(def_id) = opt_def_id(def) {
                 if Some(def_id) == cx.tcx.lang_items().owned_box() {
@@ -250,7 +254,7 @@ fn check_ty(cx: &LateContext<'_, '_>, ast_ty: &hir::Ty, is_local: bool) {
                         span_help_and_lint(
                             cx,
                             BOX_VEC,
-                            ast_ty.span,
+                            hir_ty.span,
                             "you seem to be trying to use `Box<Vec<T>>`. Consider using just `Vec<T>`",
                             "`Vec<T>` is already on the heap, `Box<Vec<T>>` makes an extra allocation.",
                         );
@@ -271,26 +275,24 @@ fn check_ty(cx: &LateContext<'_, '_>, ast_ty: &hir::Ty, is_local: bool) {
                         if Some(def_id) == cx.tcx.lang_items().owned_box();
                         // At this point, we know ty is Box<T>, now get T
                         if let Some(ref last) = last_path_segment(ty_qpath).args;
-                        if let Some(ty) = last.args.iter().find_map(|arg| match arg {
+                        if let Some(boxed_ty) = last.args.iter().find_map(|arg| match arg {
                             GenericArg::Type(ty) => Some(ty),
                             GenericArg::Lifetime(_) => None,
                         });
-                        if let TyKind::Path(ref ty_qpath) = ty.node;
-                        let def = cx.tables.qpath_def(ty_qpath, ty.hir_id);
-                        if let Some(def_id) = opt_def_id(def);
-                        let boxed_type = cx.tcx.type_of(def_id);
-                        if boxed_type.is_sized(cx.tcx.at(ty.span), cx.param_env);
                         then {
-                            span_lint_and_sugg(
-                                cx,
-                                VEC_BOX,
-                                ast_ty.span,
-                                "`Vec<T>` is already on the heap, the boxing is unnecessary.",
-                                "try",
-                                format!("Vec<{}>", boxed_type),
-                                Applicability::MaybeIncorrect,
-                            );
-                            return; // don't recurse into the type
+                            let ty_ty = hir_ty_to_ty(cx.tcx, boxed_ty);
+                            if ty_ty.is_sized(cx.tcx.at(ty.span), cx.param_env) {
+                                span_lint_and_sugg(
+                                    cx,
+                                    VEC_BOX,
+                                    hir_ty.span,
+                                    "`Vec<T>` is already on the heap, the boxing is unnecessary.",
+                                    "try",
+                                    format!("Vec<{}>", ty_ty),
+                                    Applicability::MachineApplicable,
+                                );
+                                return; // don't recurse into the type
+                            }
                         }
                     }
                 } else if match_def_path(cx.tcx, def_id, &paths::OPTION) {
@@ -298,7 +300,7 @@ fn check_ty(cx: &LateContext<'_, '_>, ast_ty: &hir::Ty, is_local: bool) {
                         span_lint(
                             cx,
                             OPTION_OPTION,
-                            ast_ty.span,
+                            hir_ty.span,
                             "consider using `Option<T>` instead of `Option<Option<T>>` or a custom \
                              enum if you need to distinguish all 3 cases",
                         );
@@ -308,7 +310,7 @@ enum if you need to distinguish all 3 cases",
                     span_help_and_lint(
                         cx,
                         LINKEDLIST,
-                        ast_ty.span,
+                        hir_ty.span,
                         "I see you're using a LinkedList! Perhaps you meant some other data structure?",
                         "a VecDeque might work",
                     );
@@ -356,7 +358,7 @@ enum if you need to distinguish all 3 cases",
                 },
             }
         },
-        TyKind::Rptr(ref lt, ref mut_ty) => check_ty_rptr(cx, ast_ty, is_local, lt, mut_ty),
+        TyKind::Rptr(ref lt, ref mut_ty) => check_ty_rptr(cx, hir_ty, is_local, lt, mut_ty),
         // recurse
         TyKind::Slice(ref ty) | TyKind::Array(ref ty, _) | TyKind::Ptr(MutTy { ref ty, .. }) => {
             check_ty(cx, ty, is_local)
@@ -370,7 +372,7 @@ enum if you need to distinguish all 3 cases",
     }
 }
 
-fn check_ty_rptr(cx: &LateContext<'_, '_>, ast_ty: &hir::Ty, is_local: bool, lt: &Lifetime, mut_ty: &MutTy) {
+fn check_ty_rptr(cx: &LateContext<'_, '_>, hir_ty: &hir::Ty, is_local: bool, lt: &Lifetime, mut_ty: &MutTy) {
     match mut_ty.ty.node {
         TyKind::Path(ref qpath) => {
             let hir_id = cx.tcx.hir().node_to_hir_id(mut_ty.ty.id);
@@ -406,7 +408,7 @@ fn check_ty_rptr(cx: &LateContext<'_, '_>, ast_ty: &hir::Ty, is_local: bool, lt:
                     span_lint_and_sugg(
                         cx,
                         BORROWED_BOX,
-                        ast_ty.span,
+                        hir_ty.span,
                         "you seem to be trying to use `&Box<T>`. Consider using just `&T`",
                         "try",
                         format!(
@@ -467,6 +469,10 @@ impl LintPass for LetPass {
     fn get_lints(&self) -> LintArray {
         lint_array!(LET_UNIT_VALUE)
     }
+
+    fn name(&self) -> &'static str {
+        "LetUnitValue"
+    }
 }
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetPass {
@@ -531,6 +537,10 @@ impl LintPass for UnitCmp {
     fn get_lints(&self) -> LintArray {
         lint_array!(UNIT_CMP)
     }
+
+    fn name(&self) -> &'static str {
+        "UnicCmp"
+    }
 }
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitCmp {
@@ -586,6 +596,10 @@ impl LintPass for UnitArg {
     fn get_lints(&self) -> LintArray {
         lint_array!(UNIT_ARG)
     }
+
+    fn name(&self) -> &'static str {
+        "UnitArg"
+    }
 }
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnitArg {
@@ -593,36 +607,43 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
         if in_macro(expr.span) {
             return;
         }
+
+        // apparently stuff in the desugaring of `?` can trigger this
+        // so check for that here
+        // only the calls to `Try::from_error` is marked as desugared,
+        // so we need to check both the current Expr and its parent.
+        if is_questionmark_desugar_marked_call(expr) {
+            return;
+        }
+        if_chain! {
+            let map = &cx.tcx.hir();
+            let opt_parent_node = map.find(map.get_parent_node(expr.id));
+            if let Some(hir::Node::Expr(parent_expr)) = opt_parent_node;
+            if is_questionmark_desugar_marked_call(parent_expr);
+            then {
+                return;
+            }
+        }
+
         match expr.node {
             ExprKind::Call(_, ref args) | ExprKind::MethodCall(_, _, ref args) => {
                 for arg in args {
                     if is_unit(cx.tables.expr_ty(arg)) && !is_unit_literal(arg) {
-                        let map = &cx.tcx.hir();
-                        // apparently stuff in the desugaring of `?` can trigger this
-                        // so check for that here
-                        // only the calls to `Try::from_error` is marked as desugared,
-                        // so we need to check both the current Expr and its parent.
-                        if !is_questionmark_desugar_marked_call(expr) {
-                            if_chain! {
-                                let opt_parent_node = map.find(map.get_parent_node(expr.id));
-                                if let Some(hir::Node::Expr(parent_expr)) = opt_parent_node;
-                                if is_questionmark_desugar_marked_call(parent_expr);
-                                then {}
-                                else {
-                                    // `expr` and `parent_expr` where _both_ not from
-                                    // desugaring `?`, so lint
-                                    span_lint_and_sugg(
-                                        cx,
-                                        UNIT_ARG,
-                                        arg.span,
-                                        "passing a unit value to a function",
-                                        "if you intended to pass a unit value, use a unit literal instead",
-                                        "()".to_string(),
-                                        Applicability::MachineApplicable,
-                                    );
-                                }
+                        if let ExprKind::Match(.., match_source) = &arg.node {
+                            if *match_source == MatchSource::TryDesugar {
+                                continue;
                             }
                         }
+
+                        span_lint_and_sugg(
+                            cx,
+                            UNIT_ARG,
+                            arg.span,
+                            "passing a unit value to a function",
+                            "if you intended to pass a unit value, use a unit literal instead",
+                            "()".to_string(),
+                            Applicability::MachineApplicable,
+                        );
                     }
                 }
             },
@@ -1073,6 +1094,10 @@ fn get_lints(&self) -> LintArray {
             FN_TO_NUMERIC_CAST_WITH_TRUNCATION,
         )
     }
+
+    fn name(&self) -> &'static str {
+        "Casts"
+    }
 }
 
 // Check if the given type is either `core::ffi::c_void` or
@@ -1278,6 +1303,10 @@ impl LintPass for TypeComplexityPass {
     fn get_lints(&self) -> LintArray {
         lint_array!(TYPE_COMPLEXITY)
     }
+
+    fn name(&self) -> &'static str {
+        "TypeComplexityPass"
+    }
 }
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeComplexityPass {
@@ -1293,7 +1322,7 @@ fn check_fn(
         self.check_fndecl(cx, decl);
     }
 
-    fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, field: &'tcx StructField) {
+    fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, field: &'tcx hir::StructField) {
         // enum variants are also struct fields now
         self.check_type(cx, &field.ty);
     }
@@ -1442,6 +1471,10 @@ impl LintPass for CharLitAsU8 {
     fn get_lints(&self) -> LintArray {
         lint_array!(CHAR_LIT_AS_U8)
     }
+
+    fn name(&self) -> &'static str {
+        "CharLiteralAsU8"
+    }
 }
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CharLitAsU8 {
@@ -1500,6 +1533,10 @@ impl LintPass for AbsurdExtremeComparisons {
     fn get_lints(&self) -> LintArray {
         lint_array!(ABSURD_EXTREME_COMPARISONS)
     }
+
+    fn name(&self) -> &'static str {
+        "AbsurdExtremeComparisons"
+    }
 }
 
 enum ExtremeType {
@@ -1675,6 +1712,10 @@ impl LintPass for InvalidUpcastComparisons {
     fn get_lints(&self) -> LintArray {
         lint_array!(INVALID_UPCAST_COMPARISONS)
     }
+
+    fn name(&self) -> &'static str {
+        "InvalidUpcastComparisons"
+    }
 }
 
 #[derive(Copy, Clone, Debug, Eq)]
@@ -1918,6 +1959,10 @@ impl LintPass for ImplicitHasher {
     fn get_lints(&self) -> LintArray {
         lint_array!(IMPLICIT_HASHER)
     }
+
+    fn name(&self) -> &'static str {
+        "ImplicitHasher"
+    }
 }
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitHasher {
@@ -2265,6 +2310,10 @@ impl LintPass for RefToMut {
     fn get_lints(&self) -> LintArray {
         lint_array!(CAST_REF_TO_MUT)
     }
+
+    fn name(&self) -> &'static str {
+        "RefToMut"
+    }
 }
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RefToMut {