Move box_vec to its own module

This commit is contained in:
Yoshitomo Nakanishi 2021-02-11 17:32:46 +09:00
parent 714a826439
commit df307c0ce7
2 changed files with 25 additions and 11 deletions

View File

@ -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<Vec<T>>`. Consider using just `Vec<T>`",
None,
"`Vec<T>` is already on the heap, `Box<Vec<T>>` makes an extra allocation",
);
}
}
}

View File

@ -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<Vec<T>>`. Consider using just `Vec<T>`",
None,
"`Vec<T>` is already on the heap, `Box<Vec<T>>` 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;