From fbd25e93a4ab7b7d357452f7c59f846e984b5d74 Mon Sep 17 00:00:00 2001 From: Yoshitomo Nakanishi Date: Fri, 12 Feb 2021 14:11:04 +0900 Subject: [PATCH] Add flags to detect lints are triggered --- clippy_lints/src/types/box_vec.rs | 4 +++- clippy_lints/src/types/mod.rs | 13 +++++++++---- clippy_lints/src/types/rc_buffer.rs | 14 +++++++++----- clippy_lints/src/types/redundant_allocation.rs | 16 +++++++++++----- clippy_lints/src/types/vec_box.rs | 7 ++++++- 5 files changed, 38 insertions(+), 16 deletions(-) diff --git a/clippy_lints/src/types/box_vec.rs b/clippy_lints/src/types/box_vec.rs index 14f09ab837f..4eb032cae6b 100644 --- a/clippy_lints/src/types/box_vec.rs +++ b/clippy_lints/src/types/box_vec.rs @@ -6,7 +6,7 @@ use crate::utils::{is_ty_param_diagnostic_item, span_lint_and_help}; 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` is already on the heap, `Box>` makes an extra allocation", ); + return true; } } + false } diff --git a/clippy_lints/src/types/mod.rs b/clippy_lints/src/types/mod.rs index 8d866568df9..8506f362517 100644 --- a/clippy_lints/src/types/mod.rs +++ b/clippy_lints/src/types/mod.rs @@ -322,10 +322,11 @@ impl Types { 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 @@ impl Types { ); return; // don't recurse into the type } + + if triggered { + return; + } } match *qpath { QPath::Resolved(Some(ref ty), ref p) => { diff --git a/clippy_lints/src/types/rc_buffer.rs b/clippy_lints/src/types/rc_buffer.rs index 11e25c8bdcb..e34b95147e1 100644 --- a/clippy_lints/src/types/rc_buffer.rs +++ b/clippy_lints/src/types/rc_buffer.rs @@ -9,7 +9,7 @@ use crate::utils::{ 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> { diff --git a/clippy_lints/src/types/redundant_allocation.rs b/clippy_lints/src/types/redundant_allocation.rs index 8280b2c5629..ea5a675827a 100644 --- a/clippy_lints/src/types/redundant_allocation.rs +++ b/clippy_lints/src/types/redundant_allocation.rs @@ -10,7 +10,7 @@ use crate::utils::{ 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 } } diff --git a/clippy_lints/src/types/vec_box.rs b/clippy_lints/src/types/vec_box.rs index 2964abf3492..2530cc133c6 100644 --- a/clippy_lints/src/types/vec_box.rs +++ b/clippy_lints/src/types/vec_box.rs @@ -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 } }