From df307c0ce73552045e81ee8237d3cb582af0e7e7 Mon Sep 17 00:00:00 2001 From: Yoshitomo Nakanishi Date: Thu, 11 Feb 2021 17:32:46 +0900 Subject: [PATCH] Move box_vec to its own module --- clippy_lints/src/types/box_vec.rs | 22 ++++++++++++++++++++++ clippy_lints/src/types/mod.rs | 14 +++----------- 2 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 clippy_lints/src/types/box_vec.rs diff --git a/clippy_lints/src/types/box_vec.rs b/clippy_lints/src/types/box_vec.rs new file mode 100644 index 000000000000..14f09ab837f3 --- /dev/null +++ b/clippy_lints/src/types/box_vec.rs @@ -0,0 +1,22 @@ +use rustc_hir::{self as hir, def_id::DefId, QPath}; +use rustc_lint::LateContext; +use rustc_span::symbol::sym; + +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) { + 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( + cx, + BOX_VEC, + hir_ty.span, + "you seem to be trying to use `Box>`. Consider using just `Vec`", + None, + "`Vec` is already on the heap, `Box>` makes an extra allocation", + ); + } + } +} diff --git a/clippy_lints/src/types/mod.rs b/clippy_lints/src/types/mod.rs index ce201b956d83..b5ba2c58a81c 100644 --- a/clippy_lints/src/types/mod.rs +++ b/clippy_lints/src/types/mod.rs @@ -1,5 +1,7 @@ #![allow(rustc::default_hash_types)] +mod box_vec; + use std::borrow::Cow; use std::cmp::Ordering; use std::collections::BTreeMap; @@ -346,6 +348,7 @@ 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); if Some(def_id) == cx.tcx.lang_items().owned_box() { if let Some(span) = match_borrows_parameter(cx, qpath) { let mut applicability = Applicability::MachineApplicable; @@ -360,17 +363,6 @@ impl Types { ); return; // don't recurse into the type } - if is_ty_param_diagnostic_item(cx, qpath, sym::vec_type).is_some() { - span_lint_and_help( - cx, - BOX_VEC, - hir_ty.span, - "you seem to be trying to use `Box>`. Consider using just `Vec`", - None, - "`Vec` is already on the heap, `Box>` makes an extra allocation", - ); - return; // don't recurse into the type - } } else if cx.tcx.is_diagnostic_item(sym::Rc, def_id) { if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::Rc) { let mut applicability = Applicability::MachineApplicable;