@@ -14,7 +14,7 @@ use rustc_span::sym;
14
14
use rustc_typeck:: hir_ty_to_ty;
15
15
16
16
pub ( crate ) fn check_match ( cx : & LateContext < ' _ > , ex : & Expr < ' _ > , arms : & [ Arm < ' _ > ] , expr : & Expr < ' _ > ) {
17
- if arms. len ( ) > 1 && ! is_coercion_casting ( cx, ex, expr) && check_all_arms ( cx, ex, arms) {
17
+ if arms. len ( ) > 1 && expr_ty_matches_p_type ( cx, ex, expr) && check_all_arms ( cx, ex, arms) {
18
18
let mut applicability = Applicability :: MachineApplicable ;
19
19
span_lint_and_sugg (
20
20
cx,
@@ -49,7 +49,7 @@ pub(crate) fn check_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>],
49
49
/// ```
50
50
pub ( crate ) fn check ( cx : & LateContext < ' _ > , ex : & Expr < ' _ > ) {
51
51
if let Some ( ref if_let) = higher:: IfLet :: hir ( cx, ex) {
52
- if !is_else_clause ( cx. tcx , ex) && ! is_coercion_casting ( cx, if_let. let_expr , ex) && check_if_let ( cx, if_let) {
52
+ if !is_else_clause ( cx. tcx , ex) && expr_ty_matches_p_type ( cx, if_let. let_expr , ex) && check_if_let ( cx, if_let) {
53
53
let mut applicability = Applicability :: MachineApplicable ;
54
54
span_lint_and_sugg (
55
55
cx,
@@ -119,39 +119,35 @@ fn strip_return<'hir>(expr: &'hir Expr<'hir>) -> &'hir Expr<'hir> {
119
119
120
120
/// Manually check for coercion casting by checking if the type of the match operand or let expr
121
121
/// differs with the assigned local variable or the funtion return type.
122
- fn is_coercion_casting ( cx : & LateContext < ' _ > , match_expr : & Expr < ' _ > , expr : & Expr < ' _ > ) -> bool {
123
- if let Some ( p_node) = get_parent_node ( cx. tcx , expr . hir_id ) {
122
+ fn expr_ty_matches_p_type ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , p_expr : & Expr < ' _ > ) -> bool {
123
+ if let Some ( p_node) = get_parent_node ( cx. tcx , p_expr . hir_id ) {
124
124
match p_node {
125
125
// Compare match_expr ty with local in `let local = match match_expr {..}`
126
126
Node :: Local ( local) => {
127
127
let results = cx. typeck_results ( ) ;
128
- return ! same_type_and_consts ( results. node_type ( local. hir_id ) , results. expr_ty ( match_expr ) ) ;
128
+ return same_type_and_consts ( results. node_type ( local. hir_id ) , results. expr_ty ( expr ) ) ;
129
129
} ,
130
130
// compare match_expr ty with RetTy in `fn foo() -> RetTy`
131
131
Node :: Item ( ..) => {
132
132
if let Some ( fn_decl) = p_node. fn_decl ( ) {
133
133
if let FnRetTy :: Return ( ret_ty) = fn_decl. output {
134
- return !same_type_and_consts (
135
- hir_ty_to_ty ( cx. tcx , ret_ty) ,
136
- cx. typeck_results ( ) . expr_ty ( match_expr) ,
137
- ) ;
134
+ return same_type_and_consts ( hir_ty_to_ty ( cx. tcx , ret_ty) , cx. typeck_results ( ) . expr_ty ( expr) ) ;
138
135
}
139
136
}
140
137
} ,
141
138
// check the parent expr for this whole block `{ match match_expr {..} }`
142
139
Node :: Block ( block) => {
143
140
if let Some ( block_parent_expr) = get_parent_expr_for_hir ( cx, block. hir_id ) {
144
- return is_coercion_casting ( cx, match_expr , block_parent_expr) ;
141
+ return expr_ty_matches_p_type ( cx, expr , block_parent_expr) ;
145
142
}
146
143
} ,
147
144
// recursively call on `if xxx {..}` etc.
148
145
Node :: Expr ( p_expr) => {
149
- return is_coercion_casting ( cx, match_expr , p_expr) ;
146
+ return expr_ty_matches_p_type ( cx, expr , p_expr) ;
150
147
} ,
151
148
_ => { } ,
152
149
}
153
150
}
154
-
155
151
false
156
152
}
157
153
0 commit comments