]> git.lizzy.rs Git - rust.git/commitdiff
Add flags to detect lints are triggered
authorYoshitomo Nakanishi <yurayura.rounin.3@gmail.com>
Fri, 12 Feb 2021 05:11:04 +0000 (14:11 +0900)
committerYoshitomo Nakanishi <yurayura.rounin.3@gmail.com>
Mon, 8 Mar 2021 15:49:04 +0000 (00:49 +0900)
clippy_lints/src/types/box_vec.rs
clippy_lints/src/types/mod.rs
clippy_lints/src/types/rc_buffer.rs
clippy_lints/src/types/redundant_allocation.rs
clippy_lints/src/types/vec_box.rs

index 14f09ab837f34d5a282fec1621c7bdfde85fdf5d..4eb032cae6b1a678b7d1c08319ec071dd00c6f6b 100644 (file)
@@ -6,7 +6,7 @@
 
 use super::BOX_VEC;
 
-pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) {
+pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
     if Some(def_id) == cx.tcx.lang_items().owned_box() {
         if is_ty_param_diagnostic_item(cx, qpath, sym::vec_type).is_some() {
             span_lint_and_help(
@@ -17,6 +17,8 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
                 None,
                 "`Vec<T>` is already on the heap, `Box<Vec<T>>` makes an extra allocation",
             );
+            return true;
         }
     }
+    false
 }
index 8d866568df938fe12abea793bb2cccfc6cc2b075..8506f362517aaa83a610b85b47785d59cef96708 100644 (file)
@@ -322,10 +322,11 @@ fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, is_local: boo
                 let hir_id = hir_ty.hir_id;
                 let res = cx.qpath_res(qpath, hir_id);
                 if let Some(def_id) = res.opt_def_id() {
-                    box_vec::check(cx, hir_ty, qpath, def_id);
-                    redundant_allocation::check(cx, hir_ty, qpath, def_id);
-                    rc_buffer::check(cx, hir_ty, qpath, def_id);
-                    vec_box::check(cx, hir_ty, qpath, def_id, self.vec_box_size_threshold);
+                    let mut triggered = false;
+                    triggered |= box_vec::check(cx, hir_ty, qpath, def_id);
+                    triggered |= redundant_allocation::check(cx, hir_ty, qpath, def_id);
+                    triggered |= rc_buffer::check(cx, hir_ty, qpath, def_id);
+                    triggered |= vec_box::check(cx, hir_ty, qpath, def_id, self.vec_box_size_threshold);
 
                     if cx.tcx.is_diagnostic_item(sym::option_type, def_id) {
                         if is_ty_param_diagnostic_item(cx, qpath, sym::option_type).is_some() {
@@ -349,6 +350,10 @@ enum if you need to distinguish all 3 cases",
                         );
                         return; // don't recurse into the type
                     }
+
+                    if triggered {
+                        return;
+                    }
                 }
                 match *qpath {
                     QPath::Resolved(Some(ref ty), ref p) => {
index 11e25c8bdcb0d11c08b564c7408a46fe12c5d88c..e34b95147e10a6d4cf2fb2c06734b9d1459a71d2 100644 (file)
@@ -9,7 +9,7 @@
 
 use super::RC_BUFFER;
 
-pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) {
+pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
     if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
         if let Some(alternate) = match_buffer_type(cx, qpath) {
             span_lint_and_sugg(
@@ -24,11 +24,11 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
         } else if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::vec_type) {
             let qpath = match &ty.kind {
                 TyKind::Path(qpath) => qpath,
-                _ => return,
+                _ => return false,
             };
             let inner_span = match get_qpath_generic_tys(qpath).next() {
                 Some(ty) => ty.span,
-                None => return,
+                None => return false,
             };
             let mut applicability = Applicability::MachineApplicable;
             span_lint_and_sugg(
@@ -43,6 +43,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
                 ),
                 Applicability::MachineApplicable,
             );
+            return true;
         }
     } else if cx.tcx.is_diagnostic_item(sym::Arc, def_id) {
         if let Some(alternate) = match_buffer_type(cx, qpath) {
@@ -58,11 +59,11 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
         } else if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::vec_type) {
             let qpath = match &ty.kind {
                 TyKind::Path(qpath) => qpath,
-                _ => return,
+                _ => return false,
             };
             let inner_span = match get_qpath_generic_tys(qpath).next() {
                 Some(ty) => ty.span,
-                None => return,
+                None => return false,
             };
             let mut applicability = Applicability::MachineApplicable;
             span_lint_and_sugg(
@@ -77,8 +78,11 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
                 ),
                 Applicability::MachineApplicable,
             );
+            return true;
         }
     }
+
+    false
 }
 
 fn match_buffer_type(cx: &LateContext<'_>, qpath: &QPath<'_>) -> Option<&'static str> {
index 8280b2c5629cfbd2635129f2077d8a7affc14889..ea5a675827ab08e3f75d4ebda4733ccf4e8a2d7c 100644 (file)
@@ -10,7 +10,7 @@
 
 use super::{utils, REDUNDANT_ALLOCATION};
 
-pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) {
+pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool {
     if Some(def_id) == cx.tcx.lang_items().owned_box() {
         if let Some(span) = utils::match_borrows_parameter(cx, qpath) {
             let mut applicability = Applicability::MachineApplicable;
@@ -23,7 +23,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
                 snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
                 applicability,
             );
-            return;
+            return true;
         }
     }
 
@@ -39,14 +39,15 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
                 snippet_with_applicability(cx, ty.span, "..", &mut applicability).to_string(),
                 applicability,
             );
+            true
         } else if let Some(ty) = is_ty_param_lang_item(cx, qpath, LangItem::OwnedBox) {
             let qpath = match &ty.kind {
                 TyKind::Path(qpath) => qpath,
-                _ => return,
+                _ => return false,
             };
             let inner_span = match get_qpath_generic_tys(qpath).next() {
                 Some(ty) => ty.span,
-                None => return,
+                None => return false,
             };
             let mut applicability = Applicability::MachineApplicable;
             span_lint_and_sugg(
@@ -61,6 +62,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
                 ),
                 applicability,
             );
+            true
         } else if let Some(span) = utils::match_borrows_parameter(cx, qpath) {
             let mut applicability = Applicability::MachineApplicable;
             span_lint_and_sugg(
@@ -72,7 +74,11 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_
                 snippet_with_applicability(cx, span, "..", &mut applicability).to_string(),
                 applicability,
             );
-            return; // don't recurse into the type
+            true
+        } else {
+            false
         }
+    } else {
+        false
     }
 }
index 2964abf3492eb548433a6cba9344ca05fd0c1117..2530cc133c6784ec04da443d68894b6b5b2bc0fd 100644 (file)
@@ -18,7 +18,7 @@ pub(super) fn check(
     qpath: &QPath<'_>,
     def_id: DefId,
     box_size_threshold: u64,
-) {
+) -> bool {
     if cx.tcx.is_diagnostic_item(sym::vec_type, def_id) {
         if_chain! {
             // Get the _ part of Vec<_>
@@ -53,7 +53,12 @@ pub(super) fn check(
                     format!("Vec<{}>", snippet(cx, boxed_ty.span, "..")),
                     Applicability::MachineApplicable,
                 );
+                true
+            } else {
+                false
             }
         }
+    } else {
+        false
     }
 }