1
1
use clippy_utils:: diagnostics:: span_lint_and_sugg;
2
2
use clippy_utils:: last_path_segment;
3
3
use clippy_utils:: source:: snippet;
4
- use if_chain:: if_chain;
5
4
use rustc_errors:: Applicability ;
6
5
use rustc_hir:: def_id:: DefId ;
7
6
use rustc_hir:: { self as hir, GenericArg , QPath , TyKind } ;
@@ -21,45 +20,37 @@ pub(super) fn check(
21
20
box_size_threshold : u64 ,
22
21
) -> bool {
23
22
if cx. tcx . is_diagnostic_item ( sym:: Vec , def_id) {
24
- if_chain ! {
23
+ if let Some ( last ) = last_path_segment ( qpath ) . args
25
24
// 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 ( )
31
26
// 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 ( )
36
31
// 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
61
52
}
62
53
} else {
63
54
false
64
55
}
65
- }
56
+ }
0 commit comments