1 use clippy_utils::consts::{constant, miri_to_const, Constant};
2 use clippy_utils::diagnostics::{
3 multispan_sugg, span_lint_and_help, span_lint_and_note, span_lint_and_sugg, span_lint_and_then,
5 use clippy_utils::source::{expr_block, indent_of, snippet, snippet_block, snippet_opt, snippet_with_applicability};
6 use clippy_utils::sugg::Sugg;
7 use clippy_utils::ty::{implements_trait, is_type_diagnostic_item, match_type, peel_mid_ty_refs};
8 use clippy_utils::visitors::LocalUsedVisitor;
10 get_parent_expr, in_macro, is_expn_of, is_lang_ctor, is_lint_allowed, is_refutable, is_wild, meets_msrv, msrvs,
11 path_to_local, path_to_local_id, peel_hir_pat_refs, peel_n_hir_expr_refs, recurse_or_patterns, remove_blocks,
14 use clippy_utils::{paths, search_same, SpanlessEq, SpanlessHash};
15 use if_chain::if_chain;
16 use rustc_ast::ast::LitKind;
17 use rustc_errors::Applicability;
18 use rustc_hir::def::{CtorKind, DefKind, Res};
19 use rustc_hir::LangItem::{OptionNone, OptionSome};
21 self as hir, Arm, BindingAnnotation, Block, BorrowKind, Expr, ExprKind, Guard, HirId, Local, MatchSource,
22 Mutability, Node, Pat, PatKind, PathSegment, QPath, RangeEnd, TyKind,
24 use rustc_hir::{HirIdMap, HirIdSet};
25 use rustc_lint::{LateContext, LateLintPass, LintContext};
26 use rustc_middle::lint::in_external_macro;
27 use rustc_middle::ty::{self, Ty, TyS, VariantDef};
28 use rustc_semver::RustcVersion;
29 use rustc_session::{declare_tool_lint, impl_lint_pass};
30 use rustc_span::source_map::{Span, Spanned};
32 use std::cmp::Ordering;
33 use std::collections::hash_map::Entry;
37 declare_clippy_lint! {
39 /// Checks for matches with a single arm where an `if let`
40 /// will usually suffice.
42 /// ### Why is this bad?
43 /// Just readability – `if let` nests less than a `match`.
47 /// # fn bar(stool: &str) {}
48 /// # let x = Some("abc");
51 /// Some(ref foo) => bar(foo),
56 /// if let Some(ref foo) = x {
62 "a `match` statement with a single nontrivial arm (i.e., where the other arm is `_ => {}`) instead of `if let`"
65 declare_clippy_lint! {
67 /// Checks for matches with two arms where an `if let else` will
70 /// ### Why is this bad?
71 /// Just readability – `if let` nests less than a `match`.
73 /// ### Known problems
74 /// Personal style preferences may differ.
80 /// # fn bar(foo: &usize) {}
81 /// # let other_ref: usize = 1;
82 /// # let x: Option<&usize> = Some(&1);
84 /// Some(ref foo) => bar(foo),
85 /// _ => bar(&other_ref),
89 /// Using `if let` with `else`:
92 /// # fn bar(foo: &usize) {}
93 /// # let other_ref: usize = 1;
94 /// # let x: Option<&usize> = Some(&1);
95 /// if let Some(ref foo) = x {
101 pub SINGLE_MATCH_ELSE,
103 "a `match` statement with two arms where the second arm's pattern is a placeholder instead of a specific match pattern"
106 declare_clippy_lint! {
108 /// Checks for matches where all arms match a reference,
109 /// suggesting to remove the reference and deref the matched expression
110 /// instead. It also checks for `if let &foo = bar` blocks.
112 /// ### Why is this bad?
113 /// It just makes the code less readable. That reference
114 /// destructuring adds nothing to the code.
120 /// &A(ref y) => foo(y),
127 /// A(ref y) => foo(y),
134 "a `match` or `if let` with all arms prefixed with `&` instead of deref-ing the match expression"
137 declare_clippy_lint! {
139 /// Checks for matches where match expression is a `bool`. It
140 /// suggests to replace the expression with an `if...else` block.
142 /// ### Why is this bad?
143 /// It makes the code less readable.
149 /// let condition: bool = true;
150 /// match condition {
155 /// Use if/else instead:
159 /// let condition: bool = true;
168 "a `match` on a boolean expression instead of an `if..else` block"
171 declare_clippy_lint! {
173 /// Checks for overlapping match arms.
175 /// ### Why is this bad?
176 /// It is likely to be an error and if not, makes the code
183 /// 1...10 => println!("1 ... 10"),
184 /// 5...15 => println!("5 ... 15"),
188 pub MATCH_OVERLAPPING_ARM,
190 "a `match` with overlapping arms"
193 declare_clippy_lint! {
195 /// Checks for arm which matches all errors with `Err(_)`
196 /// and take drastic actions like `panic!`.
198 /// ### Why is this bad?
199 /// It is generally a bad practice, similar to
200 /// catching all exceptions in java with `catch(Exception)`
204 /// let x: Result<i32, &str> = Ok(3);
206 /// Ok(_) => println!("ok"),
207 /// Err(_) => panic!("err"),
210 pub MATCH_WILD_ERR_ARM,
212 "a `match` with `Err(_)` arm and take drastic actions"
215 declare_clippy_lint! {
217 /// Checks for match which is used to add a reference to an
220 /// ### Why is this bad?
221 /// Using `as_ref()` or `as_mut()` instead is shorter.
225 /// let x: Option<()> = None;
228 /// let r: Option<&()> = match x {
230 /// Some(ref v) => Some(v),
234 /// let r: Option<&()> = x.as_ref();
238 "a `match` on an Option value instead of using `as_ref()` or `as_mut`"
241 declare_clippy_lint! {
243 /// Checks for wildcard enum matches using `_`.
245 /// ### Why is this bad?
246 /// New enum variants added by library updates can be missed.
248 /// ### Known problems
249 /// Suggested replacements may be incorrect if guards exhaustively cover some
250 /// variants, and also may not use correct path to enum if it's not present in the current scope.
254 /// # enum Foo { A(usize), B(usize) }
255 /// # let x = Foo::B(1);
268 pub WILDCARD_ENUM_MATCH_ARM,
270 "a wildcard enum match arm using `_`"
273 declare_clippy_lint! {
275 /// Checks for wildcard enum matches for a single variant.
277 /// ### Why is this bad?
278 /// New enum variants added by library updates can be missed.
280 /// ### Known problems
281 /// Suggested replacements may not use correct path to enum
282 /// if it's not present in the current scope.
286 /// # enum Foo { A, B, C }
287 /// # let x = Foo::B;
302 pub MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
304 "a wildcard enum match for a single variant"
307 declare_clippy_lint! {
309 /// Checks for wildcard pattern used with others patterns in same match arm.
311 /// ### Why is this bad?
312 /// Wildcard pattern already covers any other pattern as it will match anyway.
313 /// It makes the code less readable, especially to spot wildcard pattern use in match arm.
329 pub WILDCARD_IN_OR_PATTERNS,
331 "a wildcard pattern used with others patterns in same match arm"
334 declare_clippy_lint! {
336 /// Checks for matches being used to destructure a single-variant enum
337 /// or tuple struct where a `let` will suffice.
339 /// ### Why is this bad?
340 /// Just readability – `let` doesn't nest, whereas a `match` does.
348 /// let wrapper = Wrapper::Data(42);
350 /// let data = match wrapper {
351 /// Wrapper::Data(i) => i,
355 /// The correct use would be:
361 /// let wrapper = Wrapper::Data(42);
362 /// let Wrapper::Data(data) = wrapper;
364 pub INFALLIBLE_DESTRUCTURING_MATCH,
366 "a `match` statement with a single infallible arm instead of a `let`"
369 declare_clippy_lint! {
371 /// Checks for useless match that binds to only one value.
373 /// ### Why is this bad?
374 /// Readability and needless complexity.
376 /// ### Known problems
377 /// Suggested replacements may be incorrect when `match`
378 /// is actually binding temporary value, bringing a 'dropped while borrowed' error.
393 /// let (c, d) = (a, b);
395 pub MATCH_SINGLE_BINDING,
397 "a match with a single binding instead of using `let` statement"
400 declare_clippy_lint! {
402 /// Checks for unnecessary '..' pattern binding on struct when all fields are explicitly matched.
404 /// ### Why is this bad?
405 /// Correctness and readability. It's like having a wildcard pattern after
406 /// matching all enum variants explicitly.
410 /// # struct A { a: i32 }
411 /// let a = A { a: 5 };
415 /// A { a: 5, .. } => {},
421 /// A { a: 5 } => {},
425 pub REST_PAT_IN_FULLY_BOUND_STRUCTS,
427 "a match on a struct that binds all fields but still uses the wildcard pattern"
430 declare_clippy_lint! {
432 /// Lint for redundant pattern matching over `Result`, `Option`,
433 /// `std::task::Poll` or `std::net::IpAddr`
435 /// ### Why is this bad?
436 /// It's more concise and clear to just use the proper
439 /// ### Known problems
440 /// This will change the drop order for the matched type. Both `if let` and
441 /// `while let` will drop the value at the end of the block, both `if` and `while` will drop the
442 /// value before entering the block. For most types this change will not matter, but for a few
443 /// types this will not be an acceptable change (e.g. locks). See the
444 /// [reference](https://doc.rust-lang.org/reference/destructors.html#drop-scopes) for more about
449 /// # use std::task::Poll;
450 /// # use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
451 /// if let Ok(_) = Ok::<i32, i32>(42) {}
452 /// if let Err(_) = Err::<i32, i32>(42) {}
453 /// if let None = None::<()> {}
454 /// if let Some(_) = Some(42) {}
455 /// if let Poll::Pending = Poll::Pending::<()> {}
456 /// if let Poll::Ready(_) = Poll::Ready(42) {}
457 /// if let IpAddr::V4(_) = IpAddr::V4(Ipv4Addr::LOCALHOST) {}
458 /// if let IpAddr::V6(_) = IpAddr::V6(Ipv6Addr::LOCALHOST) {}
459 /// match Ok::<i32, i32>(42) {
465 /// The more idiomatic use would be:
468 /// # use std::task::Poll;
469 /// # use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
470 /// if Ok::<i32, i32>(42).is_ok() {}
471 /// if Err::<i32, i32>(42).is_err() {}
472 /// if None::<()>.is_none() {}
473 /// if Some(42).is_some() {}
474 /// if Poll::Pending::<()>.is_pending() {}
475 /// if Poll::Ready(42).is_ready() {}
476 /// if IpAddr::V4(Ipv4Addr::LOCALHOST).is_ipv4() {}
477 /// if IpAddr::V6(Ipv6Addr::LOCALHOST).is_ipv6() {}
478 /// Ok::<i32, i32>(42).is_ok();
480 pub REDUNDANT_PATTERN_MATCHING,
482 "use the proper utility function avoiding an `if let`"
485 declare_clippy_lint! {
487 /// Checks for `match` or `if let` expressions producing a
488 /// `bool` that could be written using `matches!`
490 /// ### Why is this bad?
491 /// Readability and needless complexity.
493 /// ### Known problems
494 /// This lint falsely triggers, if there are arms with
495 /// `cfg` attributes that remove an arm evaluating to `false`.
502 /// let a = match x {
507 /// let a = if let Some(0) = x {
514 /// let a = matches!(x, Some(0));
516 pub MATCH_LIKE_MATCHES_MACRO,
518 "a match that could be written with the matches! macro"
521 declare_clippy_lint! {
523 /// Checks for `match` with identical arm bodies.
525 /// ### Why is this bad?
526 /// This is probably a copy & paste error. If arm bodies
527 /// are the same on purpose, you can factor them
528 /// [using `|`](https://doc.rust-lang.org/book/patterns.html#multiple-patterns).
530 /// ### Known problems
531 /// False positive possible with order dependent `match`
533 /// [#860](https://github.com/rust-lang/rust-clippy/issues/860)).
540 /// Baz => bar(), // <= oops
544 /// This should probably be
549 /// Baz => baz(), // <= fixed
553 /// or if the original code was not a typo:
556 /// Bar | Baz => bar(), // <= shows the intent better
562 "`match` with identical arm bodies"
567 msrv: Option<RustcVersion>,
568 infallible_destructuring_match_linted: bool,
573 pub fn new(msrv: Option<RustcVersion>) -> Self {
581 impl_lint_pass!(Matches => [
586 MATCH_OVERLAPPING_ARM,
589 WILDCARD_ENUM_MATCH_ARM,
590 MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
591 WILDCARD_IN_OR_PATTERNS,
592 MATCH_SINGLE_BINDING,
593 INFALLIBLE_DESTRUCTURING_MATCH,
594 REST_PAT_IN_FULLY_BOUND_STRUCTS,
595 REDUNDANT_PATTERN_MATCHING,
596 MATCH_LIKE_MATCHES_MACRO,
600 impl<'tcx> LateLintPass<'tcx> for Matches {
601 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
602 if in_external_macro(cx.sess(), expr.span) || in_macro(expr.span) {
606 redundant_pattern_match::check(cx, expr);
608 if meets_msrv(self.msrv.as_ref(), &msrvs::MATCHES_MACRO) {
609 if !check_match_like_matches(cx, expr) {
610 lint_match_arms(cx, expr);
613 lint_match_arms(cx, expr);
616 if let ExprKind::Match(ex, arms, MatchSource::Normal) = expr.kind {
617 check_single_match(cx, ex, arms, expr);
618 check_match_bool(cx, ex, arms, expr);
619 check_overlapping_arms(cx, ex, arms);
620 check_wild_err_arm(cx, ex, arms);
621 check_wild_enum_match(cx, ex, arms);
622 check_match_as_ref(cx, ex, arms, expr);
623 check_wild_in_or_pats(cx, arms);
625 if self.infallible_destructuring_match_linted {
626 self.infallible_destructuring_match_linted = false;
628 check_match_single_binding(cx, ex, arms, expr);
631 if let ExprKind::Match(ex, arms, _) = expr.kind {
632 check_match_ref_pats(cx, ex, arms, expr);
636 fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'_>) {
638 if !in_external_macro(cx.sess(), local.span);
639 if !in_macro(local.span);
640 if let Some(expr) = local.init;
641 if let ExprKind::Match(target, arms, MatchSource::Normal) = expr.kind;
642 if arms.len() == 1 && arms[0].guard.is_none();
643 if let PatKind::TupleStruct(
644 QPath::Resolved(None, variant_name), args, _) = arms[0].pat.kind;
646 if let PatKind::Binding(_, arg, ..) = strip_pat_refs(&args[0]).kind;
647 let body = remove_blocks(arms[0].body);
648 if path_to_local_id(body, arg);
651 let mut applicability = Applicability::MachineApplicable;
652 self.infallible_destructuring_match_linted = true;
655 INFALLIBLE_DESTRUCTURING_MATCH,
657 "you seem to be trying to use `match` to destructure a single infallible pattern. \
658 Consider using `let`",
662 snippet_with_applicability(cx, variant_name.span, "..", &mut applicability),
663 snippet_with_applicability(cx, local.pat.span, "..", &mut applicability),
664 snippet_with_applicability(cx, target.span, "..", &mut applicability),
672 fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
674 if !in_external_macro(cx.sess(), pat.span);
675 if !in_macro(pat.span);
676 if let PatKind::Struct(QPath::Resolved(_, path), fields, true) = pat.kind;
677 if let Some(def_id) = path.res.opt_def_id();
678 let ty = cx.tcx.type_of(def_id);
679 if let ty::Adt(def, _) = ty.kind();
680 if def.is_struct() || def.is_union();
681 if fields.len() == def.non_enum_variant().fields.len();
686 REST_PAT_IN_FULLY_BOUND_STRUCTS,
688 "unnecessary use of `..` pattern in struct binding. All fields were already bound",
690 "consider removing `..` from this binding",
696 extract_msrv_attr!(LateContext);
700 fn check_single_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>) {
701 if arms.len() == 2 && arms[0].guard.is_none() && arms[1].guard.is_none() {
702 if in_macro(expr.span) {
703 // Don't lint match expressions present in
704 // macro_rules! block
707 if let PatKind::Or(..) = arms[0].pat.kind {
708 // don't lint for or patterns for now, this makes
709 // the lint noisy in unnecessary situations
712 let els = arms[1].body;
713 let els = if is_unit_expr(remove_blocks(els)) {
715 } else if let ExprKind::Block(Block { stmts, expr: block_expr, .. }, _) = els.kind {
716 if stmts.len() == 1 && block_expr.is_none() || stmts.is_empty() && block_expr.is_some() {
717 // single statement/expr "else" block, don't lint
720 // block with 2+ statements or 1 expr and 1+ statement
723 // not a block, don't lint
727 let ty = cx.typeck_results().expr_ty(ex);
728 if *ty.kind() != ty::Bool || is_lint_allowed(cx, MATCH_BOOL, ex.hir_id) {
729 check_single_match_single_pattern(cx, ex, arms, expr, els);
730 check_single_match_opt_like(cx, ex, arms, expr, ty, els);
735 fn check_single_match_single_pattern(
736 cx: &LateContext<'_>,
740 els: Option<&Expr<'_>>,
742 if is_wild(arms[1].pat) {
743 report_single_match_single_pattern(cx, ex, arms, expr, els);
747 fn report_single_match_single_pattern(
748 cx: &LateContext<'_>,
752 els: Option<&Expr<'_>>,
754 let lint = if els.is_some() { SINGLE_MATCH_ELSE } else { SINGLE_MATCH };
755 let els_str = els.map_or(String::new(), |els| {
756 format!(" else {}", expr_block(cx, els, None, "..", Some(expr.span)))
759 let (pat, pat_ref_count) = peel_hir_pat_refs(arms[0].pat);
760 let (msg, sugg) = if_chain! {
761 if let PatKind::Path(_) | PatKind::Lit(_) = pat.kind;
762 let (ty, ty_ref_count) = peel_mid_ty_refs(cx.typeck_results().expr_ty(ex));
763 if let Some(spe_trait_id) = cx.tcx.lang_items().structural_peq_trait();
764 if let Some(pe_trait_id) = cx.tcx.lang_items().eq_trait();
765 if ty.is_integral() || ty.is_char() || ty.is_str()
766 || (implements_trait(cx, ty, spe_trait_id, &[])
767 && implements_trait(cx, ty, pe_trait_id, &[ty.into()]));
769 // scrutinee derives PartialEq and the pattern is a constant.
770 let pat_ref_count = match pat.kind {
771 // string literals are already a reference.
772 PatKind::Lit(Expr { kind: ExprKind::Lit(lit), .. }) if lit.node.is_str() => pat_ref_count + 1,
775 // References are only implicitly added to the pattern, so no overflow here.
776 // e.g. will work: match &Some(_) { Some(_) => () }
777 // will not: match Some(_) { &Some(_) => () }
778 let ref_count_diff = ty_ref_count - pat_ref_count;
780 // Try to remove address of expressions first.
781 let (ex, removed) = peel_n_hir_expr_refs(ex, ref_count_diff);
782 let ref_count_diff = ref_count_diff - removed;
784 let msg = "you seem to be trying to use `match` for an equality check. Consider using `if`";
786 "if {} == {}{} {}{}",
787 snippet(cx, ex.span, ".."),
788 // PartialEq for different reference counts may not exist.
789 "&".repeat(ref_count_diff),
790 snippet(cx, arms[0].pat.span, ".."),
791 expr_block(cx, arms[0].body, None, "..", Some(expr.span)),
796 let msg = "you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`";
798 "if let {} = {} {}{}",
799 snippet(cx, arms[0].pat.span, ".."),
800 snippet(cx, ex.span, ".."),
801 expr_block(cx, arms[0].body, None, "..", Some(expr.span)),
815 Applicability::HasPlaceholders,
819 fn check_single_match_opt_like(
820 cx: &LateContext<'_>,
825 els: Option<&Expr<'_>>,
827 // list of candidate `Enum`s we know will never get any more members
829 (&paths::COW, "Borrowed"),
830 (&paths::COW, "Cow::Borrowed"),
831 (&paths::COW, "Cow::Owned"),
832 (&paths::COW, "Owned"),
833 (&paths::OPTION, "None"),
834 (&paths::RESULT, "Err"),
835 (&paths::RESULT, "Ok"),
838 let path = match arms[1].pat.kind {
839 PatKind::TupleStruct(ref path, inner, _) => {
840 // Contains any non wildcard patterns (e.g., `Err(err)`)?
841 if !inner.iter().all(is_wild) {
844 rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false))
846 PatKind::Binding(BindingAnnotation::Unannotated, .., ident, None) => ident.to_string(),
847 PatKind::Path(ref path) => {
848 rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false))
853 for &(ty_path, pat_path) in candidates {
854 if path == *pat_path && match_type(cx, ty, ty_path) {
855 report_single_match_single_pattern(cx, ex, arms, expr, els);
860 fn check_match_bool(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>) {
861 // Type of expression is `bool`.
862 if *cx.typeck_results().expr_ty(ex).kind() == ty::Bool {
867 "you seem to be trying to match on a boolean expression",
871 let exprs = if let PatKind::Lit(arm_bool) = arms[0].pat.kind {
872 if let ExprKind::Lit(ref lit) = arm_bool.kind {
874 LitKind::Bool(true) => Some((&*arms[0].body, &*arms[1].body)),
875 LitKind::Bool(false) => Some((&*arms[1].body, &*arms[0].body)),
885 if let Some((true_expr, false_expr)) = exprs {
886 let sugg = match (is_unit_expr(true_expr), is_unit_expr(false_expr)) {
887 (false, false) => Some(format!(
889 snippet(cx, ex.span, "b"),
890 expr_block(cx, true_expr, None, "..", Some(expr.span)),
891 expr_block(cx, false_expr, None, "..", Some(expr.span))
893 (false, true) => Some(format!(
895 snippet(cx, ex.span, "b"),
896 expr_block(cx, true_expr, None, "..", Some(expr.span))
899 let test = Sugg::hir(cx, ex, "..");
903 expr_block(cx, false_expr, None, "..", Some(expr.span))
906 (true, true) => None,
909 if let Some(sugg) = sugg {
910 diag.span_suggestion(
912 "consider using an `if`/`else` expression",
914 Applicability::HasPlaceholders,
924 fn check_overlapping_arms<'tcx>(cx: &LateContext<'tcx>, ex: &'tcx Expr<'_>, arms: &'tcx [Arm<'_>]) {
925 if arms.len() >= 2 && cx.typeck_results().expr_ty(ex).is_integral() {
926 let ranges = all_ranges(cx, arms, cx.typeck_results().expr_ty(ex));
927 let type_ranges = type_ranges(&ranges);
928 if !type_ranges.is_empty() {
929 if let Some((start, end)) = overlapping(&type_ranges) {
932 MATCH_OVERLAPPING_ARM,
934 "some ranges overlap",
936 "overlaps with this",
943 fn check_wild_err_arm<'tcx>(cx: &LateContext<'tcx>, ex: &Expr<'tcx>, arms: &[Arm<'tcx>]) {
944 let ex_ty = cx.typeck_results().expr_ty(ex).peel_refs();
945 if is_type_diagnostic_item(cx, ex_ty, sym::result_type) {
947 if let PatKind::TupleStruct(ref path, inner, _) = arm.pat.kind {
948 let path_str = rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false));
949 if path_str == "Err" {
950 let mut matching_wild = inner.iter().any(is_wild);
951 let mut ident_bind_name = String::from("_");
953 // Looking for unused bindings (i.e.: `_e`)
954 for pat in inner.iter() {
955 if let PatKind::Binding(_, id, ident, None) = pat.kind {
956 if ident.as_str().starts_with('_')
957 && !LocalUsedVisitor::new(cx, id).check_expr(arm.body)
959 ident_bind_name = (&ident.name.as_str()).to_string();
960 matching_wild = true;
967 if let ExprKind::Block(block, _) = arm.body.kind;
968 if is_panic_block(block);
970 // `Err(_)` or `Err(_e)` arm with `panic!` found
971 span_lint_and_note(cx,
974 &format!("`Err({})` matches all errors", &ident_bind_name),
976 "match each error separately or use the error output, or use `.except(msg)` if the error case is unreachable",
986 enum CommonPrefixSearcher<'a> {
988 Path(&'a [PathSegment<'a>]),
991 impl CommonPrefixSearcher<'a> {
992 fn with_path(&mut self, path: &'a [PathSegment<'a>]) {
994 [path @ .., _] => self.with_prefix(path),
999 fn with_prefix(&mut self, path: &'a [PathSegment<'a>]) {
1001 Self::None => *self = Self::Path(path),
1002 Self::Path(self_path)
1005 .map(|p| p.ident.name)
1006 .eq(self_path.iter().map(|p| p.ident.name)) => {},
1007 Self::Path(_) => *self = Self::Mixed,
1013 fn is_hidden(cx: &LateContext<'_>, variant_def: &VariantDef) -> bool {
1014 let attrs = cx.tcx.get_attrs(variant_def.def_id);
1015 clippy_utils::attrs::is_doc_hidden(attrs) || clippy_utils::attrs::is_unstable(attrs)
1018 #[allow(clippy::too_many_lines)]
1019 fn check_wild_enum_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
1020 let ty = cx.typeck_results().expr_ty(ex).peel_refs();
1021 let adt_def = match ty.kind() {
1023 if adt_def.is_enum()
1024 && !(is_type_diagnostic_item(cx, ty, sym::option_type)
1025 || is_type_diagnostic_item(cx, ty, sym::result_type)) =>
1032 // First pass - check for violation, but don't do much book-keeping because this is hopefully
1033 // the uncommon case, and the book-keeping is slightly expensive.
1034 let mut wildcard_span = None;
1035 let mut wildcard_ident = None;
1036 let mut has_non_wild = false;
1038 match peel_hir_pat_refs(arm.pat).0.kind {
1039 PatKind::Wild => wildcard_span = Some(arm.pat.span),
1040 PatKind::Binding(_, _, ident, None) => {
1041 wildcard_span = Some(arm.pat.span);
1042 wildcard_ident = Some(ident);
1044 _ => has_non_wild = true,
1047 let wildcard_span = match wildcard_span {
1048 Some(x) if has_non_wild => x,
1052 // Accumulate the variants which should be put in place of the wildcard because they're not
1054 let has_hidden = adt_def.variants.iter().any(|x| is_hidden(cx, x));
1055 let mut missing_variants: Vec<_> = adt_def.variants.iter().filter(|x| !is_hidden(cx, x)).collect();
1057 let mut path_prefix = CommonPrefixSearcher::None;
1059 // Guards mean that this case probably isn't exhaustively covered. Technically
1060 // this is incorrect, as we should really check whether each variant is exhaustively
1061 // covered by the set of guards that cover it, but that's really hard to do.
1062 recurse_or_patterns(arm.pat, |pat| {
1063 let path = match &peel_hir_pat_refs(pat).0.kind {
1064 PatKind::Path(path) => {
1065 #[allow(clippy::match_same_arms)]
1066 let id = match cx.qpath_res(path, pat.hir_id) {
1067 Res::Def(DefKind::Const | DefKind::ConstParam | DefKind::AnonConst, _) => return,
1068 Res::Def(_, id) => id,
1071 if arm.guard.is_none() {
1072 missing_variants.retain(|e| e.ctor_def_id != Some(id));
1076 PatKind::TupleStruct(path, patterns, ..) => {
1077 if let Some(id) = cx.qpath_res(path, pat.hir_id).opt_def_id() {
1078 if arm.guard.is_none() && patterns.iter().all(|p| !is_refutable(cx, p)) {
1079 missing_variants.retain(|e| e.ctor_def_id != Some(id));
1084 PatKind::Struct(path, patterns, ..) => {
1085 if let Some(id) = cx.qpath_res(path, pat.hir_id).opt_def_id() {
1086 if arm.guard.is_none() && patterns.iter().all(|p| !is_refutable(cx, p.pat)) {
1087 missing_variants.retain(|e| e.def_id != id);
1095 QPath::Resolved(_, path) => path_prefix.with_path(path.segments),
1096 QPath::TypeRelative(
1098 kind: TyKind::Path(QPath::Resolved(_, path)),
1102 ) => path_prefix.with_prefix(path.segments),
1108 let format_suggestion = |variant: &VariantDef| {
1111 if let Some(ident) = wildcard_ident {
1112 format!("{} @ ", ident.name)
1116 if let CommonPrefixSearcher::Path(path_prefix) = path_prefix {
1117 let mut s = String::new();
1118 for seg in path_prefix {
1119 s.push_str(&seg.ident.as_str());
1124 let mut s = cx.tcx.def_path_str(adt_def.did);
1129 match variant.ctor_kind {
1130 CtorKind::Fn if variant.fields.len() == 1 => "(_)",
1131 CtorKind::Fn => "(..)",
1132 CtorKind::Const => "",
1133 CtorKind::Fictive => "{ .. }",
1138 match missing_variants.as_slice() {
1140 [x] if !adt_def.is_variant_list_non_exhaustive() && !has_hidden => span_lint_and_sugg(
1142 MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
1144 "wildcard matches only a single variant and will also match any future added variants",
1146 format_suggestion(x),
1147 Applicability::MaybeIncorrect,
1150 let mut suggestions: Vec<_> = variants.iter().copied().map(format_suggestion).collect();
1151 let message = if adt_def.is_variant_list_non_exhaustive() || has_hidden {
1152 suggestions.push("_".into());
1153 "wildcard matches known variants and will also match future added variants"
1155 "wildcard match will also match any future added variants"
1160 WILDCARD_ENUM_MATCH_ARM,
1164 suggestions.join(" | "),
1165 Applicability::MaybeIncorrect,
1171 // If the block contains only a `panic!` macro (as expression or statement)
1172 fn is_panic_block(block: &Block<'_>) -> bool {
1173 match (&block.expr, block.stmts.len(), block.stmts.first()) {
1174 (&Some(exp), 0, _) => is_expn_of(exp.span, "panic").is_some() && is_expn_of(exp.span, "unreachable").is_none(),
1175 (&None, 1, Some(stmt)) => {
1176 is_expn_of(stmt.span, "panic").is_some() && is_expn_of(stmt.span, "unreachable").is_none()
1182 fn check_match_ref_pats(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>) {
1183 if has_only_ref_pats(arms) {
1184 let mut suggs = Vec::with_capacity(arms.len() + 1);
1185 let (title, msg) = if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner) = ex.kind {
1186 let span = ex.span.source_callsite();
1187 suggs.push((span, Sugg::hir_with_macro_callsite(cx, inner, "..").to_string()));
1189 "you don't need to add `&` to both the expression and the patterns",
1193 let span = ex.span.source_callsite();
1194 suggs.push((span, Sugg::hir_with_macro_callsite(cx, ex, "..").deref().to_string()));
1196 "you don't need to add `&` to all patterns",
1197 "instead of prefixing all patterns with `&`, you can dereference the expression",
1201 suggs.extend(arms.iter().filter_map(|a| {
1202 if let PatKind::Ref(refp, _) = a.pat.kind {
1203 Some((a.pat.span, snippet(cx, refp.span, "..").to_string()))
1209 span_lint_and_then(cx, MATCH_REF_PATS, expr.span, title, |diag| {
1210 if !expr.span.from_expansion() {
1211 multispan_sugg(diag, msg, suggs);
1217 fn check_match_as_ref(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>) {
1218 if arms.len() == 2 && arms[0].guard.is_none() && arms[1].guard.is_none() {
1219 let arm_ref: Option<BindingAnnotation> = if is_none_arm(cx, &arms[0]) {
1220 is_ref_some_arm(cx, &arms[1])
1221 } else if is_none_arm(cx, &arms[1]) {
1222 is_ref_some_arm(cx, &arms[0])
1226 if let Some(rb) = arm_ref {
1227 let suggestion = if rb == BindingAnnotation::Ref {
1233 let output_ty = cx.typeck_results().expr_ty(expr);
1234 let input_ty = cx.typeck_results().expr_ty(ex);
1236 let cast = if_chain! {
1237 if let ty::Adt(_, substs) = input_ty.kind();
1238 let input_ty = substs.type_at(0);
1239 if let ty::Adt(_, substs) = output_ty.kind();
1240 let output_ty = substs.type_at(0);
1241 if let ty::Ref(_, output_ty, _) = *output_ty.kind();
1242 if input_ty != output_ty;
1250 let mut applicability = Applicability::MachineApplicable;
1255 &format!("use `{}()` instead", suggestion),
1259 snippet_with_applicability(cx, ex.span, "_", &mut applicability),
1269 fn check_wild_in_or_pats(cx: &LateContext<'_>, arms: &[Arm<'_>]) {
1271 if let PatKind::Or(fields) = arm.pat.kind {
1272 // look for multiple fields in this arm that contains at least one Wild pattern
1273 if fields.len() > 1 && fields.iter().any(is_wild) {
1276 WILDCARD_IN_OR_PATTERNS,
1278 "wildcard pattern covers any other pattern as it will match anyway",
1280 "consider handling `_` separately",
1287 /// Lint a `match` or `if let .. { .. } else { .. }` expr that could be replaced by `matches!`
1288 fn check_match_like_matches<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> bool {
1289 if let ExprKind::Match(ex, arms, ref match_source) = &expr.kind {
1290 match match_source {
1291 MatchSource::Normal => find_matches_sugg(cx, ex, arms, expr, false),
1292 MatchSource::IfLetDesugar { .. } => find_matches_sugg(cx, ex, arms, expr, true),
1300 /// Lint a `match` or desugared `if let` for replacement by `matches!`
1301 fn find_matches_sugg(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>, desugared: bool) -> bool {
1304 if cx.typeck_results().expr_ty(expr).is_bool();
1305 if let Some((b1_arm, b0_arms)) = arms.split_last();
1306 if let Some(b0) = find_bool_lit(&b0_arms[0].body.kind, desugared);
1307 if let Some(b1) = find_bool_lit(&b1_arm.body.kind, desugared);
1308 if is_wild(b1_arm.pat);
1310 let if_guard = &b0_arms[0].guard;
1311 if if_guard.is_none() || b0_arms.len() == 1;
1312 if cx.tcx.hir().attrs(b0_arms[0].hir_id).is_empty();
1313 if b0_arms[1..].iter()
1315 find_bool_lit(&arm.body.kind, desugared).map_or(false, |b| b == b0) &&
1316 arm.guard.is_none() && cx.tcx.hir().attrs(arm.hir_id).is_empty()
1319 // The suggestion may be incorrect, because some arms can have `cfg` attributes
1320 // evaluated into `false` and so such arms will be stripped before.
1321 let mut applicability = Applicability::MaybeIncorrect;
1323 use itertools::Itertools as _;
1325 .map(|arm| snippet_with_applicability(cx, arm.pat.span, "..", &mut applicability))
1328 let pat_and_guard = if let Some(Guard::If(g)) = if_guard {
1329 format!("{} if {}", pat, snippet_with_applicability(cx, g.span, "..", &mut applicability))
1334 // strip potential borrows (#6503), but only if the type is a reference
1335 let mut ex_new = ex;
1336 if let ExprKind::AddrOf(BorrowKind::Ref, .., ex_inner) = ex.kind {
1337 if let ty::Ref(..) = cx.typeck_results().expr_ty(ex_inner).kind() {
1343 MATCH_LIKE_MATCHES_MACRO,
1345 &format!("{} expression looks like `matches!` macro", if desugared { "if let .. else" } else { "match" }),
1348 "{}matches!({}, {})",
1349 if b0 { "" } else { "!" },
1350 snippet_with_applicability(cx, ex_new.span, "..", &mut applicability),
1362 /// Extract a `bool` or `{ bool }`
1363 fn find_bool_lit(ex: &ExprKind<'_>, desugared: bool) -> Option<bool> {
1365 ExprKind::Lit(Spanned {
1366 node: LitKind::Bool(b), ..
1376 if let ExprKind::Lit(Spanned {
1377 node: LitKind::Bool(b), ..
1389 #[allow(clippy::too_many_lines)]
1390 fn check_match_single_binding<'a>(cx: &LateContext<'a>, ex: &Expr<'a>, arms: &[Arm<'_>], expr: &Expr<'_>) {
1391 if in_macro(expr.span) || arms.len() != 1 || is_refutable(cx, arms[0].pat) {
1396 // This is a hack to deal with arms that are excluded by macros like `#[cfg]`. It is only used here
1397 // to prevent false positives as there is currently no better way to detect if code was excluded by
1398 // a macro. See PR #6435
1400 if let Some(match_snippet) = snippet_opt(cx, expr.span);
1401 if let Some(arm_snippet) = snippet_opt(cx, arms[0].span);
1402 if let Some(ex_snippet) = snippet_opt(cx, ex.span);
1403 let rest_snippet = match_snippet.replace(&arm_snippet, "").replace(&ex_snippet, "");
1404 if rest_snippet.contains("=>");
1406 // The code it self contains another thick arrow "=>"
1407 // -> Either another arm or a comment
1412 let matched_vars = ex.span;
1413 let bind_names = arms[0].pat.span;
1414 let match_body = remove_blocks(arms[0].body);
1415 let mut snippet_body = if match_body.span.from_expansion() {
1416 Sugg::hir_with_macro_callsite(cx, match_body, "..").to_string()
1418 snippet_block(cx, match_body.span, "..", Some(expr.span)).to_string()
1421 // Do we need to add ';' to suggestion ?
1422 match match_body.kind {
1423 ExprKind::Block(block, _) => {
1424 // macro + expr_ty(body) == ()
1425 if block.span.from_expansion() && cx.typeck_results().expr_ty(match_body).is_unit() {
1426 snippet_body.push(';');
1430 // expr_ty(body) == ()
1431 if cx.typeck_results().expr_ty(match_body).is_unit() {
1432 snippet_body.push(';');
1437 let mut applicability = Applicability::MaybeIncorrect;
1438 match arms[0].pat.kind {
1439 PatKind::Binding(..) | PatKind::Tuple(_, _) | PatKind::Struct(..) => {
1440 // If this match is in a local (`let`) stmt
1441 let (target_span, sugg) = if let Some(parent_let_node) = opt_parent_let(cx, ex) {
1443 parent_let_node.span,
1445 "let {} = {};\n{}let {} = {};",
1446 snippet_with_applicability(cx, bind_names, "..", &mut applicability),
1447 snippet_with_applicability(cx, matched_vars, "..", &mut applicability),
1448 " ".repeat(indent_of(cx, expr.span).unwrap_or(0)),
1449 snippet_with_applicability(cx, parent_let_node.pat.span, "..", &mut applicability),
1454 // If we are in closure, we need curly braces around suggestion
1455 let mut indent = " ".repeat(indent_of(cx, ex.span).unwrap_or(0));
1456 let (mut cbrace_start, mut cbrace_end) = ("".to_string(), "".to_string());
1457 if let Some(parent_expr) = get_parent_expr(cx, expr) {
1458 if let ExprKind::Closure(..) = parent_expr.kind {
1459 cbrace_end = format!("\n{}}}", indent);
1460 // Fix body indent due to the closure
1461 indent = " ".repeat(indent_of(cx, bind_names).unwrap_or(0));
1462 cbrace_start = format!("{{\n{}", indent);
1465 // If the parent is already an arm, and the body is another match statement,
1466 // we need curly braces around suggestion
1467 let parent_node_id = cx.tcx.hir().get_parent_node(expr.hir_id);
1468 if let Node::Arm(arm) = &cx.tcx.hir().get(parent_node_id) {
1469 if let ExprKind::Match(..) = arm.body.kind {
1470 cbrace_end = format!("\n{}}}", indent);
1471 // Fix body indent due to the match
1472 indent = " ".repeat(indent_of(cx, bind_names).unwrap_or(0));
1473 cbrace_start = format!("{{\n{}", indent);
1479 "{}let {} = {};\n{}{}{}",
1481 snippet_with_applicability(cx, bind_names, "..", &mut applicability),
1482 snippet_with_applicability(cx, matched_vars, "..", &mut applicability),
1491 MATCH_SINGLE_BINDING,
1493 "this match could be written as a `let` statement",
1494 "consider using `let` statement",
1500 if ex.can_have_side_effects() {
1501 let indent = " ".repeat(indent_of(cx, expr.span).unwrap_or(0));
1504 snippet_with_applicability(cx, ex.span, "..", &mut applicability),
1510 MATCH_SINGLE_BINDING,
1512 "this match could be replaced by its scrutinee and body",
1513 "consider using the scrutinee and body instead",
1520 MATCH_SINGLE_BINDING,
1522 "this match could be replaced by its body itself",
1523 "consider using the match body instead",
1525 Applicability::MachineApplicable,
1533 /// Returns true if the `ex` match expression is in a local (`let`) statement
1534 fn opt_parent_let<'a>(cx: &LateContext<'a>, ex: &Expr<'a>) -> Option<&'a Local<'a>> {
1535 let map = &cx.tcx.hir();
1537 if let Some(Node::Expr(parent_arm_expr)) = map.find(map.get_parent_node(ex.hir_id));
1538 if let Some(Node::Local(parent_let_expr)) = map.find(map.get_parent_node(parent_arm_expr.hir_id));
1540 return Some(parent_let_expr);
1546 /// Gets all arms that are unbounded `PatRange`s.
1547 fn all_ranges<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>], ty: Ty<'tcx>) -> Vec<SpannedRange<Constant>> {
1550 if let Arm { pat, guard: None, .. } = *arm {
1551 if let PatKind::Range(ref lhs, ref rhs, range_end) = pat.kind {
1552 let lhs = match lhs {
1553 Some(lhs) => constant(cx, cx.typeck_results(), lhs)?.0,
1554 None => miri_to_const(ty.numeric_min_val(cx.tcx)?)?,
1556 let rhs = match rhs {
1557 Some(rhs) => constant(cx, cx.typeck_results(), rhs)?.0,
1558 None => miri_to_const(ty.numeric_max_val(cx.tcx)?)?,
1560 let rhs = match range_end {
1561 RangeEnd::Included => Bound::Included(rhs),
1562 RangeEnd::Excluded => Bound::Excluded(rhs),
1564 return Some(SpannedRange {
1570 if let PatKind::Lit(value) = pat.kind {
1571 let value = constant(cx, cx.typeck_results(), value)?.0;
1572 return Some(SpannedRange {
1574 node: (value.clone(), Bound::Included(value)),
1583 #[derive(Debug, Eq, PartialEq)]
1584 pub struct SpannedRange<T> {
1586 pub node: (T, Bound<T>),
1589 type TypedRanges = Vec<SpannedRange<u128>>;
1591 /// Gets all `Int` ranges or all `Uint` ranges. Mixed types are an error anyway
1592 /// and other types than
1593 /// `Uint` and `Int` probably don't make sense.
1594 fn type_ranges(ranges: &[SpannedRange<Constant>]) -> TypedRanges {
1597 .filter_map(|range| match range.node {
1598 (Constant::Int(start), Bound::Included(Constant::Int(end))) => Some(SpannedRange {
1600 node: (start, Bound::Included(end)),
1602 (Constant::Int(start), Bound::Excluded(Constant::Int(end))) => Some(SpannedRange {
1604 node: (start, Bound::Excluded(end)),
1606 (Constant::Int(start), Bound::Unbounded) => Some(SpannedRange {
1608 node: (start, Bound::Unbounded),
1615 fn is_unit_expr(expr: &Expr<'_>) -> bool {
1617 ExprKind::Tup(v) if v.is_empty() => true,
1618 ExprKind::Block(b, _) if b.stmts.is_empty() && b.expr.is_none() => true,
1623 // Checks if arm has the form `None => None`
1624 fn is_none_arm(cx: &LateContext<'_>, arm: &Arm<'_>) -> bool {
1625 matches!(arm.pat.kind, PatKind::Path(ref qpath) if is_lang_ctor(cx, qpath, OptionNone))
1628 // Checks if arm has the form `Some(ref v) => Some(v)` (checks for `ref` and `ref mut`)
1629 fn is_ref_some_arm(cx: &LateContext<'_>, arm: &Arm<'_>) -> Option<BindingAnnotation> {
1631 if let PatKind::TupleStruct(ref qpath, [first_pat, ..], _) = arm.pat.kind;
1632 if is_lang_ctor(cx, qpath, OptionSome);
1633 if let PatKind::Binding(rb, .., ident, _) = first_pat.kind;
1634 if rb == BindingAnnotation::Ref || rb == BindingAnnotation::RefMut;
1635 if let ExprKind::Call(e, args) = remove_blocks(arm.body).kind;
1636 if let ExprKind::Path(ref some_path) = e.kind;
1637 if is_lang_ctor(cx, some_path, OptionSome) && args.len() == 1;
1638 if let ExprKind::Path(QPath::Resolved(_, path2)) = args[0].kind;
1639 if path2.segments.len() == 1 && ident.name == path2.segments[0].ident.name;
1647 fn has_only_ref_pats(arms: &[Arm<'_>]) -> bool {
1652 PatKind::Ref(..) => Some(true), // &-patterns
1653 PatKind::Wild => Some(false), // an "anything" wildcard is also fine
1654 _ => None, // any other pattern is not fine
1657 .collect::<Option<Vec<bool>>>();
1658 // look for Some(v) where there's at least one true element
1659 mapped.map_or(false, |v| v.iter().any(|el| *el))
1662 pub fn overlapping<T>(ranges: &[SpannedRange<T>]) -> Option<(&SpannedRange<T>, &SpannedRange<T>)>
1666 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
1668 Start(T, &'a SpannedRange<T>),
1669 End(Bound<T>, &'a SpannedRange<T>),
1672 impl<'a, T: Copy> Kind<'a, T> {
1673 fn range(&self) -> &'a SpannedRange<T> {
1675 Kind::Start(_, r) | Kind::End(_, r) => r,
1679 fn value(self) -> Bound<T> {
1681 Kind::Start(t, _) => Bound::Included(t),
1682 Kind::End(t, _) => t,
1687 impl<'a, T: Copy + Ord> PartialOrd for Kind<'a, T> {
1688 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1689 Some(self.cmp(other))
1693 impl<'a, T: Copy + Ord> Ord for Kind<'a, T> {
1694 fn cmp(&self, other: &Self) -> Ordering {
1695 match (self.value(), other.value()) {
1696 (Bound::Included(a), Bound::Included(b)) | (Bound::Excluded(a), Bound::Excluded(b)) => a.cmp(&b),
1697 // Range patterns cannot be unbounded (yet)
1698 (Bound::Unbounded, _) | (_, Bound::Unbounded) => unimplemented!(),
1699 (Bound::Included(a), Bound::Excluded(b)) => match a.cmp(&b) {
1700 Ordering::Equal => Ordering::Greater,
1703 (Bound::Excluded(a), Bound::Included(b)) => match a.cmp(&b) {
1704 Ordering::Equal => Ordering::Less,
1711 let mut values = Vec::with_capacity(2 * ranges.len());
1714 values.push(Kind::Start(r.node.0, r));
1715 values.push(Kind::End(r.node.1, r));
1720 for (a, b) in iter::zip(&values, values.iter().skip(1)) {
1722 (&Kind::Start(_, ra), &Kind::End(_, rb)) => {
1723 if ra.node != rb.node {
1724 return Some((ra, rb));
1727 (&Kind::End(a, _), &Kind::Start(b, _)) if a != Bound::Included(b) => (),
1729 // skip if the range `a` is completely included into the range `b`
1730 if let Ordering::Equal | Ordering::Less = a.cmp(b) {
1731 let kind_a = Kind::End(a.range().node.1, a.range());
1732 let kind_b = Kind::End(b.range().node.1, b.range());
1733 if let Ordering::Equal | Ordering::Greater = kind_a.cmp(&kind_b) {
1737 return Some((a.range(), b.range()));
1745 mod redundant_pattern_match {
1746 use super::REDUNDANT_PATTERN_MATCHING;
1747 use clippy_utils::diagnostics::span_lint_and_then;
1748 use clippy_utils::source::{snippet, snippet_with_applicability};
1749 use clippy_utils::ty::{implements_trait, is_type_diagnostic_item, is_type_lang_item, match_type};
1750 use clippy_utils::{is_lang_ctor, is_qpath_def_path, is_trait_method, paths};
1751 use if_chain::if_chain;
1752 use rustc_ast::ast::LitKind;
1753 use rustc_data_structures::fx::FxHashSet;
1754 use rustc_errors::Applicability;
1755 use rustc_hir::LangItem::{OptionNone, OptionSome, PollPending, PollReady, ResultErr, ResultOk};
1757 intravisit::{walk_expr, ErasedMap, NestedVisitorMap, Visitor},
1758 Arm, Block, Expr, ExprKind, LangItem, MatchSource, Node, PatKind, QPath,
1760 use rustc_lint::LateContext;
1761 use rustc_middle::ty::{self, subst::GenericArgKind, Ty};
1762 use rustc_span::sym;
1764 pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
1765 if let ExprKind::Match(op, arms, ref match_source) = &expr.kind {
1766 match match_source {
1767 MatchSource::Normal => find_sugg_for_match(cx, expr, op, arms),
1768 MatchSource::IfLetDesugar { contains_else_clause } => {
1769 find_sugg_for_if_let(cx, expr, op, &arms[0], "if", *contains_else_clause);
1771 MatchSource::WhileLetDesugar => find_sugg_for_if_let(cx, expr, op, &arms[0], "while", false),
1777 /// Checks if the drop order for a type matters. Some std types implement drop solely to
1778 /// deallocate memory. For these types, and composites containing them, changing the drop order
1779 /// won't result in any observable side effects.
1780 fn type_needs_ordered_drop(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
1781 type_needs_ordered_drop_inner(cx, ty, &mut FxHashSet::default())
1784 fn type_needs_ordered_drop_inner(cx: &LateContext<'tcx>, ty: Ty<'tcx>, seen: &mut FxHashSet<Ty<'tcx>>) -> bool {
1785 if !seen.insert(ty) {
1788 if !ty.needs_drop(cx.tcx, cx.param_env) {
1794 .map_or(false, |id| implements_trait(cx, ty, id, &[]))
1796 // This type doesn't implement drop, so no side effects here.
1797 // Check if any component type has any.
1799 ty::Tuple(_) => ty.tuple_fields().any(|ty| type_needs_ordered_drop_inner(cx, ty, seen)),
1800 ty::Array(ty, _) => type_needs_ordered_drop_inner(cx, ty, seen),
1801 ty::Adt(adt, subs) => adt
1803 .map(|f| f.ty(cx.tcx, subs))
1804 .any(|ty| type_needs_ordered_drop_inner(cx, ty, seen)),
1808 // Check for std types which implement drop, but only for memory allocation.
1809 else if is_type_diagnostic_item(cx, ty, sym::vec_type)
1810 || is_type_lang_item(cx, ty, LangItem::OwnedBox)
1811 || is_type_diagnostic_item(cx, ty, sym::Rc)
1812 || is_type_diagnostic_item(cx, ty, sym::Arc)
1813 || is_type_diagnostic_item(cx, ty, sym::cstring_type)
1814 || is_type_diagnostic_item(cx, ty, sym::BTreeMap)
1815 || is_type_diagnostic_item(cx, ty, sym::LinkedList)
1816 || match_type(cx, ty, &paths::WEAK_RC)
1817 || match_type(cx, ty, &paths::WEAK_ARC)
1819 // Check all of the generic arguments.
1820 if let ty::Adt(_, subs) = ty.kind() {
1821 subs.types().any(|ty| type_needs_ordered_drop_inner(cx, ty, seen))
1830 // Extract the generic arguments out of a type
1831 fn try_get_generic_ty(ty: Ty<'_>, index: usize) -> Option<Ty<'_>> {
1833 if let ty::Adt(_, subs) = ty.kind();
1834 if let Some(sub) = subs.get(index);
1835 if let GenericArgKind::Type(sub_ty) = sub.unpack();
1844 // Checks if there are any temporaries created in the given expression for which drop order
1846 fn temporaries_need_ordered_drop(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
1847 struct V<'a, 'tcx> {
1848 cx: &'a LateContext<'tcx>,
1851 impl<'a, 'tcx> Visitor<'tcx> for V<'a, 'tcx> {
1852 type Map = ErasedMap<'tcx>;
1853 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
1854 NestedVisitorMap::None
1857 fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
1859 // Taking the reference of a value leaves a temporary
1860 // e.g. In `&String::new()` the string is a temporary value.
1861 // Remaining fields are temporary values
1862 // e.g. In `(String::new(), 0).1` the string is a temporary value.
1863 ExprKind::AddrOf(_, _, expr) | ExprKind::Field(expr, _) => {
1864 if !matches!(expr.kind, ExprKind::Path(_)) {
1865 if type_needs_ordered_drop(self.cx, self.cx.typeck_results().expr_ty(expr)) {
1868 self.visit_expr(expr);
1872 // the base type is alway taken by reference.
1873 // e.g. In `(vec![0])[0]` the vector is a temporary value.
1874 ExprKind::Index(base, index) => {
1875 if !matches!(base.kind, ExprKind::Path(_)) {
1876 if type_needs_ordered_drop(self.cx, self.cx.typeck_results().expr_ty(base)) {
1879 self.visit_expr(base);
1882 self.visit_expr(index);
1884 // Method calls can take self by reference.
1885 // e.g. In `String::new().len()` the string is a temporary value.
1886 ExprKind::MethodCall(_, _, [self_arg, args @ ..], _) => {
1887 if !matches!(self_arg.kind, ExprKind::Path(_)) {
1888 let self_by_ref = self
1891 .type_dependent_def_id(expr.hir_id)
1892 .map_or(false, |id| self.cx.tcx.fn_sig(id).skip_binder().inputs()[0].is_ref());
1894 && type_needs_ordered_drop(self.cx, self.cx.typeck_results().expr_ty(self_arg))
1898 self.visit_expr(self_arg);
1901 args.iter().for_each(|arg| self.visit_expr(arg));
1903 // Either explicitly drops values, or changes control flow.
1904 ExprKind::DropTemps(_)
1906 | ExprKind::Break(..)
1907 | ExprKind::Yield(..)
1908 | ExprKind::Block(Block { expr: None, .. }, _)
1909 | ExprKind::Loop(..) => (),
1911 // Only consider the final expression.
1912 ExprKind::Block(Block { expr: Some(expr), .. }, _) => self.visit_expr(expr),
1914 _ => walk_expr(self, expr),
1919 let mut v = V { cx, res: false };
1924 fn find_sugg_for_if_let<'tcx>(
1925 cx: &LateContext<'tcx>,
1926 expr: &'tcx Expr<'_>,
1927 op: &'tcx Expr<'tcx>,
1929 keyword: &'static str,
1932 // also look inside refs
1933 let mut kind = &arm.pat.kind;
1934 // if we have &None for example, peel it so we can detect "if let None = x"
1935 if let PatKind::Ref(inner, _mutability) = kind {
1938 let op_ty = cx.typeck_results().expr_ty(op);
1939 // Determine which function should be used, and the type contained by the corresponding
1941 let (good_method, inner_ty) = match kind {
1942 PatKind::TupleStruct(ref path, [sub_pat], _) => {
1943 if let PatKind::Wild = sub_pat.kind {
1944 if is_lang_ctor(cx, path, ResultOk) {
1945 ("is_ok()", try_get_generic_ty(op_ty, 0).unwrap_or(op_ty))
1946 } else if is_lang_ctor(cx, path, ResultErr) {
1947 ("is_err()", try_get_generic_ty(op_ty, 1).unwrap_or(op_ty))
1948 } else if is_lang_ctor(cx, path, OptionSome) {
1949 ("is_some()", op_ty)
1950 } else if is_lang_ctor(cx, path, PollReady) {
1951 ("is_ready()", op_ty)
1952 } else if is_qpath_def_path(cx, path, sub_pat.hir_id, &paths::IPADDR_V4) {
1953 ("is_ipv4()", op_ty)
1954 } else if is_qpath_def_path(cx, path, sub_pat.hir_id, &paths::IPADDR_V6) {
1955 ("is_ipv6()", op_ty)
1963 PatKind::Path(ref path) => {
1964 let method = if is_lang_ctor(cx, path, OptionNone) {
1966 } else if is_lang_ctor(cx, path, PollPending) {
1971 // `None` and `Pending` don't have an inner type.
1972 (method, cx.tcx.types.unit)
1977 // If this is the last expression in a block or there is an else clause then the whole
1978 // type needs to be considered, not just the inner type of the branch being matched on.
1979 // Note the last expression in a block is dropped after all local bindings.
1980 let check_ty = if has_else
1981 || (keyword == "if" && matches!(cx.tcx.hir().parent_iter(expr.hir_id).next(), Some((_, Node::Block(..)))))
1988 // All temporaries created in the scrutinee expression are dropped at the same time as the
1989 // scrutinee would be, so they have to be considered as well.
1990 // e.g. in `if let Some(x) = foo.lock().unwrap().baz.as_ref() { .. }` the lock will be held
1991 // for the duration if body.
1992 let needs_drop = type_needs_ordered_drop(cx, check_ty) || temporaries_need_ordered_drop(cx, op);
1994 // check that `while_let_on_iterator` lint does not trigger
1996 if keyword == "while";
1997 if let ExprKind::MethodCall(method_path, _, _, _) = op.kind;
1998 if method_path.ident.name == sym::next;
1999 if is_trait_method(cx, op, sym::Iterator);
2005 let result_expr = match &op.kind {
2006 ExprKind::AddrOf(_, _, borrowed) => borrowed,
2011 REDUNDANT_PATTERN_MATCHING,
2013 &format!("redundant pattern matching, consider using `{}`", good_method),
2015 // while let ... = ... { ... }
2016 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2017 let expr_span = expr.span;
2019 // while let ... = ... { ... }
2021 let op_span = result_expr.span.source_callsite();
2023 // while let ... = ... { ... }
2024 // ^^^^^^^^^^^^^^^^^^^
2025 let span = expr_span.until(op_span.shrink_to_hi());
2027 let mut app = if needs_drop {
2028 Applicability::MaybeIncorrect
2030 Applicability::MachineApplicable
2032 let sugg = snippet_with_applicability(cx, op_span, "_", &mut app);
2034 diag.span_suggestion(span, "try this", format!("{} {}.{}", keyword, sugg, good_method), app);
2037 diag.note("this will change drop order of the result, as well as all temporaries");
2038 diag.note("add `#[allow(clippy::redundant_pattern_matching)]` if this is important");
2044 fn find_sugg_for_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op: &Expr<'_>, arms: &[Arm<'_>]) {
2045 if arms.len() == 2 {
2046 let node_pair = (&arms[0].pat.kind, &arms[1].pat.kind);
2048 let found_good_method = match node_pair {
2050 PatKind::TupleStruct(ref path_left, patterns_left, _),
2051 PatKind::TupleStruct(ref path_right, patterns_right, _),
2052 ) if patterns_left.len() == 1 && patterns_right.len() == 1 => {
2053 if let (PatKind::Wild, PatKind::Wild) = (&patterns_left[0].kind, &patterns_right[0].kind) {
2054 find_good_method_for_match(
2065 find_good_method_for_match(
2080 (PatKind::TupleStruct(ref path_left, patterns, _), PatKind::Path(ref path_right))
2081 | (PatKind::Path(ref path_left), PatKind::TupleStruct(ref path_right, patterns, _))
2082 if patterns.len() == 1 =>
2084 if let PatKind::Wild = patterns[0].kind {
2085 find_good_method_for_match(
2090 &paths::OPTION_SOME,
2091 &paths::OPTION_NONE,
2096 find_good_method_for_match(
2102 &paths::POLL_PENDING,
2114 if let Some(good_method) = found_good_method {
2115 let span = expr.span.to(op.span);
2116 let result_expr = match &op.kind {
2117 ExprKind::AddrOf(_, _, borrowed) => borrowed,
2122 REDUNDANT_PATTERN_MATCHING,
2124 &format!("redundant pattern matching, consider using `{}`", good_method),
2126 diag.span_suggestion(
2129 format!("{}.{}", snippet(cx, result_expr.span, "_"), good_method),
2130 Applicability::MaybeIncorrect, // snippet
2138 #[allow(clippy::too_many_arguments)]
2139 fn find_good_method_for_match<'a>(
2140 cx: &LateContext<'_>,
2142 path_left: &QPath<'_>,
2143 path_right: &QPath<'_>,
2144 expected_left: &[&str],
2145 expected_right: &[&str],
2146 should_be_left: &'a str,
2147 should_be_right: &'a str,
2148 ) -> Option<&'a str> {
2149 let body_node_pair = if is_qpath_def_path(cx, path_left, arms[0].pat.hir_id, expected_left)
2150 && is_qpath_def_path(cx, path_right, arms[1].pat.hir_id, expected_right)
2152 (&(*arms[0].body).kind, &(*arms[1].body).kind)
2153 } else if is_qpath_def_path(cx, path_right, arms[1].pat.hir_id, expected_left)
2154 && is_qpath_def_path(cx, path_left, arms[0].pat.hir_id, expected_right)
2156 (&(*arms[1].body).kind, &(*arms[0].body).kind)
2161 match body_node_pair {
2162 (ExprKind::Lit(ref lit_left), ExprKind::Lit(ref lit_right)) => match (&lit_left.node, &lit_right.node) {
2163 (LitKind::Bool(true), LitKind::Bool(false)) => Some(should_be_left),
2164 (LitKind::Bool(false), LitKind::Bool(true)) => Some(should_be_right),
2173 fn test_overlapping() {
2174 use rustc_span::source_map::DUMMY_SP;
2176 let sp = |s, e| SpannedRange {
2181 assert_eq!(None, overlapping::<u8>(&[]));
2182 assert_eq!(None, overlapping(&[sp(1, Bound::Included(4))]));
2185 overlapping(&[sp(1, Bound::Included(4)), sp(5, Bound::Included(6))])
2190 sp(1, Bound::Included(4)),
2191 sp(5, Bound::Included(6)),
2192 sp(10, Bound::Included(11))
2196 Some((&sp(1, Bound::Included(4)), &sp(3, Bound::Included(6)))),
2197 overlapping(&[sp(1, Bound::Included(4)), sp(3, Bound::Included(6))])
2200 Some((&sp(5, Bound::Included(6)), &sp(6, Bound::Included(11)))),
2202 sp(1, Bound::Included(4)),
2203 sp(5, Bound::Included(6)),
2204 sp(6, Bound::Included(11))
2209 /// Implementation of `MATCH_SAME_ARMS`.
2210 fn lint_match_arms<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) {
2211 if let ExprKind::Match(_, arms, MatchSource::Normal) = expr.kind {
2212 let hash = |&(_, arm): &(usize, &Arm<'_>)| -> u64 {
2213 let mut h = SpanlessHash::new(cx);
2214 h.hash_expr(arm.body);
2218 let eq = |&(lindex, lhs): &(usize, &Arm<'_>), &(rindex, rhs): &(usize, &Arm<'_>)| -> bool {
2219 let min_index = usize::min(lindex, rindex);
2220 let max_index = usize::max(lindex, rindex);
2222 let mut local_map: HirIdMap<HirId> = HirIdMap::default();
2223 let eq_fallback = |a: &Expr<'_>, b: &Expr<'_>| {
2225 if let Some(a_id) = path_to_local(a);
2226 if let Some(b_id) = path_to_local(b);
2227 let entry = match local_map.entry(a_id) {
2228 Entry::Vacant(entry) => entry,
2229 // check if using the same bindings as before
2230 Entry::Occupied(entry) => return *entry.get() == b_id,
2232 // the names technically don't have to match; this makes the lint more conservative
2233 if cx.tcx.hir().name(a_id) == cx.tcx.hir().name(b_id);
2234 if TyS::same_type(cx.typeck_results().expr_ty(a), cx.typeck_results().expr_ty(b));
2235 if pat_contains_local(lhs.pat, a_id);
2236 if pat_contains_local(rhs.pat, b_id);
2245 // Arms with a guard are ignored, those can’t always be merged together
2246 // This is also the case for arms in-between each there is an arm with a guard
2247 (min_index..=max_index).all(|index| arms[index].guard.is_none())
2248 && SpanlessEq::new(cx)
2249 .expr_fallback(eq_fallback)
2250 .eq_expr(lhs.body, rhs.body)
2251 // these checks could be removed to allow unused bindings
2252 && bindings_eq(lhs.pat, local_map.keys().copied().collect())
2253 && bindings_eq(rhs.pat, local_map.values().copied().collect())
2256 let indexed_arms: Vec<(usize, &Arm<'_>)> = arms.iter().enumerate().collect();
2257 for (&(_, i), &(_, j)) in search_same(&indexed_arms, hash, eq) {
2262 "this `match` has identical arm bodies",
2264 diag.span_note(i.body.span, "same as this");
2266 // Note: this does not use `span_suggestion` on purpose:
2267 // there is no clean way
2268 // to remove the other arm. Building a span and suggest to replace it to ""
2269 // makes an even more confusing error message. Also in order not to make up a
2270 // span for the whole pattern, the suggestion is only shown when there is only
2271 // one pattern. The user should know about `|` if they are already using it…
2273 let lhs = snippet(cx, i.pat.span, "<pat1>");
2274 let rhs = snippet(cx, j.pat.span, "<pat2>");
2276 if let PatKind::Wild = j.pat.kind {
2277 // if the last arm is _, then i could be integrated into _
2278 // note that i.pat cannot be _, because that would mean that we're
2279 // hiding all the subsequent arms, and rust won't compile
2283 "`{}` has the same arm body as the `_` wildcard, consider removing it",
2288 diag.span_help(i.pat.span, &format!("consider refactoring into `{} | {}`", lhs, rhs,))
2289 .help("...or consider changing the match arm bodies");
2297 fn pat_contains_local(pat: &Pat<'_>, id: HirId) -> bool {
2298 let mut result = false;
2299 pat.walk_short(|p| {
2300 result |= matches!(p.kind, PatKind::Binding(_, binding_id, ..) if binding_id == id);
2306 /// Returns true if all the bindings in the `Pat` are in `ids` and vice versa
2307 fn bindings_eq(pat: &Pat<'_>, mut ids: HirIdSet) -> bool {
2308 let mut result = true;
2309 pat.each_binding_or_first(&mut |_, id, _, _| result &= ids.remove(&id));
2310 result && ids.is_empty()