Skip to content

Commit df307c0

Browse files
committed
Move box_vec to its own module
1 parent 714a826 commit df307c0

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

clippy_lints/src/types/box_vec.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use rustc_hir::{self as hir, def_id::DefId, QPath};
2+
use rustc_lint::LateContext;
3+
use rustc_span::symbol::sym;
4+
5+
use crate::utils::{is_ty_param_diagnostic_item, span_lint_and_help};
6+
7+
use super::BOX_VEC;
8+
9+
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) {
10+
if Some(def_id) == cx.tcx.lang_items().owned_box() {
11+
if is_ty_param_diagnostic_item(cx, qpath, sym::vec_type).is_some() {
12+
span_lint_and_help(
13+
cx,
14+
BOX_VEC,
15+
hir_ty.span,
16+
"you seem to be trying to use `Box<Vec<T>>`. Consider using just `Vec<T>`",
17+
None,
18+
"`Vec<T>` is already on the heap, `Box<Vec<T>>` makes an extra allocation",
19+
);
20+
}
21+
}
22+
}

clippy_lints/src/types/mod.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![allow(rustc::default_hash_types)]
22

3+
mod box_vec;
4+
35
use std::borrow::Cow;
46
use std::cmp::Ordering;
57
use std::collections::BTreeMap;
@@ -346,6 +348,7 @@ impl Types {
346348
let hir_id = hir_ty.hir_id;
347349
let res = cx.qpath_res(qpath, hir_id);
348350
if let Some(def_id) = res.opt_def_id() {
351+
box_vec::check(cx, hir_ty, qpath, def_id);
349352
if Some(def_id) == cx.tcx.lang_items().owned_box() {
350353
if let Some(span) = match_borrows_parameter(cx, qpath) {
351354
let mut applicability = Applicability::MachineApplicable;
@@ -360,17 +363,6 @@ impl Types {
360363
);
361364
return; // don't recurse into the type
362365
}
363-
if is_ty_param_diagnostic_item(cx, qpath, sym::vec_type).is_some() {
364-
span_lint_and_help(
365-
cx,
366-
BOX_VEC,
367-
hir_ty.span,
368-
"you seem to be trying to use `Box<Vec<T>>`. Consider using just `Vec<T>`",
369-
None,
370-
"`Vec<T>` is already on the heap, `Box<Vec<T>>` makes an extra allocation",
371-
);
372-
return; // don't recurse into the type
373-
}
374366
} else if cx.tcx.is_diagnostic_item(sym::Rc, def_id) {
375367
if let Some(ty) = is_ty_param_diagnostic_item(cx, qpath, sym::Rc) {
376368
let mut applicability = Applicability::MachineApplicable;

0 commit comments

Comments
 (0)