@@ -19,61 +19,58 @@ pub(super) fn check<'tcx>(
1919 def_id : DefId ,
2020 box_size_threshold : u64 ,
2121) -> bool {
22- if cx. tcx . is_diagnostic_item ( sym:: Vec , def_id) {
23- if let Some ( last) = last_path_segment ( qpath) . args
24- // Get the _ part of Vec<_>
25- && let Some ( GenericArg :: Type ( ty) ) = last. args . first ( )
26- // extract allocator from the Vec for later
27- && let vec_alloc_ty = last. args . get ( 1 )
28- // ty is now _ at this point
29- && let TyKind :: Path ( ref ty_qpath) = ty. kind
30- && let res = cx. qpath_res ( ty_qpath, ty. hir_id )
31- && let Some ( def_id) = res. opt_def_id ( )
32- && Some ( def_id) == cx. tcx . lang_items ( ) . owned_box ( )
33- // At this point, we know ty is Box<T>, now get T
34- && let Some ( last) = last_path_segment ( ty_qpath) . args
35- && let Some ( GenericArg :: Type ( boxed_ty) ) = last. args . first ( )
36- // extract allocator from the Box for later
37- && let boxed_alloc_ty = last. args . get ( 1 )
38- // we don't expect to encounter `_` here so ignore `GenericArg::Infer` is okay
39- && let ty_ty = lower_ty ( cx. tcx , boxed_ty. as_unambig_ty ( ) )
40- && !ty_ty. has_escaping_bound_vars ( )
41- && ty_ty. is_sized ( cx. tcx , cx. typing_env ( ) )
42- && let Ok ( ty_ty_size) = cx. layout_of ( ty_ty) . map ( |l| l. size . bytes ( ) )
43- && ty_ty_size < box_size_threshold
44- // https://github.com/rust-lang/rust-clippy/issues/7114
45- && match ( vec_alloc_ty, boxed_alloc_ty) {
46- ( None , None ) => true ,
47- // this is in the event that we have something like
48- // Vec<_, Global>, in which case is equivalent to
49- // Vec<_>
50- ( None , Some ( GenericArg :: Type ( inner) ) ) | ( Some ( GenericArg :: Type ( inner) ) , None ) => {
51- if let TyKind :: Path ( path) = inner. kind
52- && let Some ( did) = cx. qpath_res ( & path, inner. hir_id ) . opt_def_id ( ) {
53- cx. tcx . lang_items ( ) . get ( LangItem :: GlobalAlloc ) == Some ( did)
54- } else {
55- false
56- }
57- } ,
58- ( Some ( GenericArg :: Type ( l) ) , Some ( GenericArg :: Type ( r) ) ) =>
59- // we don't expect to encounter `_` here so ignore `GenericArg::Infer` is okay
60- lower_ty ( cx. tcx , l. as_unambig_ty ( ) ) == lower_ty ( cx. tcx , r. as_unambig_ty ( ) ) ,
61- _ => false
62- }
63- {
64- span_lint_and_sugg (
65- cx,
66- VEC_BOX ,
67- hir_ty. span ,
68- "`Vec<T>` is already on the heap, the boxing is unnecessary" ,
69- "try" ,
70- format ! ( "Vec<{}>" , snippet( cx, boxed_ty. span, ".." ) ) ,
71- Applicability :: Unspecified ,
72- ) ;
73- true
74- } else {
75- false
22+ if cx. tcx . is_diagnostic_item ( sym:: Vec , def_id)
23+ && let Some ( last) = last_path_segment ( qpath) . args
24+ // Get the _ part of Vec<_>
25+ && let Some ( GenericArg :: Type ( ty) ) = last. args . first ( )
26+ // extract allocator from the Vec for later
27+ && let vec_alloc_ty = last. args . get ( 1 )
28+ // ty is now _ at this point
29+ && let TyKind :: Path ( ref ty_qpath) = ty. kind
30+ && let res = cx. qpath_res ( ty_qpath, ty. hir_id )
31+ && let Some ( def_id) = res. opt_def_id ( )
32+ && Some ( def_id) == cx. tcx . lang_items ( ) . owned_box ( )
33+ // At this point, we know ty is Box<T>, now get T
34+ && let Some ( last) = last_path_segment ( ty_qpath) . args
35+ && let Some ( GenericArg :: Type ( boxed_ty) ) = last. args . first ( )
36+ // extract allocator from the Box for later
37+ && let boxed_alloc_ty = last. args . get ( 1 )
38+ // we don't expect to encounter `_` here so ignore `GenericArg::Infer` is okay
39+ && let ty_ty = lower_ty ( cx. tcx , boxed_ty. as_unambig_ty ( ) )
40+ && !ty_ty. has_escaping_bound_vars ( )
41+ && ty_ty. is_sized ( cx. tcx , cx. typing_env ( ) )
42+ && let Ok ( ty_ty_size) = cx. layout_of ( ty_ty) . map ( |l| l. size . bytes ( ) )
43+ && ty_ty_size < box_size_threshold
44+ // https://github.com/rust-lang/rust-clippy/issues/7114
45+ && match ( vec_alloc_ty, boxed_alloc_ty) {
46+ ( None , None ) => true ,
47+ // this is in the event that we have something like
48+ // Vec<_, Global>, in which case is equivalent to
49+ // Vec<_>
50+ ( None , Some ( GenericArg :: Type ( inner) ) ) | ( Some ( GenericArg :: Type ( inner) ) , None ) => {
51+ if let TyKind :: Path ( path) = inner. kind
52+ && let Some ( did) = cx. qpath_res ( & path, inner. hir_id ) . opt_def_id ( ) {
53+ cx. tcx . lang_items ( ) . get ( LangItem :: GlobalAlloc ) == Some ( did)
54+ } else {
55+ false
56+ }
57+ } ,
58+ ( Some ( GenericArg :: Type ( l) ) , Some ( GenericArg :: Type ( r) ) ) =>
59+ // we don't expect to encounter `_` here so ignore `GenericArg::Infer` is okay
60+ lower_ty ( cx. tcx , l. as_unambig_ty ( ) ) == lower_ty ( cx. tcx , r. as_unambig_ty ( ) ) ,
61+ _ => false
7662 }
63+ {
64+ span_lint_and_sugg (
65+ cx,
66+ VEC_BOX ,
67+ hir_ty. span ,
68+ "`Vec<T>` is already on the heap, the boxing is unnecessary" ,
69+ "try" ,
70+ format ! ( "Vec<{}>" , snippet( cx, boxed_ty. span, ".." ) ) ,
71+ Applicability :: Unspecified ,
72+ ) ;
73+ true
7774 } else {
7875 false
7976 }
0 commit comments