@@ -14,7 +14,7 @@ use rustc_span::sym;
1414use rustc_typeck:: hir_ty_to_ty;
1515
1616pub ( 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_ty ( cx, ex, expr) && check_all_arms ( cx, ex, arms) {
1818 let mut applicability = Applicability :: MachineApplicable ;
1919 span_lint_and_sugg (
2020 cx,
@@ -49,7 +49,7 @@ pub(crate) fn check_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>],
4949/// ```
5050pub ( crate ) fn check ( cx : & LateContext < ' _ > , ex : & Expr < ' _ > ) {
5151 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_ty ( cx, if_let. let_expr , ex) && check_if_let ( cx, if_let) {
5353 let mut applicability = Applicability :: MachineApplicable ;
5454 span_lint_and_sugg (
5555 cx,
@@ -119,39 +119,35 @@ fn strip_return<'hir>(expr: &'hir Expr<'hir>) -> &'hir Expr<'hir> {
119119
120120/// Manually check for coercion casting by checking if the type of the match operand or let expr
121121/// 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_ty ( cx : & LateContext < ' _ > , expr : & Expr < ' _ > , p_expr : & Expr < ' _ > ) -> bool {
123+ if let Some ( p_node) = get_parent_node ( cx. tcx , p_expr . hir_id ) {
124124 match p_node {
125125 // Compare match_expr ty with local in `let local = match match_expr {..}`
126126 Node :: Local ( local) => {
127127 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 ) ) ;
129129 } ,
130130 // compare match_expr ty with RetTy in `fn foo() -> RetTy`
131131 Node :: Item ( ..) => {
132132 if let Some ( fn_decl) = p_node. fn_decl ( ) {
133133 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) ) ;
138135 }
139136 }
140137 } ,
141138 // check the parent expr for this whole block `{ match match_expr {..} }`
142139 Node :: Block ( block) => {
143140 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_ty ( cx, expr , block_parent_expr) ;
145142 }
146143 } ,
147144 // recursively call on `if xxx {..}` etc.
148145 Node :: Expr ( p_expr) => {
149- return is_coercion_casting ( cx, match_expr , p_expr) ;
146+ return expr_ty_matches_p_ty ( cx, expr , p_expr) ;
150147 } ,
151148 _ => { } ,
152149 }
153150 }
154-
155151 false
156152}
157153
0 commit comments