Skip to content

Commit 85b081b

Browse files
committed
code refractor for [needless_match]
1 parent 448a26d commit 85b081b

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

clippy_lints/src/matches/needless_match.rs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::source::snippet_with_applicability;
44
use clippy_utils::ty::{is_type_diagnostic_item, same_type_and_consts};
55
use clippy_utils::{
6-
eq_expr_value, get_parent_expr_for_hir, get_parent_node, higher, is_else_clause, is_lang_ctor,
6+
eq_expr_value, get_parent_expr_for_hir, get_parent_node, higher, is_else_clause, is_lang_ctor, over,
77
peel_blocks_with_stmt,
88
};
99
use rustc_errors::Applicability;
1010
use rustc_hir::LangItem::OptionNone;
11-
use rustc_hir::{Arm, BindingAnnotation, Expr, ExprKind, FnRetTy, Node, Pat, PatKind, Path, PathSegment, QPath};
11+
use rustc_hir::{Arm, BindingAnnotation, Expr, ExprKind, FnRetTy, Node, Pat, PatKind, Path, QPath};
1212
use rustc_lint::LateContext;
1313
use rustc_span::sym;
1414
use rustc_typeck::hir_ty_to_ty;
@@ -157,8 +157,9 @@ fn pat_same_as_expr(pat: &Pat<'_>, expr: &Expr<'_>) -> bool {
157157
// Example: `Some(val) => Some(val)`
158158
(PatKind::TupleStruct(QPath::Resolved(_, path), tuple_params, _), ExprKind::Call(call_expr, call_params)) => {
159159
if let ExprKind::Path(QPath::Resolved(_, call_path)) = call_expr.kind {
160-
return same_segments(path.segments, call_path.segments)
161-
&& same_non_ref_symbols(tuple_params, call_params);
160+
return over(path.segments, call_path.segments, |pat_seg, call_seg| {
161+
pat_seg.ident.name == call_seg.ident.name
162+
}) && same_non_ref_symbols(tuple_params, call_params);
162163
}
163164
},
164165
// Example: `val => val`
@@ -177,7 +178,9 @@ fn pat_same_as_expr(pat: &Pat<'_>, expr: &Expr<'_>) -> bool {
177178
},
178179
// Example: `Custom::TypeA => Custom::TypeB`, or `None => None`
179180
(PatKind::Path(QPath::Resolved(_, p_path)), ExprKind::Path(QPath::Resolved(_, e_path))) => {
180-
return same_segments(p_path.segments, e_path.segments);
181+
return over(p_path.segments, e_path.segments, |p_seg, e_seg| {
182+
p_seg.ident.name == e_seg.ident.name
183+
});
181184
},
182185
// Example: `5 => 5`
183186
(PatKind::Lit(pat_lit_expr), ExprKind::Lit(expr_spanned)) => {
@@ -191,20 +194,6 @@ fn pat_same_as_expr(pat: &Pat<'_>, expr: &Expr<'_>) -> bool {
191194
false
192195
}
193196

194-
fn same_segments(left_segs: &[PathSegment<'_>], right_segs: &[PathSegment<'_>]) -> bool {
195-
if left_segs.len() != right_segs.len() {
196-
return false;
197-
}
198-
199-
for i in 0..left_segs.len() {
200-
if left_segs[i].ident.name != right_segs[i].ident.name {
201-
return false;
202-
}
203-
}
204-
205-
true
206-
}
207-
208197
fn same_non_ref_symbols(pats: &[Pat<'_>], exprs: &[Expr<'_>]) -> bool {
209198
if pats.len() != exprs.len() {
210199
return false;

0 commit comments

Comments
 (0)