@@ -4,6 +4,7 @@ mod box_vec;
4
4
mod rc_buffer;
5
5
mod redundant_allocation;
6
6
mod utils;
7
+ mod vec_box;
7
8
8
9
use std:: borrow:: Cow ;
9
10
use std:: cmp:: Ordering ;
@@ -22,7 +23,6 @@ use rustc_hir::{
22
23
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
23
24
use rustc_middle:: hir:: map:: Map ;
24
25
use rustc_middle:: lint:: in_external_macro;
25
- use rustc_middle:: ty:: TypeFoldable ;
26
26
use rustc_middle:: ty:: { self , FloatTy , InferTy , IntTy , Ty , TyCtxt , TyS , TypeAndMut , TypeckResults , UintTy } ;
27
27
use rustc_semver:: RustcVersion ;
28
28
use rustc_session:: { declare_lint_pass, declare_tool_lint, impl_lint_pass} ;
@@ -38,8 +38,8 @@ use crate::utils::paths;
38
38
use crate :: utils:: sugg:: Sugg ;
39
39
use crate :: utils:: {
40
40
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,
43
43
snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg,
44
44
span_lint_and_then, unsext,
45
45
} ;
@@ -325,44 +325,9 @@ impl Types {
325
325
box_vec:: check ( cx, hir_ty, qpath, def_id) ;
326
326
redundant_allocation:: check ( cx, hir_ty, qpath, def_id) ;
327
327
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) {
366
331
if is_ty_param_diagnostic_item ( cx, qpath, sym:: option_type) . is_some ( ) {
367
332
span_lint (
368
333
cx,
0 commit comments