1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::source::snippet_with_applicability;
3 use clippy_utils::{higher, is_wild};
4 use rustc_ast::{Attribute, LitKind};
5 use rustc_errors::Applicability;
6 use rustc_hir::{Arm, BorrowKind, Expr, ExprKind, Guard, Pat};
7 use rustc_lint::LateContext;
9 use rustc_span::source_map::Spanned;
11 use super::MATCH_LIKE_MATCHES_MACRO;
13 /// Lint a `match` or `if let .. { .. } else { .. }` expr that could be replaced by `matches!`
14 pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
15 if let Some(higher::IfLet {
19 if_else: Some(if_else),
20 }) = higher::IfLet::hir(cx, expr)
25 IntoIterator::into_iter([(&[][..], Some(let_pat), if_then, None), (&[][..], None, if_else, None)]),
32 pub(super) fn check_match<'tcx>(
33 cx: &LateContext<'tcx>,
35 scrutinee: &'tcx Expr<'_>,
36 arms: &'tcx [Arm<'tcx>],
41 arms.iter().map(|arm| {
43 cx.tcx.hir().attrs(arm.hir_id),
54 /// Lint a `match` or `if let` for replacement by `matches!`
55 fn find_matches_sugg<'a, 'b, I>(
72 Option<&'a Guard<'b>>,
78 if cx.typeck_results().expr_ty(expr).is_bool();
79 if let Some((_, last_pat_opt, last_expr, _)) = iter.next_back();
80 let iter_without_last = iter.clone();
81 if let Some((first_attrs, _, first_expr, first_guard)) = iter.next();
82 if let Some(b0) = find_bool_lit(&first_expr.kind, is_if_let);
83 if let Some(b1) = find_bool_lit(&last_expr.kind, is_if_let);
85 if first_guard.is_none() || iter.len() == 0;
86 if first_attrs.is_empty();
89 find_bool_lit(&arm.2.kind, is_if_let).map_or(false, |b| b == b0) && arm.3.is_none() && arm.0.is_empty()
92 if let Some(last_pat) = last_pat_opt {
93 if !is_wild(last_pat) {
98 // The suggestion may be incorrect, because some arms can have `cfg` attributes
99 // evaluated into `false` and so such arms will be stripped before.
100 let mut applicability = Applicability::MaybeIncorrect;
102 use itertools::Itertools as _;
105 let pat_span = arm.1?.span;
106 Some(snippet_with_applicability(cx, pat_span, "..", &mut applicability))
110 let pat_and_guard = if let Some(Guard::If(g)) = first_guard {
111 format!("{} if {}", pat, snippet_with_applicability(cx, g.span, "..", &mut applicability))
116 // strip potential borrows (#6503), but only if the type is a reference
118 if let ExprKind::AddrOf(BorrowKind::Ref, .., ex_inner) = ex.kind {
119 if let ty::Ref(..) = cx.typeck_results().expr_ty(ex_inner).kind() {
125 MATCH_LIKE_MATCHES_MACRO,
127 &format!("{} expression looks like `matches!` macro", if is_if_let { "if let .. else" } else { "match" }),
130 "{}matches!({}, {})",
131 if b0 { "" } else { "!" },
132 snippet_with_applicability(cx, ex_new.span, "..", &mut applicability),
144 /// Extract a `bool` or `{ bool }`
145 fn find_bool_lit(ex: &ExprKind<'_>, is_if_let: bool) -> Option<bool> {
147 ExprKind::Lit(Spanned {
148 node: LitKind::Bool(b), ..
158 if let ExprKind::Lit(Spanned {
159 node: LitKind::Bool(b), ..