Skip to content

Commit 2c2fb39

Browse files
committed
Move vec_box to its own module
1 parent f110c5e commit 2c2fb39

File tree

2 files changed

+65
-41
lines changed

2 files changed

+65
-41
lines changed

clippy_lints/src/types/mod.rs

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod box_vec;
44
mod rc_buffer;
55
mod redundant_allocation;
66
mod utils;
7+
mod vec_box;
78

89
use std::borrow::Cow;
910
use std::cmp::Ordering;
@@ -22,7 +23,6 @@ use rustc_hir::{
2223
use rustc_lint::{LateContext, LateLintPass, LintContext};
2324
use rustc_middle::hir::map::Map;
2425
use rustc_middle::lint::in_external_macro;
25-
use rustc_middle::ty::TypeFoldable;
2626
use rustc_middle::ty::{self, FloatTy, InferTy, IntTy, Ty, TyCtxt, TyS, TypeAndMut, TypeckResults, UintTy};
2727
use rustc_semver::RustcVersion;
2828
use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
@@ -38,8 +38,8 @@ use crate::utils::paths;
3838
use crate::utils::sugg::Sugg;
3939
use crate::utils::{
4040
clip, comparisons, differing_macro_contexts, higher, in_constant, indent_of, int_bits, is_hir_ty_cfg_dependant,
41-
is_ty_param_diagnostic_item, is_type_diagnostic_item, last_path_segment, match_def_path, match_path, meets_msrv,
42-
method_chain_args, multispan_sugg, numeric_literal::NumericLiteral, reindent_multiline, sext, snippet, snippet_opt,
41+
is_ty_param_diagnostic_item, is_type_diagnostic_item, match_def_path, match_path, meets_msrv, method_chain_args,
42+
multispan_sugg, numeric_literal::NumericLiteral, reindent_multiline, sext, snippet, snippet_opt,
4343
snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg,
4444
span_lint_and_then, unsext,
4545
};
@@ -325,44 +325,9 @@ impl Types {
325325
box_vec::check(cx, hir_ty, qpath, def_id);
326326
redundant_allocation::check(cx, hir_ty, qpath, def_id);
327327
rc_buffer::check(cx, hir_ty, qpath, def_id);
328-
if cx.tcx.is_diagnostic_item(sym::vec_type, def_id) {
329-
if_chain! {
330-
// Get the _ part of Vec<_>
331-
if let Some(ref last) = last_path_segment(qpath).args;
332-
if let Some(ty) = last.args.iter().find_map(|arg| match arg {
333-
GenericArg::Type(ty) => Some(ty),
334-
_ => None,
335-
});
336-
// ty is now _ at this point
337-
if let TyKind::Path(ref ty_qpath) = ty.kind;
338-
let res = cx.qpath_res(ty_qpath, ty.hir_id);
339-
if let Some(def_id) = res.opt_def_id();
340-
if Some(def_id) == cx.tcx.lang_items().owned_box();
341-
// At this point, we know ty is Box<T>, now get T
342-
if let Some(ref last) = last_path_segment(ty_qpath).args;
343-
if let Some(boxed_ty) = last.args.iter().find_map(|arg| match arg {
344-
GenericArg::Type(ty) => Some(ty),
345-
_ => None,
346-
});
347-
let ty_ty = hir_ty_to_ty(cx.tcx, boxed_ty);
348-
if !ty_ty.has_escaping_bound_vars();
349-
if ty_ty.is_sized(cx.tcx.at(ty.span), cx.param_env);
350-
if let Ok(ty_ty_size) = cx.layout_of(ty_ty).map(|l| l.size.bytes());
351-
if ty_ty_size <= self.vec_box_size_threshold;
352-
then {
353-
span_lint_and_sugg(
354-
cx,
355-
VEC_BOX,
356-
hir_ty.span,
357-
"`Vec<T>` is already on the heap, the boxing is unnecessary",
358-
"try",
359-
format!("Vec<{}>", snippet(cx, boxed_ty.span, "..")),
360-
Applicability::MachineApplicable,
361-
);
362-
return; // don't recurse into the type
363-
}
364-
}
365-
} else if cx.tcx.is_diagnostic_item(sym::option_type, def_id) {
328+
vec_box::check(cx, hir_ty, qpath, def_id, self.vec_box_size_threshold);
329+
330+
if cx.tcx.is_diagnostic_item(sym::option_type, def_id) {
366331
if is_ty_param_diagnostic_item(cx, qpath, sym::option_type).is_some() {
367332
span_lint(
368333
cx,

clippy_lints/src/types/vec_box.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use rustc_errors::Applicability;
2+
use rustc_hir::{self as hir, def_id::DefId, GenericArg, QPath, TyKind};
3+
use rustc_lint::LateContext;
4+
use rustc_middle::ty::TypeFoldable;
5+
use rustc_span::symbol::sym;
6+
use rustc_target::abi::LayoutOf;
7+
use rustc_typeck::hir_ty_to_ty;
8+
9+
use if_chain::if_chain;
10+
11+
use crate::utils::{last_path_segment, snippet, span_lint_and_sugg};
12+
13+
use super::VEC_BOX;
14+
15+
pub(super) fn check(
16+
cx: &LateContext<'_>,
17+
hir_ty: &hir::Ty<'_>,
18+
qpath: &QPath<'_>,
19+
def_id: DefId,
20+
box_size_threshold: u64,
21+
) {
22+
if cx.tcx.is_diagnostic_item(sym::vec_type, def_id) {
23+
if_chain! {
24+
// Get the _ part of Vec<_>
25+
if let Some(ref last) = last_path_segment(qpath).args;
26+
if let Some(ty) = last.args.iter().find_map(|arg| match arg {
27+
GenericArg::Type(ty) => Some(ty),
28+
_ => None,
29+
});
30+
// ty is now _ at this point
31+
if let TyKind::Path(ref ty_qpath) = ty.kind;
32+
let res = cx.qpath_res(ty_qpath, ty.hir_id);
33+
if let Some(def_id) = res.opt_def_id();
34+
if Some(def_id) == cx.tcx.lang_items().owned_box();
35+
// At this point, we know ty is Box<T>, now get T
36+
if let Some(ref last) = last_path_segment(ty_qpath).args;
37+
if let Some(boxed_ty) = last.args.iter().find_map(|arg| match arg {
38+
GenericArg::Type(ty) => Some(ty),
39+
_ => None,
40+
});
41+
let ty_ty = hir_ty_to_ty(cx.tcx, boxed_ty);
42+
if !ty_ty.has_escaping_bound_vars();
43+
if ty_ty.is_sized(cx.tcx.at(ty.span), cx.param_env);
44+
if let Ok(ty_ty_size) = cx.layout_of(ty_ty).map(|l| l.size.bytes());
45+
if ty_ty_size <= box_size_threshold;
46+
then {
47+
span_lint_and_sugg(
48+
cx,
49+
VEC_BOX,
50+
hir_ty.span,
51+
"`Vec<T>` is already on the heap, the boxing is unnecessary",
52+
"try",
53+
format!("Vec<{}>", snippet(cx, boxed_ty.span, "..")),
54+
Applicability::MachineApplicable,
55+
);
56+
}
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)