1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::source::{expr_block, snippet};
3 use clippy_utils::ty::{implements_trait, match_type, peel_mid_ty_refs};
5 is_lint_allowed, is_unit_expr, is_wild, paths, peel_blocks, peel_hir_pat_refs, peel_n_hir_expr_refs,
8 use rustc_errors::Applicability;
9 use rustc_hir::{Arm, BindingAnnotation, Block, Expr, ExprKind, Pat, PatKind};
10 use rustc_lint::LateContext;
11 use rustc_middle::ty::{self, Ty};
13 use super::{MATCH_BOOL, SINGLE_MATCH, SINGLE_MATCH_ELSE};
16 pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>) {
17 if arms.len() == 2 && arms[0].guard.is_none() && arms[1].guard.is_none() {
18 if expr.span.from_expansion() {
19 // Don't lint match expressions present in
23 if let PatKind::Or(..) = arms[0].pat.kind {
24 // don't lint for or patterns for now, this makes
25 // the lint noisy in unnecessary situations
28 let els = arms[1].body;
29 let els = if is_unit_expr(peel_blocks(els)) {
31 } else if let ExprKind::Block(Block { stmts, expr: block_expr, .. }, _) = els.kind {
32 if stmts.len() == 1 && block_expr.is_none() || stmts.is_empty() && block_expr.is_some() {
33 // single statement/expr "else" block, don't lint
36 // block with 2+ statements or 1 expr and 1+ statement
39 // not a block, don't lint
43 let ty = cx.typeck_results().expr_ty(ex);
44 if *ty.kind() != ty::Bool || is_lint_allowed(cx, MATCH_BOOL, ex.hir_id) {
45 check_single_pattern(cx, ex, arms, expr, els);
46 check_opt_like(cx, ex, arms, expr, ty, els);
51 fn check_single_pattern(
56 els: Option<&Expr<'_>>,
58 if is_wild(arms[1].pat) {
59 report_single_pattern(cx, ex, arms, expr, els);
63 fn report_single_pattern(
68 els: Option<&Expr<'_>>,
70 let lint = if els.is_some() { SINGLE_MATCH_ELSE } else { SINGLE_MATCH };
71 let els_str = els.map_or(String::new(), |els| {
72 format!(" else {}", expr_block(cx, els, None, "..", Some(expr.span)))
75 let (pat, pat_ref_count) = peel_hir_pat_refs(arms[0].pat);
76 let (msg, sugg) = if_chain! {
77 if let PatKind::Path(_) | PatKind::Lit(_) = pat.kind;
78 let (ty, ty_ref_count) = peel_mid_ty_refs(cx.typeck_results().expr_ty(ex));
79 if let Some(spe_trait_id) = cx.tcx.lang_items().structural_peq_trait();
80 if let Some(pe_trait_id) = cx.tcx.lang_items().eq_trait();
81 if ty.is_integral() || ty.is_char() || ty.is_str()
82 || (implements_trait(cx, ty, spe_trait_id, &[])
83 && implements_trait(cx, ty, pe_trait_id, &[ty.into()]));
85 // scrutinee derives PartialEq and the pattern is a constant.
86 let pat_ref_count = match pat.kind {
87 // string literals are already a reference.
88 PatKind::Lit(Expr { kind: ExprKind::Lit(lit), .. }) if lit.node.is_str() => pat_ref_count + 1,
91 // References are only implicitly added to the pattern, so no overflow here.
92 // e.g. will work: match &Some(_) { Some(_) => () }
93 // will not: match Some(_) { &Some(_) => () }
94 let ref_count_diff = ty_ref_count - pat_ref_count;
96 // Try to remove address of expressions first.
97 let (ex, removed) = peel_n_hir_expr_refs(ex, ref_count_diff);
98 let ref_count_diff = ref_count_diff - removed;
100 let msg = "you seem to be trying to use `match` for an equality check. Consider using `if`";
102 "if {} == {}{} {}{}",
103 snippet(cx, ex.span, ".."),
104 // PartialEq for different reference counts may not exist.
105 "&".repeat(ref_count_diff),
106 snippet(cx, arms[0].pat.span, ".."),
107 expr_block(cx, arms[0].body, None, "..", Some(expr.span)),
112 let msg = "you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`";
114 "if let {} = {} {}{}",
115 snippet(cx, arms[0].pat.span, ".."),
116 snippet(cx, ex.span, ".."),
117 expr_block(cx, arms[0].body, None, "..", Some(expr.span)),
131 Applicability::HasPlaceholders,
135 fn check_opt_like<'a>(
136 cx: &LateContext<'a>,
141 els: Option<&Expr<'_>>,
143 // We don't want to lint if the second arm contains an enum which could
144 // have more variants in the future.
145 if form_exhaustive_matches(cx, ty, arms[0].pat, arms[1].pat) {
146 report_single_pattern(cx, ex, arms, expr, els);
150 /// Returns `true` if all of the types in the pattern are enums which we know
151 /// won't be expanded in the future
152 fn pat_in_candidate_enum<'a>(cx: &LateContext<'a>, ty: Ty<'a>, pat: &Pat<'_>) -> bool {
153 let mut paths_and_types = Vec::new();
154 collect_pat_paths(&mut paths_and_types, cx, pat, ty);
155 paths_and_types.iter().all(|ty| in_candidate_enum(cx, *ty))
158 /// Returns `true` if the given type is an enum we know won't be expanded in the future
159 fn in_candidate_enum<'a>(cx: &LateContext<'a>, ty: Ty<'_>) -> bool {
160 // list of candidate `Enum`s we know will never get any more members
161 let candidates = [&paths::COW, &paths::OPTION, &paths::RESULT];
163 for candidate_ty in candidates {
164 if match_type(cx, ty, candidate_ty) {
171 /// Collects types from the given pattern
172 fn collect_pat_paths<'a>(acc: &mut Vec<Ty<'a>>, cx: &LateContext<'a>, pat: &Pat<'_>, ty: Ty<'a>) {
174 PatKind::Tuple(inner, _) => inner.iter().for_each(|p| {
175 let p_ty = cx.typeck_results().pat_ty(p);
176 collect_pat_paths(acc, cx, p, p_ty);
178 PatKind::TupleStruct(..) | PatKind::Binding(BindingAnnotation::NONE, .., None) | PatKind::Path(_) => {
185 /// Returns true if the given arm of pattern matching contains wildcard patterns.
186 fn contains_only_wilds(pat: &Pat<'_>) -> bool {
188 PatKind::Wild => true,
189 PatKind::Tuple(inner, _) | PatKind::TupleStruct(_, inner, ..) => inner.iter().all(contains_only_wilds),
194 /// Returns true if the given patterns forms only exhaustive matches that don't contain enum
195 /// patterns without a wildcard.
196 fn form_exhaustive_matches<'a>(cx: &LateContext<'a>, ty: Ty<'a>, left: &Pat<'_>, right: &Pat<'_>) -> bool {
197 match (&left.kind, &right.kind) {
198 (PatKind::Wild, _) | (_, PatKind::Wild) => true,
199 (PatKind::Tuple(left_in, left_pos), PatKind::Tuple(right_in, right_pos)) => {
200 // We don't actually know the position and the presence of the `..` (dotdot) operator
201 // in the arms, so we need to evaluate the correct offsets here in order to iterate in
202 // both arms at the same time.
205 if left_pos.is_some() { 1 } else { 0 }
208 if right_pos.is_some() { 1 } else { 0 }
211 let mut left_pos = left_pos.unwrap_or(usize::MAX);
212 let mut right_pos = right_pos.unwrap_or(usize::MAX);
213 let mut left_dot_space = 0;
214 let mut right_dot_space = 0;
216 let mut found_dotdot = false;
219 if left_dot_space < len - left_in.len() {
225 right_dot_space += 1;
226 if right_dot_space < len - right_in.len() {
234 if !contains_only_wilds(&left_in[i - left_dot_space])
235 && !contains_only_wilds(&right_in[i - right_dot_space])
242 (PatKind::TupleStruct(..), PatKind::Path(_)) => pat_in_candidate_enum(cx, ty, right),
243 (PatKind::TupleStruct(..), PatKind::TupleStruct(_, inner, _)) => {
244 pat_in_candidate_enum(cx, ty, right) && inner.iter().all(contains_only_wilds)