@@ -24,32 +24,29 @@ fn get_iterator_length<'tcx>(cx: &LateContext<'tcx>, iter: &'tcx Expr<'tcx>) ->
24
24
let ty:: Adt ( adt, substs) = cx. typeck_results ( ) . expr_ty ( iter) . kind ( ) else {
25
25
return None ;
26
26
} ;
27
- let did = adt. did ( ) ;
28
27
29
- if cx. tcx . is_diagnostic_item ( sym:: ArrayIntoIter , did) {
30
- // For array::IntoIter<T, const N: usize>, the length is the second generic
31
- // parameter.
32
- substs. const_at ( 1 ) . try_to_target_usize ( cx. tcx ) . map ( u128:: from)
33
- } else if cx. tcx . is_diagnostic_item ( sym:: SliceIter , did)
34
- && let ExprKind :: MethodCall ( _, recv, ..) = iter. kind
35
- {
36
- if let ty:: Array ( _, len) = cx. typeck_results ( ) . expr_ty ( recv) . peel_refs ( ) . kind ( ) {
37
- // For slice::Iter<'_, T>, the receiver might be an array literal: [1,2,3].iter().skip(..)
38
- len. try_to_target_usize ( cx. tcx ) . map ( u128:: from)
39
- } else if let Some ( args) = VecArgs :: hir ( cx, expr_or_init ( cx, recv) ) {
40
- match args {
41
- VecArgs :: Vec ( vec) => vec. len ( ) . try_into ( ) . ok ( ) ,
42
- VecArgs :: Repeat ( _, len) => expr_as_u128 ( cx, len) ,
28
+ match cx. tcx . get_diagnostic_name ( adt. did ( ) ) {
29
+ Some ( sym:: ArrayIntoIter ) => {
30
+ // For array::IntoIter<T, const N: usize>, the length is the second generic
31
+ // parameter.
32
+ substs. const_at ( 1 ) . try_to_target_usize ( cx. tcx ) . map ( u128:: from)
33
+ } ,
34
+ Some ( sym:: SliceIter ) if let ExprKind :: MethodCall ( _, recv, ..) = iter. kind => {
35
+ if let ty:: Array ( _, len) = cx. typeck_results ( ) . expr_ty ( recv) . peel_refs ( ) . kind ( ) {
36
+ // For slice::Iter<'_, T>, the receiver might be an array literal: [1,2,3].iter().skip(..)
37
+ len. try_to_target_usize ( cx. tcx ) . map ( u128:: from)
38
+ } else if let Some ( args) = VecArgs :: hir ( cx, expr_or_init ( cx, recv) ) {
39
+ match args {
40
+ VecArgs :: Vec ( vec) => vec. len ( ) . try_into ( ) . ok ( ) ,
41
+ VecArgs :: Repeat ( _, len) => expr_as_u128 ( cx, len) ,
42
+ }
43
+ } else {
44
+ None
43
45
}
44
- } else {
45
- None
46
- }
47
- } else if cx. tcx . is_diagnostic_item ( sym:: IterEmpty , did) {
48
- Some ( 0 )
49
- } else if cx. tcx . is_diagnostic_item ( sym:: IterOnce , did) {
50
- Some ( 1 )
51
- } else {
52
- None
46
+ } ,
47
+ Some ( sym:: IterEmpty ) => Some ( 0 ) ,
48
+ Some ( sym:: IterOnce ) => Some ( 1 ) ,
49
+ _ => None ,
53
50
}
54
51
}
55
52
0 commit comments