Skip to content

Commit 58d0efb

Browse files
committed
improve parent expr check
1 parent 4b12862 commit 58d0efb

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

clippy_lints/src/matches/needless_match.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_span::sym;
1414
use rustc_typeck::hir_ty_to_ty;
1515

1616
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) {
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
/// ```
5050
pub(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_type(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_type(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_type(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_type(cx, expr, p_expr);
150147
},
151148
_ => {},
152149
}
153150
}
154-
155151
false
156152
}
157153

tests/ui/needless_match.fixed

+12
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,16 @@ mod issue8551 {
182182
}
183183
}
184184

185+
trait Tr {
186+
fn as_mut(&mut self) -> Result<&mut i32, &mut i32>;
187+
}
188+
impl Tr for Result<i32, i32> {
189+
fn as_mut(&mut self) -> Result<&mut i32, &mut i32> {
190+
match self {
191+
Ok(x) => Ok(x),
192+
Err(e) => Err(e),
193+
}
194+
}
195+
}
196+
185197
fn main() {}

tests/ui/needless_match.rs

+12
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,16 @@ mod issue8551 {
219219
}
220220
}
221221

222+
trait Tr {
223+
fn as_mut(&mut self) -> Result<&mut i32, &mut i32>;
224+
}
225+
impl Tr for Result<i32, i32> {
226+
fn as_mut(&mut self) -> Result<&mut i32, &mut i32> {
227+
match self {
228+
Ok(x) => Ok(x),
229+
Err(e) => Err(e),
230+
}
231+
}
232+
}
233+
222234
fn main() {}

0 commit comments

Comments
 (0)