11use clippy_utils:: diagnostics:: span_lint_and_sugg;
22use clippy_utils:: last_path_segment;
33use clippy_utils:: source:: snippet;
4- use if_chain:: if_chain;
54use rustc_errors:: Applicability ;
65use rustc_hir:: def_id:: DefId ;
76use rustc_hir:: { self as hir, GenericArg , QPath , TyKind } ;
@@ -21,45 +20,37 @@ pub(super) fn check(
2120 box_size_threshold : u64 ,
2221) -> bool {
2322 if cx. tcx . is_diagnostic_item ( sym:: Vec , def_id) {
24- if_chain ! {
23+ if let Some ( last ) = last_path_segment ( qpath ) . args
2524 // Get the _ part of Vec<_>
26- if let Some ( last) = last_path_segment( qpath) . args;
27- if let Some ( ty) = last. args. iter( ) . find_map( |arg| match arg {
28- GenericArg :: Type ( ty) => Some ( ty) ,
29- _ => None ,
30- } ) ;
25+ && let Some ( GenericArg :: Type ( ty) ) = last. args . first ( )
3126 // ty is now _ at this point
32- if let TyKind :: Path ( ref ty_qpath) = ty. kind;
33- let res = cx. qpath_res( ty_qpath, ty. hir_id) ;
34- if let Some ( def_id) = res. opt_def_id( ) ;
35- if Some ( def_id) == cx. tcx. lang_items( ) . owned_box( ) ;
27+ && let TyKind :: Path ( ref ty_qpath) = ty. kind
28+ && let res = cx. qpath_res ( ty_qpath, ty. hir_id )
29+ && let Some ( def_id) = res. opt_def_id ( )
30+ && Some ( def_id) == cx. tcx . lang_items ( ) . owned_box ( )
3631 // At this point, we know ty is Box<T>, now get T
37- if let Some ( last) = last_path_segment( ty_qpath) . args;
38- if let Some ( boxed_ty) = last. args. iter( ) . find_map( |arg| match arg {
39- GenericArg :: Type ( ty) => Some ( ty) ,
40- _ => None ,
41- } ) ;
42- let ty_ty = hir_ty_to_ty( cx. tcx, boxed_ty) ;
43- if !ty_ty. has_escaping_bound_vars( ) ;
44- if ty_ty. is_sized( cx. tcx, cx. param_env) ;
45- if let Ok ( ty_ty_size) = cx. layout_of( ty_ty) . map( |l| l. size. bytes( ) ) ;
46- if ty_ty_size < box_size_threshold;
47- then {
48- span_lint_and_sugg(
49- cx,
50- VEC_BOX ,
51- hir_ty. span,
52- "`Vec<T>` is already on the heap, the boxing is unnecessary" ,
53- "try" ,
54- format!( "Vec<{}>" , snippet( cx, boxed_ty. span, ".." ) ) ,
55- Applicability :: MachineApplicable ,
56- ) ;
57- true
58- } else {
59- false
60- }
32+ && let Some ( last) = last_path_segment ( ty_qpath) . args
33+ && let Some ( GenericArg :: Type ( boxed_ty) ) = last. args . first ( )
34+ && let ty_ty = hir_ty_to_ty ( cx. tcx , boxed_ty)
35+ && !ty_ty. has_escaping_bound_vars ( )
36+ && ty_ty. is_sized ( cx. tcx , cx. param_env )
37+ && let Ok ( ty_ty_size) = cx. layout_of ( ty_ty) . map ( |l| l. size . bytes ( ) )
38+ && ty_ty_size < box_size_threshold
39+ {
40+ span_lint_and_sugg (
41+ cx,
42+ VEC_BOX ,
43+ hir_ty. span ,
44+ "`Vec<T>` is already on the heap, the boxing is unnecessary" ,
45+ "try" ,
46+ format ! ( "Vec<{}>" , snippet( cx, boxed_ty. span, ".." ) ) ,
47+ Applicability :: MachineApplicable ,
48+ ) ;
49+ true
50+ } else {
51+ false
6152 }
6253 } else {
6354 false
6455 }
65- }
56+ }
0 commit comments