@@ -43,56 +43,50 @@ impl<'tcx> LateLintPass<'tcx> for MapUnitFn {
43
43
return ;
44
44
}
45
45
46
- if let StmtKind :: Semi ( expr) = stmt. kind {
47
- if let ExprKind :: MethodCall ( path, receiver, args, span) = expr. kind {
48
- if path. ident . name . as_str ( ) == "map" {
49
- if receiver. span . from_expansion ( )
50
- || args. iter ( ) . any ( |e| e. span . from_expansion ( ) )
51
- || !is_impl_slice ( cx, receiver)
52
- || !is_diagnostic_name ( cx, expr. hir_id , "IteratorMap" )
53
- {
54
- return ;
46
+ if let StmtKind :: Semi ( expr) = stmt. kind
47
+ && let ExprKind :: MethodCall ( path, receiver, args, span) = expr. kind
48
+ {
49
+ if path. ident . name . as_str ( ) == "map" {
50
+ if receiver. span . from_expansion ( )
51
+ || args. iter ( ) . any ( |e| e. span . from_expansion ( ) )
52
+ || !is_impl_slice ( cx, receiver)
53
+ || !is_diagnostic_name ( cx, expr. hir_id , "IteratorMap" )
54
+ {
55
+ return ;
56
+ }
57
+ let arg_ty = cx. typeck_results ( ) . expr_ty ( & args[ 0 ] ) ;
58
+ let default_span = args[ 0 ] . span ;
59
+ if let ty:: FnDef ( id, _) = arg_ty. kind ( ) {
60
+ let fn_ty = cx. tcx . fn_sig ( id) . skip_binder ( ) ;
61
+ let ret_ty = fn_ty. output ( ) . skip_binder ( ) ;
62
+ if is_unit_type ( ret_ty) {
63
+ cx. emit_span_lint (
64
+ MAP_UNIT_FN ,
65
+ span,
66
+ MappingToUnit {
67
+ function_label : cx. tcx . span_of_impl ( * id) . unwrap_or ( default_span) ,
68
+ argument_label : args[ 0 ] . span ,
69
+ map_label : span,
70
+ suggestion : path. ident . span ,
71
+ replace : "for_each" . to_string ( ) ,
72
+ } ,
73
+ )
55
74
}
56
- let arg_ty = cx. typeck_results ( ) . expr_ty ( & args[ 0 ] ) ;
57
- let default_span = args[ 0 ] . span ;
58
- if let ty:: FnDef ( id, _) = arg_ty. kind ( ) {
59
- let fn_ty = cx. tcx . fn_sig ( id) . skip_binder ( ) ;
60
- let ret_ty = fn_ty. output ( ) . skip_binder ( ) ;
61
- if is_unit_type ( ret_ty) {
62
- cx. emit_span_lint (
63
- MAP_UNIT_FN ,
64
- span,
65
- MappingToUnit {
66
- function_label : cx
67
- . tcx
68
- . span_of_impl ( * id)
69
- . unwrap_or ( default_span) ,
70
- argument_label : args[ 0 ] . span ,
71
- map_label : span,
72
- suggestion : path. ident . span ,
73
- replace : "for_each" . to_string ( ) ,
74
- } ,
75
- )
76
- }
77
- } else if let ty:: Closure ( id, subs) = arg_ty. kind ( ) {
78
- let cl_ty = subs. as_closure ( ) . sig ( ) ;
79
- let ret_ty = cl_ty. output ( ) . skip_binder ( ) ;
80
- if is_unit_type ( ret_ty) {
81
- cx. emit_span_lint (
82
- MAP_UNIT_FN ,
83
- span,
84
- MappingToUnit {
85
- function_label : cx
86
- . tcx
87
- . span_of_impl ( * id)
88
- . unwrap_or ( default_span) ,
89
- argument_label : args[ 0 ] . span ,
90
- map_label : span,
91
- suggestion : path. ident . span ,
92
- replace : "for_each" . to_string ( ) ,
93
- } ,
94
- )
95
- }
75
+ } else if let ty:: Closure ( id, subs) = arg_ty. kind ( ) {
76
+ let cl_ty = subs. as_closure ( ) . sig ( ) ;
77
+ let ret_ty = cl_ty. output ( ) . skip_binder ( ) ;
78
+ if is_unit_type ( ret_ty) {
79
+ cx. emit_span_lint (
80
+ MAP_UNIT_FN ,
81
+ span,
82
+ MappingToUnit {
83
+ function_label : cx. tcx . span_of_impl ( * id) . unwrap_or ( default_span) ,
84
+ argument_label : args[ 0 ] . span ,
85
+ map_label : span,
86
+ suggestion : path. ident . span ,
87
+ replace : "for_each" . to_string ( ) ,
88
+ } ,
89
+ )
96
90
}
97
91
}
98
92
}
@@ -101,10 +95,10 @@ impl<'tcx> LateLintPass<'tcx> for MapUnitFn {
101
95
}
102
96
103
97
fn is_impl_slice ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > ) -> bool {
104
- if let Some ( method_id) = cx. typeck_results ( ) . type_dependent_def_id ( expr. hir_id ) {
105
- if let Some ( impl_id) = cx. tcx . impl_of_method ( method_id) {
106
- return cx . tcx . type_of ( impl_id ) . skip_binder ( ) . is_slice ( ) ;
107
- }
98
+ if let Some ( method_id) = cx. typeck_results ( ) . type_dependent_def_id ( expr. hir_id )
99
+ && let Some ( impl_id) = cx. tcx . impl_of_method ( method_id)
100
+ {
101
+ return cx . tcx . type_of ( impl_id ) . skip_binder ( ) . is_slice ( ) ;
108
102
}
109
103
false
110
104
}
@@ -114,11 +108,11 @@ fn is_unit_type(ty: Ty<'_>) -> bool {
114
108
}
115
109
116
110
fn is_diagnostic_name ( cx : & LateContext < ' _ > , id : HirId , name : & str ) -> bool {
117
- if let Some ( def_id) = cx. typeck_results ( ) . type_dependent_def_id ( id) {
118
- if let Some ( item) = cx. tcx . get_diagnostic_name ( def_id) {
119
- if item . as_str ( ) == name {
120
- return true ;
121
- }
111
+ if let Some ( def_id) = cx. typeck_results ( ) . type_dependent_def_id ( id)
112
+ && let Some ( item) = cx. tcx . get_diagnostic_name ( def_id)
113
+ {
114
+ if item . as_str ( ) == name {
115
+ return true ;
122
116
}
123
117
}
124
118
false
0 commit comments