1 use super::NEEDLESS_MATCH;
2 use clippy_utils::diagnostics::span_lint_and_sugg;
3 use clippy_utils::source::snippet_with_applicability;
4 use clippy_utils::ty::{is_type_diagnostic_item, same_type_and_consts};
6 eq_expr_value, get_parent_expr_for_hir, get_parent_node, higher, is_else_clause, is_lang_ctor, over,
9 use rustc_errors::Applicability;
10 use rustc_hir::LangItem::OptionNone;
11 use rustc_hir::{Arm, BindingAnnotation, Expr, ExprKind, FnRetTy, Node, Pat, PatKind, Path, QPath};
12 use rustc_lint::LateContext;
14 use rustc_typeck::hir_ty_to_ty;
16 pub(crate) fn check_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>) {
17 if arms.len() > 1 && expr_ty_matches_p_ty(cx, ex, expr) && check_all_arms(cx, ex, arms) {
18 let mut applicability = Applicability::MachineApplicable;
23 "this match expression is unnecessary",
25 snippet_with_applicability(cx, ex.span, "..", &mut applicability).to_string(),
31 /// Check for nop `if let` expression that assembled as unnecessary match
34 /// if let Some(a) = option {
42 /// if let SomeEnum::A = some_enum {
44 /// } else if let SomeEnum::B = some_enum {
50 pub(crate) fn check_if_let<'tcx>(cx: &LateContext<'tcx>, ex: &Expr<'_>, if_let: &higher::IfLet<'tcx>) {
51 if !is_else_clause(cx.tcx, ex) && expr_ty_matches_p_ty(cx, if_let.let_expr, ex) && check_if_let_inner(cx, if_let) {
52 let mut applicability = Applicability::MachineApplicable;
57 "this if-let expression is unnecessary",
59 snippet_with_applicability(cx, if_let.let_expr.span, "..", &mut applicability).to_string(),
65 fn check_all_arms(cx: &LateContext<'_>, match_expr: &Expr<'_>, arms: &[Arm<'_>]) -> bool {
67 let arm_expr = peel_blocks_with_stmt(arm.body);
68 if let PatKind::Wild = arm.pat.kind {
69 return eq_expr_value(cx, match_expr, strip_return(arm_expr));
70 } else if !pat_same_as_expr(arm.pat, arm_expr) {
78 fn check_if_let_inner(cx: &LateContext<'_>, if_let: &higher::IfLet<'_>) -> bool {
79 if let Some(if_else) = if_let.if_else {
80 if !pat_same_as_expr(if_let.let_pat, peel_blocks_with_stmt(if_let.if_then)) {
84 // Recursively check for each `else if let` phrase,
85 if let Some(ref nested_if_let) = higher::IfLet::hir(cx, if_else) {
86 return check_if_let_inner(cx, nested_if_let);
89 if matches!(if_else.kind, ExprKind::Block(..)) {
90 let else_expr = peel_blocks_with_stmt(if_else);
91 if matches!(else_expr.kind, ExprKind::Block(..)) {
94 let ret = strip_return(else_expr);
95 let let_expr_ty = cx.typeck_results().expr_ty(if_let.let_expr);
96 if is_type_diagnostic_item(cx, let_expr_ty, sym::Option) {
97 if let ExprKind::Path(ref qpath) = ret.kind {
98 return is_lang_ctor(cx, qpath, OptionNone) || eq_expr_value(cx, if_let.let_expr, ret);
102 return eq_expr_value(cx, if_let.let_expr, ret);
109 /// Strip `return` keyword if the expression type is `ExprKind::Ret`.
110 fn strip_return<'hir>(expr: &'hir Expr<'hir>) -> &'hir Expr<'hir> {
111 if let ExprKind::Ret(Some(ret)) = expr.kind {
118 /// Manually check for coercion casting by checking if the type of the match operand or let expr
119 /// differs with the assigned local variable or the function return type.
120 fn expr_ty_matches_p_ty(cx: &LateContext<'_>, expr: &Expr<'_>, p_expr: &Expr<'_>) -> bool {
121 if let Some(p_node) = get_parent_node(cx.tcx, p_expr.hir_id) {
123 // Compare match_expr ty with local in `let local = match match_expr {..}`
124 Node::Local(local) => {
125 let results = cx.typeck_results();
126 return same_type_and_consts(results.node_type(local.hir_id), results.expr_ty(expr));
128 // compare match_expr ty with RetTy in `fn foo() -> RetTy`
130 if let Some(fn_decl) = p_node.fn_decl() {
131 if let FnRetTy::Return(ret_ty) = fn_decl.output {
132 return same_type_and_consts(hir_ty_to_ty(cx.tcx, ret_ty), cx.typeck_results().expr_ty(expr));
136 // check the parent expr for this whole block `{ match match_expr {..} }`
137 Node::Block(block) => {
138 if let Some(block_parent_expr) = get_parent_expr_for_hir(cx, block.hir_id) {
139 return expr_ty_matches_p_ty(cx, expr, block_parent_expr);
142 // recursively call on `if xxx {..}` etc.
143 Node::Expr(p_expr) => {
144 return expr_ty_matches_p_ty(cx, expr, p_expr);
152 fn pat_same_as_expr(pat: &Pat<'_>, expr: &Expr<'_>) -> bool {
153 let expr = strip_return(expr);
154 match (&pat.kind, &expr.kind) {
155 // Example: `Some(val) => Some(val)`
156 (PatKind::TupleStruct(QPath::Resolved(_, path), tuple_params, _), ExprKind::Call(call_expr, call_params)) => {
157 if let ExprKind::Path(QPath::Resolved(_, call_path)) = call_expr.kind {
158 return over(path.segments, call_path.segments, |pat_seg, call_seg| {
159 pat_seg.ident.name == call_seg.ident.name
160 }) && same_non_ref_symbols(tuple_params, call_params);
163 // Example: `val => val`
165 PatKind::Binding(annot, _, pat_ident, _),
166 ExprKind::Path(QPath::Resolved(
169 segments: [first_seg, ..],
174 return !matches!(annot, BindingAnnotation::Ref | BindingAnnotation::RefMut)
175 && pat_ident.name == first_seg.ident.name;
177 // Example: `Custom::TypeA => Custom::TypeB`, or `None => None`
178 (PatKind::Path(QPath::Resolved(_, p_path)), ExprKind::Path(QPath::Resolved(_, e_path))) => {
179 return over(p_path.segments, e_path.segments, |p_seg, e_seg| {
180 p_seg.ident.name == e_seg.ident.name
184 (PatKind::Lit(pat_lit_expr), ExprKind::Lit(expr_spanned)) => {
185 if let ExprKind::Lit(pat_spanned) = &pat_lit_expr.kind {
186 return pat_spanned.node == expr_spanned.node;
195 fn same_non_ref_symbols(pats: &[Pat<'_>], exprs: &[Expr<'_>]) -> bool {
196 if pats.len() != exprs.len() {
200 for i in 0..pats.len() {
201 if !pat_same_as_expr(&pats[i], &exprs[i]) {