1 use clippy_utils::consts::{constant, constant_full_int, miri_to_const, FullInt};
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::higher;
6 use clippy_utils::source::{expr_block, indent_of, snippet, snippet_block, snippet_opt, snippet_with_applicability};
7 use clippy_utils::sugg::Sugg;
8 use clippy_utils::ty::{implements_trait, is_type_diagnostic_item, match_type, peel_mid_ty_refs};
9 use clippy_utils::visitors::is_local_used;
11 get_parent_expr, in_macro, is_expn_of, is_lang_ctor, is_lint_allowed, is_refutable, is_unit_expr, is_wild,
12 meets_msrv, msrvs, path_to_local, path_to_local_id, peel_hir_pat_refs, peel_n_hir_expr_refs, recurse_or_patterns,
13 remove_blocks, strip_pat_refs,
15 use clippy_utils::{paths, search_same, SpanlessEq, SpanlessHash};
16 use core::iter::{once, ExactSizeIterator};
17 use if_chain::if_chain;
18 use rustc_ast::ast::{Attribute, LitKind};
19 use rustc_errors::Applicability;
20 use rustc_hir::def::{CtorKind, DefKind, Res};
21 use rustc_hir::LangItem::{OptionNone, OptionSome};
23 self as hir, Arm, BindingAnnotation, Block, BorrowKind, Expr, ExprKind, Guard, HirId, Local, MatchSource,
24 Mutability, Node, Pat, PatKind, PathSegment, QPath, RangeEnd, TyKind,
26 use rustc_hir::{HirIdMap, HirIdSet};
27 use rustc_lint::{LateContext, LateLintPass, LintContext};
28 use rustc_middle::lint::in_external_macro;
29 use rustc_middle::ty::{self, Ty, TyS, VariantDef};
30 use rustc_semver::RustcVersion;
31 use rustc_session::{declare_tool_lint, impl_lint_pass};
32 use rustc_span::source_map::{Span, Spanned};
34 use std::cmp::Ordering;
35 use std::collections::hash_map::Entry;
39 declare_clippy_lint! {
41 /// Checks for matches with a single arm where an `if let`
42 /// will usually suffice.
44 /// ### Why is this bad?
45 /// Just readability – `if let` nests less than a `match`.
49 /// # fn bar(stool: &str) {}
50 /// # let x = Some("abc");
53 /// Some(ref foo) => bar(foo),
58 /// if let Some(ref foo) = x {
64 "a `match` statement with a single nontrivial arm (i.e., where the other arm is `_ => {}`) instead of `if let`"
67 declare_clippy_lint! {
69 /// Checks for matches with two arms where an `if let else` will
72 /// ### Why is this bad?
73 /// Just readability – `if let` nests less than a `match`.
75 /// ### Known problems
76 /// Personal style preferences may differ.
82 /// # fn bar(foo: &usize) {}
83 /// # let other_ref: usize = 1;
84 /// # let x: Option<&usize> = Some(&1);
86 /// Some(ref foo) => bar(foo),
87 /// _ => bar(&other_ref),
91 /// Using `if let` with `else`:
94 /// # fn bar(foo: &usize) {}
95 /// # let other_ref: usize = 1;
96 /// # let x: Option<&usize> = Some(&1);
97 /// if let Some(ref foo) = x {
103 pub SINGLE_MATCH_ELSE,
105 "a `match` statement with two arms where the second arm's pattern is a placeholder instead of a specific match pattern"
108 declare_clippy_lint! {
110 /// Checks for matches where all arms match a reference,
111 /// suggesting to remove the reference and deref the matched expression
112 /// instead. It also checks for `if let &foo = bar` blocks.
114 /// ### Why is this bad?
115 /// It just makes the code less readable. That reference
116 /// destructuring adds nothing to the code.
122 /// &A(ref y) => foo(y),
129 /// A(ref y) => foo(y),
136 "a `match` or `if let` with all arms prefixed with `&` instead of deref-ing the match expression"
139 declare_clippy_lint! {
141 /// Checks for matches where match expression is a `bool`. It
142 /// suggests to replace the expression with an `if...else` block.
144 /// ### Why is this bad?
145 /// It makes the code less readable.
151 /// let condition: bool = true;
152 /// match condition {
157 /// Use if/else instead:
161 /// let condition: bool = true;
170 "a `match` on a boolean expression instead of an `if..else` block"
173 declare_clippy_lint! {
175 /// Checks for overlapping match arms.
177 /// ### Why is this bad?
178 /// It is likely to be an error and if not, makes the code
185 /// 1..=10 => println!("1 ... 10"),
186 /// 5..=15 => println!("5 ... 15"),
190 pub MATCH_OVERLAPPING_ARM,
192 "a `match` with overlapping arms"
195 declare_clippy_lint! {
197 /// Checks for arm which matches all errors with `Err(_)`
198 /// and take drastic actions like `panic!`.
200 /// ### Why is this bad?
201 /// It is generally a bad practice, similar to
202 /// catching all exceptions in java with `catch(Exception)`
206 /// let x: Result<i32, &str> = Ok(3);
208 /// Ok(_) => println!("ok"),
209 /// Err(_) => panic!("err"),
212 pub MATCH_WILD_ERR_ARM,
214 "a `match` with `Err(_)` arm and take drastic actions"
217 declare_clippy_lint! {
219 /// Checks for match which is used to add a reference to an
222 /// ### Why is this bad?
223 /// Using `as_ref()` or `as_mut()` instead is shorter.
227 /// let x: Option<()> = None;
230 /// let r: Option<&()> = match x {
232 /// Some(ref v) => Some(v),
236 /// let r: Option<&()> = x.as_ref();
240 "a `match` on an Option value instead of using `as_ref()` or `as_mut`"
243 declare_clippy_lint! {
245 /// Checks for wildcard enum matches using `_`.
247 /// ### Why is this bad?
248 /// New enum variants added by library updates can be missed.
250 /// ### Known problems
251 /// Suggested replacements may be incorrect if guards exhaustively cover some
252 /// variants, and also may not use correct path to enum if it's not present in the current scope.
256 /// # enum Foo { A(usize), B(usize) }
257 /// # let x = Foo::B(1);
270 pub WILDCARD_ENUM_MATCH_ARM,
272 "a wildcard enum match arm using `_`"
275 declare_clippy_lint! {
277 /// Checks for wildcard enum matches for a single variant.
279 /// ### Why is this bad?
280 /// New enum variants added by library updates can be missed.
282 /// ### Known problems
283 /// Suggested replacements may not use correct path to enum
284 /// if it's not present in the current scope.
288 /// # enum Foo { A, B, C }
289 /// # let x = Foo::B;
304 pub MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
306 "a wildcard enum match for a single variant"
309 declare_clippy_lint! {
311 /// Checks for wildcard pattern used with others patterns in same match arm.
313 /// ### Why is this bad?
314 /// Wildcard pattern already covers any other pattern as it will match anyway.
315 /// It makes the code less readable, especially to spot wildcard pattern use in match arm.
331 pub WILDCARD_IN_OR_PATTERNS,
333 "a wildcard pattern used with others patterns in same match arm"
336 declare_clippy_lint! {
338 /// Checks for matches being used to destructure a single-variant enum
339 /// or tuple struct where a `let` will suffice.
341 /// ### Why is this bad?
342 /// Just readability – `let` doesn't nest, whereas a `match` does.
350 /// let wrapper = Wrapper::Data(42);
352 /// let data = match wrapper {
353 /// Wrapper::Data(i) => i,
357 /// The correct use would be:
363 /// let wrapper = Wrapper::Data(42);
364 /// let Wrapper::Data(data) = wrapper;
366 pub INFALLIBLE_DESTRUCTURING_MATCH,
368 "a `match` statement with a single infallible arm instead of a `let`"
371 declare_clippy_lint! {
373 /// Checks for useless match that binds to only one value.
375 /// ### Why is this bad?
376 /// Readability and needless complexity.
378 /// ### Known problems
379 /// Suggested replacements may be incorrect when `match`
380 /// is actually binding temporary value, bringing a 'dropped while borrowed' error.
395 /// let (c, d) = (a, b);
397 pub MATCH_SINGLE_BINDING,
399 "a match with a single binding instead of using `let` statement"
402 declare_clippy_lint! {
404 /// Checks for unnecessary '..' pattern binding on struct when all fields are explicitly matched.
406 /// ### Why is this bad?
407 /// Correctness and readability. It's like having a wildcard pattern after
408 /// matching all enum variants explicitly.
412 /// # struct A { a: i32 }
413 /// let a = A { a: 5 };
417 /// A { a: 5, .. } => {},
423 /// A { a: 5 } => {},
427 pub REST_PAT_IN_FULLY_BOUND_STRUCTS,
429 "a match on a struct that binds all fields but still uses the wildcard pattern"
432 declare_clippy_lint! {
434 /// Lint for redundant pattern matching over `Result`, `Option`,
435 /// `std::task::Poll` or `std::net::IpAddr`
437 /// ### Why is this bad?
438 /// It's more concise and clear to just use the proper
441 /// ### Known problems
442 /// This will change the drop order for the matched type. Both `if let` and
443 /// `while let` will drop the value at the end of the block, both `if` and `while` will drop the
444 /// value before entering the block. For most types this change will not matter, but for a few
445 /// types this will not be an acceptable change (e.g. locks). See the
446 /// [reference](https://doc.rust-lang.org/reference/destructors.html#drop-scopes) for more about
451 /// # use std::task::Poll;
452 /// # use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
453 /// if let Ok(_) = Ok::<i32, i32>(42) {}
454 /// if let Err(_) = Err::<i32, i32>(42) {}
455 /// if let None = None::<()> {}
456 /// if let Some(_) = Some(42) {}
457 /// if let Poll::Pending = Poll::Pending::<()> {}
458 /// if let Poll::Ready(_) = Poll::Ready(42) {}
459 /// if let IpAddr::V4(_) = IpAddr::V4(Ipv4Addr::LOCALHOST) {}
460 /// if let IpAddr::V6(_) = IpAddr::V6(Ipv6Addr::LOCALHOST) {}
461 /// match Ok::<i32, i32>(42) {
467 /// The more idiomatic use would be:
470 /// # use std::task::Poll;
471 /// # use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
472 /// if Ok::<i32, i32>(42).is_ok() {}
473 /// if Err::<i32, i32>(42).is_err() {}
474 /// if None::<()>.is_none() {}
475 /// if Some(42).is_some() {}
476 /// if Poll::Pending::<()>.is_pending() {}
477 /// if Poll::Ready(42).is_ready() {}
478 /// if IpAddr::V4(Ipv4Addr::LOCALHOST).is_ipv4() {}
479 /// if IpAddr::V6(Ipv6Addr::LOCALHOST).is_ipv6() {}
480 /// Ok::<i32, i32>(42).is_ok();
482 pub REDUNDANT_PATTERN_MATCHING,
484 "use the proper utility function avoiding an `if let`"
487 declare_clippy_lint! {
489 /// Checks for `match` or `if let` expressions producing a
490 /// `bool` that could be written using `matches!`
492 /// ### Why is this bad?
493 /// Readability and needless complexity.
495 /// ### Known problems
496 /// This lint falsely triggers, if there are arms with
497 /// `cfg` attributes that remove an arm evaluating to `false`.
504 /// let a = match x {
509 /// let a = if let Some(0) = x {
516 /// let a = matches!(x, Some(0));
518 pub MATCH_LIKE_MATCHES_MACRO,
520 "a match that could be written with the matches! macro"
523 declare_clippy_lint! {
525 /// Checks for `match` with identical arm bodies.
527 /// ### Why is this bad?
528 /// This is probably a copy & paste error. If arm bodies
529 /// are the same on purpose, you can factor them
530 /// [using `|`](https://doc.rust-lang.org/book/patterns.html#multiple-patterns).
532 /// ### Known problems
533 /// False positive possible with order dependent `match`
535 /// [#860](https://github.com/rust-lang/rust-clippy/issues/860)).
542 /// Baz => bar(), // <= oops
546 /// This should probably be
551 /// Baz => baz(), // <= fixed
555 /// or if the original code was not a typo:
558 /// Bar | Baz => bar(), // <= shows the intent better
564 "`match` with identical arm bodies"
569 msrv: Option<RustcVersion>,
570 infallible_destructuring_match_linted: bool,
575 pub fn new(msrv: Option<RustcVersion>) -> Self {
583 impl_lint_pass!(Matches => [
588 MATCH_OVERLAPPING_ARM,
591 WILDCARD_ENUM_MATCH_ARM,
592 MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
593 WILDCARD_IN_OR_PATTERNS,
594 MATCH_SINGLE_BINDING,
595 INFALLIBLE_DESTRUCTURING_MATCH,
596 REST_PAT_IN_FULLY_BOUND_STRUCTS,
597 REDUNDANT_PATTERN_MATCHING,
598 MATCH_LIKE_MATCHES_MACRO,
602 impl<'tcx> LateLintPass<'tcx> for Matches {
603 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
604 if in_external_macro(cx.sess(), expr.span) || in_macro(expr.span) {
608 redundant_pattern_match::check(cx, expr);
610 if meets_msrv(self.msrv.as_ref(), &msrvs::MATCHES_MACRO) {
611 if !check_match_like_matches(cx, expr) {
612 lint_match_arms(cx, expr);
615 lint_match_arms(cx, expr);
618 if let ExprKind::Match(ex, arms, MatchSource::Normal) = expr.kind {
619 check_single_match(cx, ex, arms, expr);
620 check_match_bool(cx, ex, arms, expr);
621 check_overlapping_arms(cx, ex, arms);
622 check_wild_err_arm(cx, ex, arms);
623 check_wild_enum_match(cx, ex, arms);
624 check_match_as_ref(cx, ex, arms, expr);
625 check_wild_in_or_pats(cx, arms);
627 if self.infallible_destructuring_match_linted {
628 self.infallible_destructuring_match_linted = false;
630 check_match_single_binding(cx, ex, arms, expr);
633 if let ExprKind::Match(ex, arms, _) = expr.kind {
634 check_match_ref_pats(cx, ex, arms.iter().map(|el| el.pat), expr);
636 if let Some(higher::IfLet { let_pat, let_expr, .. }) = higher::IfLet::hir(cx, expr) {
637 check_match_ref_pats(cx, let_expr, once(let_pat), expr);
641 fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'_>) {
643 if !in_external_macro(cx.sess(), local.span);
644 if !in_macro(local.span);
645 if let Some(expr) = local.init;
646 if let ExprKind::Match(target, arms, MatchSource::Normal) = expr.kind;
647 if arms.len() == 1 && arms[0].guard.is_none();
648 if let PatKind::TupleStruct(
649 QPath::Resolved(None, variant_name), args, _) = arms[0].pat.kind;
651 if let PatKind::Binding(_, arg, ..) = strip_pat_refs(&args[0]).kind;
652 let body = remove_blocks(arms[0].body);
653 if path_to_local_id(body, arg);
656 let mut applicability = Applicability::MachineApplicable;
657 self.infallible_destructuring_match_linted = true;
660 INFALLIBLE_DESTRUCTURING_MATCH,
662 "you seem to be trying to use `match` to destructure a single infallible pattern. \
663 Consider using `let`",
667 snippet_with_applicability(cx, variant_name.span, "..", &mut applicability),
668 snippet_with_applicability(cx, local.pat.span, "..", &mut applicability),
669 snippet_with_applicability(cx, target.span, "..", &mut applicability),
677 fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
679 if !in_external_macro(cx.sess(), pat.span);
680 if !in_macro(pat.span);
681 if let PatKind::Struct(QPath::Resolved(_, path), fields, true) = pat.kind;
682 if let Some(def_id) = path.res.opt_def_id();
683 let ty = cx.tcx.type_of(def_id);
684 if let ty::Adt(def, _) = ty.kind();
685 if def.is_struct() || def.is_union();
686 if fields.len() == def.non_enum_variant().fields.len();
691 REST_PAT_IN_FULLY_BOUND_STRUCTS,
693 "unnecessary use of `..` pattern in struct binding. All fields were already bound",
695 "consider removing `..` from this binding",
701 extract_msrv_attr!(LateContext);
705 fn check_single_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>) {
706 if arms.len() == 2 && arms[0].guard.is_none() && arms[1].guard.is_none() {
707 if in_macro(expr.span) {
708 // Don't lint match expressions present in
709 // macro_rules! block
712 if let PatKind::Or(..) = arms[0].pat.kind {
713 // don't lint for or patterns for now, this makes
714 // the lint noisy in unnecessary situations
717 let els = arms[1].body;
718 let els = if is_unit_expr(remove_blocks(els)) {
720 } else if let ExprKind::Block(Block { stmts, expr: block_expr, .. }, _) = els.kind {
721 if stmts.len() == 1 && block_expr.is_none() || stmts.is_empty() && block_expr.is_some() {
722 // single statement/expr "else" block, don't lint
725 // block with 2+ statements or 1 expr and 1+ statement
728 // not a block, don't lint
732 let ty = cx.typeck_results().expr_ty(ex);
733 if *ty.kind() != ty::Bool || is_lint_allowed(cx, MATCH_BOOL, ex.hir_id) {
734 check_single_match_single_pattern(cx, ex, arms, expr, els);
735 check_single_match_opt_like(cx, ex, arms, expr, ty, els);
740 fn check_single_match_single_pattern(
741 cx: &LateContext<'_>,
745 els: Option<&Expr<'_>>,
747 if is_wild(arms[1].pat) {
748 report_single_match_single_pattern(cx, ex, arms, expr, els);
752 fn report_single_match_single_pattern(
753 cx: &LateContext<'_>,
757 els: Option<&Expr<'_>>,
759 let lint = if els.is_some() { SINGLE_MATCH_ELSE } else { SINGLE_MATCH };
760 let els_str = els.map_or(String::new(), |els| {
761 format!(" else {}", expr_block(cx, els, None, "..", Some(expr.span)))
764 let (pat, pat_ref_count) = peel_hir_pat_refs(arms[0].pat);
765 let (msg, sugg) = if_chain! {
766 if let PatKind::Path(_) | PatKind::Lit(_) = pat.kind;
767 let (ty, ty_ref_count) = peel_mid_ty_refs(cx.typeck_results().expr_ty(ex));
768 if let Some(spe_trait_id) = cx.tcx.lang_items().structural_peq_trait();
769 if let Some(pe_trait_id) = cx.tcx.lang_items().eq_trait();
770 if ty.is_integral() || ty.is_char() || ty.is_str()
771 || (implements_trait(cx, ty, spe_trait_id, &[])
772 && implements_trait(cx, ty, pe_trait_id, &[ty.into()]));
774 // scrutinee derives PartialEq and the pattern is a constant.
775 let pat_ref_count = match pat.kind {
776 // string literals are already a reference.
777 PatKind::Lit(Expr { kind: ExprKind::Lit(lit), .. }) if lit.node.is_str() => pat_ref_count + 1,
780 // References are only implicitly added to the pattern, so no overflow here.
781 // e.g. will work: match &Some(_) { Some(_) => () }
782 // will not: match Some(_) { &Some(_) => () }
783 let ref_count_diff = ty_ref_count - pat_ref_count;
785 // Try to remove address of expressions first.
786 let (ex, removed) = peel_n_hir_expr_refs(ex, ref_count_diff);
787 let ref_count_diff = ref_count_diff - removed;
789 let msg = "you seem to be trying to use `match` for an equality check. Consider using `if`";
791 "if {} == {}{} {}{}",
792 snippet(cx, ex.span, ".."),
793 // PartialEq for different reference counts may not exist.
794 "&".repeat(ref_count_diff),
795 snippet(cx, arms[0].pat.span, ".."),
796 expr_block(cx, arms[0].body, None, "..", Some(expr.span)),
801 let msg = "you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`";
803 "if let {} = {} {}{}",
804 snippet(cx, arms[0].pat.span, ".."),
805 snippet(cx, ex.span, ".."),
806 expr_block(cx, arms[0].body, None, "..", Some(expr.span)),
820 Applicability::HasPlaceholders,
824 fn check_single_match_opt_like(
825 cx: &LateContext<'_>,
830 els: Option<&Expr<'_>>,
832 // list of candidate `Enum`s we know will never get any more members
834 (&paths::COW, "Borrowed"),
835 (&paths::COW, "Cow::Borrowed"),
836 (&paths::COW, "Cow::Owned"),
837 (&paths::COW, "Owned"),
838 (&paths::OPTION, "None"),
839 (&paths::RESULT, "Err"),
840 (&paths::RESULT, "Ok"),
843 let path = match arms[1].pat.kind {
844 PatKind::TupleStruct(ref path, inner, _) => {
845 // Contains any non wildcard patterns (e.g., `Err(err)`)?
846 if !inner.iter().all(is_wild) {
849 rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false))
851 PatKind::Binding(BindingAnnotation::Unannotated, .., ident, None) => ident.to_string(),
852 PatKind::Path(ref path) => {
853 rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false))
858 for &(ty_path, pat_path) in candidates {
859 if path == *pat_path && match_type(cx, ty, ty_path) {
860 report_single_match_single_pattern(cx, ex, arms, expr, els);
865 fn check_match_bool(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>) {
866 // Type of expression is `bool`.
867 if *cx.typeck_results().expr_ty(ex).kind() == ty::Bool {
872 "you seem to be trying to match on a boolean expression",
876 let exprs = if let PatKind::Lit(arm_bool) = arms[0].pat.kind {
877 if let ExprKind::Lit(ref lit) = arm_bool.kind {
879 LitKind::Bool(true) => Some((&*arms[0].body, &*arms[1].body)),
880 LitKind::Bool(false) => Some((&*arms[1].body, &*arms[0].body)),
890 if let Some((true_expr, false_expr)) = exprs {
891 let sugg = match (is_unit_expr(true_expr), is_unit_expr(false_expr)) {
892 (false, false) => Some(format!(
894 snippet(cx, ex.span, "b"),
895 expr_block(cx, true_expr, None, "..", Some(expr.span)),
896 expr_block(cx, false_expr, None, "..", Some(expr.span))
898 (false, true) => Some(format!(
900 snippet(cx, ex.span, "b"),
901 expr_block(cx, true_expr, None, "..", Some(expr.span))
904 let test = Sugg::hir(cx, ex, "..");
908 expr_block(cx, false_expr, None, "..", Some(expr.span))
911 (true, true) => None,
914 if let Some(sugg) = sugg {
915 diag.span_suggestion(
917 "consider using an `if`/`else` expression",
919 Applicability::HasPlaceholders,
929 fn check_overlapping_arms<'tcx>(cx: &LateContext<'tcx>, ex: &'tcx Expr<'_>, arms: &'tcx [Arm<'_>]) {
930 if arms.len() >= 2 && cx.typeck_results().expr_ty(ex).is_integral() {
931 let ranges = all_ranges(cx, arms, cx.typeck_results().expr_ty(ex));
932 if !ranges.is_empty() {
933 if let Some((start, end)) = overlapping(&ranges) {
936 MATCH_OVERLAPPING_ARM,
938 "some ranges overlap",
940 "overlaps with this",
947 fn check_wild_err_arm<'tcx>(cx: &LateContext<'tcx>, ex: &Expr<'tcx>, arms: &[Arm<'tcx>]) {
948 let ex_ty = cx.typeck_results().expr_ty(ex).peel_refs();
949 if is_type_diagnostic_item(cx, ex_ty, sym::Result) {
951 if let PatKind::TupleStruct(ref path, inner, _) = arm.pat.kind {
952 let path_str = rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false));
953 if path_str == "Err" {
954 let mut matching_wild = inner.iter().any(is_wild);
955 let mut ident_bind_name = String::from("_");
957 // Looking for unused bindings (i.e.: `_e`)
958 for pat in inner.iter() {
959 if let PatKind::Binding(_, id, ident, None) = pat.kind {
960 if ident.as_str().starts_with('_') && !is_local_used(cx, arm.body, id) {
961 ident_bind_name = (&ident.name.as_str()).to_string();
962 matching_wild = true;
969 if is_panic_call(arm.body);
971 // `Err(_)` or `Err(_e)` arm with `panic!` found
972 span_lint_and_note(cx,
975 &format!("`Err({})` matches all errors", &ident_bind_name),
977 "match each error separately or use the error output, or use `.except(msg)` if the error case is unreachable",
987 enum CommonPrefixSearcher<'a> {
989 Path(&'a [PathSegment<'a>]),
992 impl CommonPrefixSearcher<'a> {
993 fn with_path(&mut self, path: &'a [PathSegment<'a>]) {
995 [path @ .., _] => self.with_prefix(path),
1000 fn with_prefix(&mut self, path: &'a [PathSegment<'a>]) {
1002 Self::None => *self = Self::Path(path),
1003 Self::Path(self_path)
1006 .map(|p| p.ident.name)
1007 .eq(self_path.iter().map(|p| p.ident.name)) => {},
1008 Self::Path(_) => *self = Self::Mixed,
1014 fn is_hidden(cx: &LateContext<'_>, variant_def: &VariantDef) -> bool {
1015 let attrs = cx.tcx.get_attrs(variant_def.def_id);
1016 clippy_utils::attrs::is_doc_hidden(attrs) || clippy_utils::attrs::is_unstable(attrs)
1019 #[allow(clippy::too_many_lines)]
1020 fn check_wild_enum_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
1021 let ty = cx.typeck_results().expr_ty(ex).peel_refs();
1022 let adt_def = match ty.kind() {
1024 if adt_def.is_enum()
1025 && !(is_type_diagnostic_item(cx, ty, sym::Option) || is_type_diagnostic_item(cx, ty, sym::Result)) =>
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) {
1068 DefKind::Const | DefKind::ConstParam | DefKind::AnonConst | DefKind::InlineConst,
1071 Res::Def(_, id) => id,
1074 if arm.guard.is_none() {
1075 missing_variants.retain(|e| e.ctor_def_id != Some(id));
1079 PatKind::TupleStruct(path, patterns, ..) => {
1080 if let Some(id) = cx.qpath_res(path, pat.hir_id).opt_def_id() {
1081 if arm.guard.is_none() && patterns.iter().all(|p| !is_refutable(cx, p)) {
1082 missing_variants.retain(|e| e.ctor_def_id != Some(id));
1087 PatKind::Struct(path, patterns, ..) => {
1088 if let Some(id) = cx.qpath_res(path, pat.hir_id).opt_def_id() {
1089 if arm.guard.is_none() && patterns.iter().all(|p| !is_refutable(cx, p.pat)) {
1090 missing_variants.retain(|e| e.def_id != id);
1098 QPath::Resolved(_, path) => path_prefix.with_path(path.segments),
1099 QPath::TypeRelative(
1101 kind: TyKind::Path(QPath::Resolved(_, path)),
1105 ) => path_prefix.with_prefix(path.segments),
1111 let format_suggestion = |variant: &VariantDef| {
1114 if let Some(ident) = wildcard_ident {
1115 format!("{} @ ", ident.name)
1119 if let CommonPrefixSearcher::Path(path_prefix) = path_prefix {
1120 let mut s = String::new();
1121 for seg in path_prefix {
1122 s.push_str(&seg.ident.as_str());
1127 let mut s = cx.tcx.def_path_str(adt_def.did);
1132 match variant.ctor_kind {
1133 CtorKind::Fn if variant.fields.len() == 1 => "(_)",
1134 CtorKind::Fn => "(..)",
1135 CtorKind::Const => "",
1136 CtorKind::Fictive => "{ .. }",
1141 match missing_variants.as_slice() {
1143 [x] if !adt_def.is_variant_list_non_exhaustive() && !has_hidden => span_lint_and_sugg(
1145 MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
1147 "wildcard matches only a single variant and will also match any future added variants",
1149 format_suggestion(x),
1150 Applicability::MaybeIncorrect,
1153 let mut suggestions: Vec<_> = variants.iter().copied().map(format_suggestion).collect();
1154 let message = if adt_def.is_variant_list_non_exhaustive() || has_hidden {
1155 suggestions.push("_".into());
1156 "wildcard matches known variants and will also match future added variants"
1158 "wildcard match will also match any future added variants"
1163 WILDCARD_ENUM_MATCH_ARM,
1167 suggestions.join(" | "),
1168 Applicability::MaybeIncorrect,
1174 // If the block contains only a `panic!` macro (as expression or statement)
1175 fn is_panic_call(expr: &Expr<'_>) -> bool {
1176 // Unwrap any wrapping blocks
1177 let span = if let ExprKind::Block(block, _) = expr.kind {
1178 match (&block.expr, block.stmts.len(), block.stmts.first()) {
1179 (&Some(exp), 0, _) => exp.span,
1180 (&None, 1, Some(stmt)) => stmt.span,
1187 is_expn_of(span, "panic").is_some() && is_expn_of(span, "unreachable").is_none()
1190 fn check_match_ref_pats<'a, 'b, I>(cx: &LateContext<'_>, ex: &Expr<'_>, pats: I, expr: &Expr<'_>)
1193 I: Clone + Iterator<Item = &'a Pat<'b>>,
1195 if !has_multiple_ref_pats(pats.clone()) {
1199 let (first_sugg, msg, title);
1200 let span = ex.span.source_callsite();
1201 if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner) = ex.kind {
1202 first_sugg = once((span, Sugg::hir_with_macro_callsite(cx, inner, "..").to_string()));
1204 title = "you don't need to add `&` to both the expression and the patterns";
1206 first_sugg = once((span, Sugg::hir_with_macro_callsite(cx, ex, "..").deref().to_string()));
1207 msg = "instead of prefixing all patterns with `&`, you can dereference the expression";
1208 title = "you don't need to add `&` to all patterns";
1211 let remaining_suggs = pats.filter_map(|pat| {
1212 if let PatKind::Ref(refp, _) = pat.kind {
1213 Some((pat.span, snippet(cx, refp.span, "..").to_string()))
1219 span_lint_and_then(cx, MATCH_REF_PATS, expr.span, title, |diag| {
1220 if !expr.span.from_expansion() {
1221 multispan_sugg(diag, msg, first_sugg.chain(remaining_suggs));
1226 fn check_match_as_ref(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>) {
1227 if arms.len() == 2 && arms[0].guard.is_none() && arms[1].guard.is_none() {
1228 let arm_ref: Option<BindingAnnotation> = if is_none_arm(cx, &arms[0]) {
1229 is_ref_some_arm(cx, &arms[1])
1230 } else if is_none_arm(cx, &arms[1]) {
1231 is_ref_some_arm(cx, &arms[0])
1235 if let Some(rb) = arm_ref {
1236 let suggestion = if rb == BindingAnnotation::Ref {
1242 let output_ty = cx.typeck_results().expr_ty(expr);
1243 let input_ty = cx.typeck_results().expr_ty(ex);
1245 let cast = if_chain! {
1246 if let ty::Adt(_, substs) = input_ty.kind();
1247 let input_ty = substs.type_at(0);
1248 if let ty::Adt(_, substs) = output_ty.kind();
1249 let output_ty = substs.type_at(0);
1250 if let ty::Ref(_, output_ty, _) = *output_ty.kind();
1251 if input_ty != output_ty;
1259 let mut applicability = Applicability::MachineApplicable;
1264 &format!("use `{}()` instead", suggestion),
1268 snippet_with_applicability(cx, ex.span, "_", &mut applicability),
1278 fn check_wild_in_or_pats(cx: &LateContext<'_>, arms: &[Arm<'_>]) {
1280 if let PatKind::Or(fields) = arm.pat.kind {
1281 // look for multiple fields in this arm that contains at least one Wild pattern
1282 if fields.len() > 1 && fields.iter().any(is_wild) {
1285 WILDCARD_IN_OR_PATTERNS,
1287 "wildcard pattern covers any other pattern as it will match anyway",
1289 "consider handling `_` separately",
1296 /// Lint a `match` or `if let .. { .. } else { .. }` expr that could be replaced by `matches!`
1297 fn check_match_like_matches<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> bool {
1298 if let Some(higher::IfLet {
1302 if_else: Some(if_else),
1303 }) = higher::IfLet::hir(cx, expr)
1305 return find_matches_sugg(
1308 IntoIterator::into_iter([(&[][..], Some(let_pat), if_then, None), (&[][..], None, if_else, None)]),
1314 if let ExprKind::Match(scrut, arms, MatchSource::Normal) = expr.kind {
1315 return find_matches_sugg(
1318 arms.iter().map(|arm| {
1320 cx.tcx.hir().attrs(arm.hir_id),
1334 /// Lint a `match` or `if let` for replacement by `matches!`
1335 fn find_matches_sugg<'a, 'b, I>(
1336 cx: &LateContext<'_>,
1345 + DoubleEndedIterator
1350 Option<&'a Pat<'b>>,
1352 Option<&'a Guard<'b>>,
1358 if cx.typeck_results().expr_ty(expr).is_bool();
1359 if let Some((_, last_pat_opt, last_expr, _)) = iter.next_back();
1360 let iter_without_last = iter.clone();
1361 if let Some((first_attrs, _, first_expr, first_guard)) = iter.next();
1362 if let Some(b0) = find_bool_lit(&first_expr.kind, is_if_let);
1363 if let Some(b1) = find_bool_lit(&last_expr.kind, is_if_let);
1365 if first_guard.is_none() || iter.len() == 0;
1366 if first_attrs.is_empty();
1369 find_bool_lit(&arm.2.kind, is_if_let).map_or(false, |b| b == b0) && arm.3.is_none() && arm.0.is_empty()
1372 if let Some(last_pat) = last_pat_opt {
1373 if !is_wild(last_pat) {
1378 // The suggestion may be incorrect, because some arms can have `cfg` attributes
1379 // evaluated into `false` and so such arms will be stripped before.
1380 let mut applicability = Applicability::MaybeIncorrect;
1382 use itertools::Itertools as _;
1385 let pat_span = arm.1?.span;
1386 Some(snippet_with_applicability(cx, pat_span, "..", &mut applicability))
1390 let pat_and_guard = if let Some(Guard::If(g)) = first_guard {
1391 format!("{} if {}", pat, snippet_with_applicability(cx, g.span, "..", &mut applicability))
1396 // strip potential borrows (#6503), but only if the type is a reference
1397 let mut ex_new = ex;
1398 if let ExprKind::AddrOf(BorrowKind::Ref, .., ex_inner) = ex.kind {
1399 if let ty::Ref(..) = cx.typeck_results().expr_ty(ex_inner).kind() {
1405 MATCH_LIKE_MATCHES_MACRO,
1407 &format!("{} expression looks like `matches!` macro", if is_if_let { "if let .. else" } else { "match" }),
1410 "{}matches!({}, {})",
1411 if b0 { "" } else { "!" },
1412 snippet_with_applicability(cx, ex_new.span, "..", &mut applicability),
1424 /// Extract a `bool` or `{ bool }`
1425 fn find_bool_lit(ex: &ExprKind<'_>, is_if_let: bool) -> Option<bool> {
1427 ExprKind::Lit(Spanned {
1428 node: LitKind::Bool(b), ..
1438 if let ExprKind::Lit(Spanned {
1439 node: LitKind::Bool(b), ..
1451 #[allow(clippy::too_many_lines)]
1452 fn check_match_single_binding<'a>(cx: &LateContext<'a>, ex: &Expr<'a>, arms: &[Arm<'_>], expr: &Expr<'_>) {
1453 if in_macro(expr.span) || arms.len() != 1 || is_refutable(cx, arms[0].pat) {
1458 // This is a hack to deal with arms that are excluded by macros like `#[cfg]`. It is only used here
1459 // to prevent false positives as there is currently no better way to detect if code was excluded by
1460 // a macro. See PR #6435
1462 if let Some(match_snippet) = snippet_opt(cx, expr.span);
1463 if let Some(arm_snippet) = snippet_opt(cx, arms[0].span);
1464 if let Some(ex_snippet) = snippet_opt(cx, ex.span);
1465 let rest_snippet = match_snippet.replace(&arm_snippet, "").replace(&ex_snippet, "");
1466 if rest_snippet.contains("=>");
1468 // The code it self contains another thick arrow "=>"
1469 // -> Either another arm or a comment
1474 let matched_vars = ex.span;
1475 let bind_names = arms[0].pat.span;
1476 let match_body = remove_blocks(arms[0].body);
1477 let mut snippet_body = if match_body.span.from_expansion() {
1478 Sugg::hir_with_macro_callsite(cx, match_body, "..").to_string()
1480 snippet_block(cx, match_body.span, "..", Some(expr.span)).to_string()
1483 // Do we need to add ';' to suggestion ?
1484 match match_body.kind {
1485 ExprKind::Block(block, _) => {
1486 // macro + expr_ty(body) == ()
1487 if block.span.from_expansion() && cx.typeck_results().expr_ty(match_body).is_unit() {
1488 snippet_body.push(';');
1492 // expr_ty(body) == ()
1493 if cx.typeck_results().expr_ty(match_body).is_unit() {
1494 snippet_body.push(';');
1499 let mut applicability = Applicability::MaybeIncorrect;
1500 match arms[0].pat.kind {
1501 PatKind::Binding(..) | PatKind::Tuple(_, _) | PatKind::Struct(..) => {
1502 // If this match is in a local (`let`) stmt
1503 let (target_span, sugg) = if let Some(parent_let_node) = opt_parent_let(cx, ex) {
1505 parent_let_node.span,
1507 "let {} = {};\n{}let {} = {};",
1508 snippet_with_applicability(cx, bind_names, "..", &mut applicability),
1509 snippet_with_applicability(cx, matched_vars, "..", &mut applicability),
1510 " ".repeat(indent_of(cx, expr.span).unwrap_or(0)),
1511 snippet_with_applicability(cx, parent_let_node.pat.span, "..", &mut applicability),
1516 // If we are in closure, we need curly braces around suggestion
1517 let mut indent = " ".repeat(indent_of(cx, ex.span).unwrap_or(0));
1518 let (mut cbrace_start, mut cbrace_end) = ("".to_string(), "".to_string());
1519 if let Some(parent_expr) = get_parent_expr(cx, expr) {
1520 if let ExprKind::Closure(..) = parent_expr.kind {
1521 cbrace_end = format!("\n{}}}", indent);
1522 // Fix body indent due to the closure
1523 indent = " ".repeat(indent_of(cx, bind_names).unwrap_or(0));
1524 cbrace_start = format!("{{\n{}", indent);
1527 // If the parent is already an arm, and the body is another match statement,
1528 // we need curly braces around suggestion
1529 let parent_node_id = cx.tcx.hir().get_parent_node(expr.hir_id);
1530 if let Node::Arm(arm) = &cx.tcx.hir().get(parent_node_id) {
1531 if let ExprKind::Match(..) = arm.body.kind {
1532 cbrace_end = format!("\n{}}}", indent);
1533 // Fix body indent due to the match
1534 indent = " ".repeat(indent_of(cx, bind_names).unwrap_or(0));
1535 cbrace_start = format!("{{\n{}", indent);
1541 "{}let {} = {};\n{}{}{}",
1543 snippet_with_applicability(cx, bind_names, "..", &mut applicability),
1544 snippet_with_applicability(cx, matched_vars, "..", &mut applicability),
1553 MATCH_SINGLE_BINDING,
1555 "this match could be written as a `let` statement",
1556 "consider using `let` statement",
1562 if ex.can_have_side_effects() {
1563 let indent = " ".repeat(indent_of(cx, expr.span).unwrap_or(0));
1566 snippet_with_applicability(cx, ex.span, "..", &mut applicability),
1572 MATCH_SINGLE_BINDING,
1574 "this match could be replaced by its scrutinee and body",
1575 "consider using the scrutinee and body instead",
1582 MATCH_SINGLE_BINDING,
1584 "this match could be replaced by its body itself",
1585 "consider using the match body instead",
1587 Applicability::MachineApplicable,
1595 /// Returns true if the `ex` match expression is in a local (`let`) statement
1596 fn opt_parent_let<'a>(cx: &LateContext<'a>, ex: &Expr<'a>) -> Option<&'a Local<'a>> {
1597 let map = &cx.tcx.hir();
1599 if let Some(Node::Expr(parent_arm_expr)) = map.find(map.get_parent_node(ex.hir_id));
1600 if let Some(Node::Local(parent_let_expr)) = map.find(map.get_parent_node(parent_arm_expr.hir_id));
1602 return Some(parent_let_expr);
1608 /// Gets all arms that are unbounded `PatRange`s.
1609 fn all_ranges<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>], ty: Ty<'tcx>) -> Vec<SpannedRange<FullInt>> {
1612 if let Arm { pat, guard: None, .. } = *arm {
1613 if let PatKind::Range(ref lhs, ref rhs, range_end) = pat.kind {
1614 let lhs = match lhs {
1615 Some(lhs) => constant(cx, cx.typeck_results(), lhs)?.0,
1616 None => miri_to_const(ty.numeric_min_val(cx.tcx)?)?,
1618 let rhs = match rhs {
1619 Some(rhs) => constant(cx, cx.typeck_results(), rhs)?.0,
1620 None => miri_to_const(ty.numeric_max_val(cx.tcx)?)?,
1623 let lhs_val = lhs.int_value(cx, ty)?;
1624 let rhs_val = rhs.int_value(cx, ty)?;
1626 let rhs_bound = match range_end {
1627 RangeEnd::Included => Bound::Included(rhs_val),
1628 RangeEnd::Excluded => Bound::Excluded(rhs_val),
1630 return Some(SpannedRange {
1632 node: (lhs_val, rhs_bound),
1636 if let PatKind::Lit(value) = pat.kind {
1637 let value = constant_full_int(cx, cx.typeck_results(), value)?;
1638 return Some(SpannedRange {
1640 node: (value, Bound::Included(value)),
1649 #[derive(Debug, Eq, PartialEq)]
1650 pub struct SpannedRange<T> {
1652 pub node: (T, Bound<T>),
1655 // Checks if arm has the form `None => None`
1656 fn is_none_arm(cx: &LateContext<'_>, arm: &Arm<'_>) -> bool {
1657 matches!(arm.pat.kind, PatKind::Path(ref qpath) if is_lang_ctor(cx, qpath, OptionNone))
1660 // Checks if arm has the form `Some(ref v) => Some(v)` (checks for `ref` and `ref mut`)
1661 fn is_ref_some_arm(cx: &LateContext<'_>, arm: &Arm<'_>) -> Option<BindingAnnotation> {
1663 if let PatKind::TupleStruct(ref qpath, [first_pat, ..], _) = arm.pat.kind;
1664 if is_lang_ctor(cx, qpath, OptionSome);
1665 if let PatKind::Binding(rb, .., ident, _) = first_pat.kind;
1666 if rb == BindingAnnotation::Ref || rb == BindingAnnotation::RefMut;
1667 if let ExprKind::Call(e, args) = remove_blocks(arm.body).kind;
1668 if let ExprKind::Path(ref some_path) = e.kind;
1669 if is_lang_ctor(cx, some_path, OptionSome) && args.len() == 1;
1670 if let ExprKind::Path(QPath::Resolved(_, path2)) = args[0].kind;
1671 if path2.segments.len() == 1 && ident.name == path2.segments[0].ident.name;
1679 fn has_multiple_ref_pats<'a, 'b, I>(pats: I) -> bool
1682 I: Iterator<Item = &'a Pat<'b>>,
1684 let mut ref_count = 0;
1685 for opt in pats.map(|pat| match pat.kind {
1686 PatKind::Ref(..) => Some(true), // &-patterns
1687 PatKind::Wild => Some(false), // an "anything" wildcard is also fine
1688 _ => None, // any other pattern is not fine
1690 if let Some(inner) = opt {
1701 pub fn overlapping<T>(ranges: &[SpannedRange<T>]) -> Option<(&SpannedRange<T>, &SpannedRange<T>)>
1705 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
1707 Start(T, &'a SpannedRange<T>),
1708 End(Bound<T>, &'a SpannedRange<T>),
1711 impl<'a, T: Copy> Kind<'a, T> {
1712 fn range(&self) -> &'a SpannedRange<T> {
1714 Kind::Start(_, r) | Kind::End(_, r) => r,
1718 fn value(self) -> Bound<T> {
1720 Kind::Start(t, _) => Bound::Included(t),
1721 Kind::End(t, _) => t,
1726 impl<'a, T: Copy + Ord> PartialOrd for Kind<'a, T> {
1727 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1728 Some(self.cmp(other))
1732 impl<'a, T: Copy + Ord> Ord for Kind<'a, T> {
1733 fn cmp(&self, other: &Self) -> Ordering {
1734 match (self.value(), other.value()) {
1735 (Bound::Included(a), Bound::Included(b)) | (Bound::Excluded(a), Bound::Excluded(b)) => a.cmp(&b),
1736 // Range patterns cannot be unbounded (yet)
1737 (Bound::Unbounded, _) | (_, Bound::Unbounded) => unimplemented!(),
1738 (Bound::Included(a), Bound::Excluded(b)) => match a.cmp(&b) {
1739 Ordering::Equal => Ordering::Greater,
1742 (Bound::Excluded(a), Bound::Included(b)) => match a.cmp(&b) {
1743 Ordering::Equal => Ordering::Less,
1750 let mut values = Vec::with_capacity(2 * ranges.len());
1753 values.push(Kind::Start(r.node.0, r));
1754 values.push(Kind::End(r.node.1, r));
1759 for (a, b) in iter::zip(&values, values.iter().skip(1)) {
1761 (&Kind::Start(_, ra), &Kind::End(_, rb)) => {
1762 if ra.node != rb.node {
1763 return Some((ra, rb));
1766 (&Kind::End(a, _), &Kind::Start(b, _)) if a != Bound::Included(b) => (),
1768 // skip if the range `a` is completely included into the range `b`
1769 if let Ordering::Equal | Ordering::Less = a.cmp(b) {
1770 let kind_a = Kind::End(a.range().node.1, a.range());
1771 let kind_b = Kind::End(b.range().node.1, b.range());
1772 if let Ordering::Equal | Ordering::Greater = kind_a.cmp(&kind_b) {
1776 return Some((a.range(), b.range()));
1784 mod redundant_pattern_match {
1785 use super::REDUNDANT_PATTERN_MATCHING;
1786 use clippy_utils::diagnostics::span_lint_and_then;
1787 use clippy_utils::higher;
1788 use clippy_utils::source::{snippet, snippet_with_applicability};
1789 use clippy_utils::ty::{implements_trait, is_type_diagnostic_item, is_type_lang_item, match_type};
1790 use clippy_utils::{is_lang_ctor, is_qpath_def_path, is_trait_method, paths};
1791 use if_chain::if_chain;
1792 use rustc_ast::ast::LitKind;
1793 use rustc_data_structures::fx::FxHashSet;
1794 use rustc_errors::Applicability;
1795 use rustc_hir::LangItem::{OptionNone, OptionSome, PollPending, PollReady, ResultErr, ResultOk};
1797 intravisit::{walk_expr, ErasedMap, NestedVisitorMap, Visitor},
1798 Arm, Block, Expr, ExprKind, LangItem, MatchSource, Node, Pat, PatKind, QPath,
1800 use rustc_lint::LateContext;
1801 use rustc_middle::ty::{self, subst::GenericArgKind, Ty};
1802 use rustc_span::sym;
1804 pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
1805 if let Some(higher::IfLet {
1810 }) = higher::IfLet::hir(cx, expr)
1812 find_sugg_for_if_let(cx, expr, let_pat, let_expr, "if", if_else.is_some());
1814 if let ExprKind::Match(op, arms, MatchSource::Normal) = &expr.kind {
1815 find_sugg_for_match(cx, expr, op, arms);
1817 if let Some(higher::WhileLet { let_pat, let_expr, .. }) = higher::WhileLet::hir(expr) {
1818 find_sugg_for_if_let(cx, expr, let_pat, let_expr, "while", false);
1822 /// Checks if the drop order for a type matters. Some std types implement drop solely to
1823 /// deallocate memory. For these types, and composites containing them, changing the drop order
1824 /// won't result in any observable side effects.
1825 fn type_needs_ordered_drop(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
1826 type_needs_ordered_drop_inner(cx, ty, &mut FxHashSet::default())
1829 fn type_needs_ordered_drop_inner(cx: &LateContext<'tcx>, ty: Ty<'tcx>, seen: &mut FxHashSet<Ty<'tcx>>) -> bool {
1830 if !seen.insert(ty) {
1833 if !ty.needs_drop(cx.tcx, cx.param_env) {
1839 .map_or(false, |id| implements_trait(cx, ty, id, &[]))
1841 // This type doesn't implement drop, so no side effects here.
1842 // Check if any component type has any.
1844 ty::Tuple(_) => ty.tuple_fields().any(|ty| type_needs_ordered_drop_inner(cx, ty, seen)),
1845 ty::Array(ty, _) => type_needs_ordered_drop_inner(cx, ty, seen),
1846 ty::Adt(adt, subs) => adt
1848 .map(|f| f.ty(cx.tcx, subs))
1849 .any(|ty| type_needs_ordered_drop_inner(cx, ty, seen)),
1853 // Check for std types which implement drop, but only for memory allocation.
1854 else if is_type_diagnostic_item(cx, ty, sym::Vec)
1855 || is_type_lang_item(cx, ty, LangItem::OwnedBox)
1856 || is_type_diagnostic_item(cx, ty, sym::Rc)
1857 || is_type_diagnostic_item(cx, ty, sym::Arc)
1858 || is_type_diagnostic_item(cx, ty, sym::cstring_type)
1859 || is_type_diagnostic_item(cx, ty, sym::BTreeMap)
1860 || is_type_diagnostic_item(cx, ty, sym::LinkedList)
1861 || match_type(cx, ty, &paths::WEAK_RC)
1862 || match_type(cx, ty, &paths::WEAK_ARC)
1864 // Check all of the generic arguments.
1865 if let ty::Adt(_, subs) = ty.kind() {
1866 subs.types().any(|ty| type_needs_ordered_drop_inner(cx, ty, seen))
1875 // Extract the generic arguments out of a type
1876 fn try_get_generic_ty(ty: Ty<'_>, index: usize) -> Option<Ty<'_>> {
1878 if let ty::Adt(_, subs) = ty.kind();
1879 if let Some(sub) = subs.get(index);
1880 if let GenericArgKind::Type(sub_ty) = sub.unpack();
1889 // Checks if there are any temporaries created in the given expression for which drop order
1891 fn temporaries_need_ordered_drop(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
1892 struct V<'a, 'tcx> {
1893 cx: &'a LateContext<'tcx>,
1896 impl<'a, 'tcx> Visitor<'tcx> for V<'a, 'tcx> {
1897 type Map = ErasedMap<'tcx>;
1898 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
1899 NestedVisitorMap::None
1902 fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
1904 // Taking the reference of a value leaves a temporary
1905 // e.g. In `&String::new()` the string is a temporary value.
1906 // Remaining fields are temporary values
1907 // e.g. In `(String::new(), 0).1` the string is a temporary value.
1908 ExprKind::AddrOf(_, _, expr) | ExprKind::Field(expr, _) => {
1909 if !matches!(expr.kind, ExprKind::Path(_)) {
1910 if type_needs_ordered_drop(self.cx, self.cx.typeck_results().expr_ty(expr)) {
1913 self.visit_expr(expr);
1917 // the base type is alway taken by reference.
1918 // e.g. In `(vec![0])[0]` the vector is a temporary value.
1919 ExprKind::Index(base, index) => {
1920 if !matches!(base.kind, ExprKind::Path(_)) {
1921 if type_needs_ordered_drop(self.cx, self.cx.typeck_results().expr_ty(base)) {
1924 self.visit_expr(base);
1927 self.visit_expr(index);
1929 // Method calls can take self by reference.
1930 // e.g. In `String::new().len()` the string is a temporary value.
1931 ExprKind::MethodCall(_, _, [self_arg, args @ ..], _) => {
1932 if !matches!(self_arg.kind, ExprKind::Path(_)) {
1933 let self_by_ref = self
1936 .type_dependent_def_id(expr.hir_id)
1937 .map_or(false, |id| self.cx.tcx.fn_sig(id).skip_binder().inputs()[0].is_ref());
1939 && type_needs_ordered_drop(self.cx, self.cx.typeck_results().expr_ty(self_arg))
1943 self.visit_expr(self_arg);
1946 args.iter().for_each(|arg| self.visit_expr(arg));
1948 // Either explicitly drops values, or changes control flow.
1949 ExprKind::DropTemps(_)
1951 | ExprKind::Break(..)
1952 | ExprKind::Yield(..)
1953 | ExprKind::Block(Block { expr: None, .. }, _)
1954 | ExprKind::Loop(..) => (),
1956 // Only consider the final expression.
1957 ExprKind::Block(Block { expr: Some(expr), .. }, _) => self.visit_expr(expr),
1959 _ => walk_expr(self, expr),
1964 let mut v = V { cx, res: false };
1969 fn find_sugg_for_if_let<'tcx>(
1970 cx: &LateContext<'tcx>,
1971 expr: &'tcx Expr<'_>,
1973 let_expr: &'tcx Expr<'_>,
1974 keyword: &'static str,
1977 // also look inside refs
1978 let mut kind = &let_pat.kind;
1979 // if we have &None for example, peel it so we can detect "if let None = x"
1980 if let PatKind::Ref(inner, _mutability) = kind {
1983 let op_ty = cx.typeck_results().expr_ty(let_expr);
1984 // Determine which function should be used, and the type contained by the corresponding
1986 let (good_method, inner_ty) = match kind {
1987 PatKind::TupleStruct(ref path, [sub_pat], _) => {
1988 if let PatKind::Wild = sub_pat.kind {
1989 if is_lang_ctor(cx, path, ResultOk) {
1990 ("is_ok()", try_get_generic_ty(op_ty, 0).unwrap_or(op_ty))
1991 } else if is_lang_ctor(cx, path, ResultErr) {
1992 ("is_err()", try_get_generic_ty(op_ty, 1).unwrap_or(op_ty))
1993 } else if is_lang_ctor(cx, path, OptionSome) {
1994 ("is_some()", op_ty)
1995 } else if is_lang_ctor(cx, path, PollReady) {
1996 ("is_ready()", op_ty)
1997 } else if is_qpath_def_path(cx, path, sub_pat.hir_id, &paths::IPADDR_V4) {
1998 ("is_ipv4()", op_ty)
1999 } else if is_qpath_def_path(cx, path, sub_pat.hir_id, &paths::IPADDR_V6) {
2000 ("is_ipv6()", op_ty)
2008 PatKind::Path(ref path) => {
2009 let method = if is_lang_ctor(cx, path, OptionNone) {
2011 } else if is_lang_ctor(cx, path, PollPending) {
2016 // `None` and `Pending` don't have an inner type.
2017 (method, cx.tcx.types.unit)
2022 // If this is the last expression in a block or there is an else clause then the whole
2023 // type needs to be considered, not just the inner type of the branch being matched on.
2024 // Note the last expression in a block is dropped after all local bindings.
2025 let check_ty = if has_else
2026 || (keyword == "if" && matches!(cx.tcx.hir().parent_iter(expr.hir_id).next(), Some((_, Node::Block(..)))))
2033 // All temporaries created in the scrutinee expression are dropped at the same time as the
2034 // scrutinee would be, so they have to be considered as well.
2035 // e.g. in `if let Some(x) = foo.lock().unwrap().baz.as_ref() { .. }` the lock will be held
2036 // for the duration if body.
2037 let needs_drop = type_needs_ordered_drop(cx, check_ty) || temporaries_need_ordered_drop(cx, let_expr);
2039 // check that `while_let_on_iterator` lint does not trigger
2041 if keyword == "while";
2042 if let ExprKind::MethodCall(method_path, _, _, _) = let_expr.kind;
2043 if method_path.ident.name == sym::next;
2044 if is_trait_method(cx, let_expr, sym::Iterator);
2050 let result_expr = match &let_expr.kind {
2051 ExprKind::AddrOf(_, _, borrowed) => borrowed,
2056 REDUNDANT_PATTERN_MATCHING,
2058 &format!("redundant pattern matching, consider using `{}`", good_method),
2060 // if/while let ... = ... { ... }
2061 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2062 let expr_span = expr.span;
2064 // if/while let ... = ... { ... }
2066 let op_span = result_expr.span.source_callsite();
2068 // if/while let ... = ... { ... }
2069 // ^^^^^^^^^^^^^^^^^^^
2070 let span = expr_span.until(op_span.shrink_to_hi());
2072 let mut app = if needs_drop {
2073 Applicability::MaybeIncorrect
2075 Applicability::MachineApplicable
2077 let sugg = snippet_with_applicability(cx, op_span, "_", &mut app);
2079 diag.span_suggestion(span, "try this", format!("{} {}.{}", keyword, sugg, good_method), app);
2082 diag.note("this will change drop order of the result, as well as all temporaries");
2083 diag.note("add `#[allow(clippy::redundant_pattern_matching)]` if this is important");
2089 fn find_sugg_for_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op: &Expr<'_>, arms: &[Arm<'_>]) {
2090 if arms.len() == 2 {
2091 let node_pair = (&arms[0].pat.kind, &arms[1].pat.kind);
2093 let found_good_method = match node_pair {
2095 PatKind::TupleStruct(ref path_left, patterns_left, _),
2096 PatKind::TupleStruct(ref path_right, patterns_right, _),
2097 ) if patterns_left.len() == 1 && patterns_right.len() == 1 => {
2098 if let (PatKind::Wild, PatKind::Wild) = (&patterns_left[0].kind, &patterns_right[0].kind) {
2099 find_good_method_for_match(
2110 find_good_method_for_match(
2125 (PatKind::TupleStruct(ref path_left, patterns, _), PatKind::Path(ref path_right))
2126 | (PatKind::Path(ref path_left), PatKind::TupleStruct(ref path_right, patterns, _))
2127 if patterns.len() == 1 =>
2129 if let PatKind::Wild = patterns[0].kind {
2130 find_good_method_for_match(
2135 &paths::OPTION_SOME,
2136 &paths::OPTION_NONE,
2141 find_good_method_for_match(
2147 &paths::POLL_PENDING,
2159 if let Some(good_method) = found_good_method {
2160 let span = expr.span.to(op.span);
2161 let result_expr = match &op.kind {
2162 ExprKind::AddrOf(_, _, borrowed) => borrowed,
2167 REDUNDANT_PATTERN_MATCHING,
2169 &format!("redundant pattern matching, consider using `{}`", good_method),
2171 diag.span_suggestion(
2174 format!("{}.{}", snippet(cx, result_expr.span, "_"), good_method),
2175 Applicability::MaybeIncorrect, // snippet
2183 #[allow(clippy::too_many_arguments)]
2184 fn find_good_method_for_match<'a>(
2185 cx: &LateContext<'_>,
2187 path_left: &QPath<'_>,
2188 path_right: &QPath<'_>,
2189 expected_left: &[&str],
2190 expected_right: &[&str],
2191 should_be_left: &'a str,
2192 should_be_right: &'a str,
2193 ) -> Option<&'a str> {
2194 let body_node_pair = if is_qpath_def_path(cx, path_left, arms[0].pat.hir_id, expected_left)
2195 && is_qpath_def_path(cx, path_right, arms[1].pat.hir_id, expected_right)
2197 (&(*arms[0].body).kind, &(*arms[1].body).kind)
2198 } else if is_qpath_def_path(cx, path_right, arms[1].pat.hir_id, expected_left)
2199 && is_qpath_def_path(cx, path_left, arms[0].pat.hir_id, expected_right)
2201 (&(*arms[1].body).kind, &(*arms[0].body).kind)
2206 match body_node_pair {
2207 (ExprKind::Lit(ref lit_left), ExprKind::Lit(ref lit_right)) => match (&lit_left.node, &lit_right.node) {
2208 (LitKind::Bool(true), LitKind::Bool(false)) => Some(should_be_left),
2209 (LitKind::Bool(false), LitKind::Bool(true)) => Some(should_be_right),
2218 fn test_overlapping() {
2219 use rustc_span::source_map::DUMMY_SP;
2221 let sp = |s, e| SpannedRange {
2226 assert_eq!(None, overlapping::<u8>(&[]));
2227 assert_eq!(None, overlapping(&[sp(1, Bound::Included(4))]));
2230 overlapping(&[sp(1, Bound::Included(4)), sp(5, Bound::Included(6))])
2235 sp(1, Bound::Included(4)),
2236 sp(5, Bound::Included(6)),
2237 sp(10, Bound::Included(11))
2241 Some((&sp(1, Bound::Included(4)), &sp(3, Bound::Included(6)))),
2242 overlapping(&[sp(1, Bound::Included(4)), sp(3, Bound::Included(6))])
2245 Some((&sp(5, Bound::Included(6)), &sp(6, Bound::Included(11)))),
2247 sp(1, Bound::Included(4)),
2248 sp(5, Bound::Included(6)),
2249 sp(6, Bound::Included(11))
2254 /// Implementation of `MATCH_SAME_ARMS`.
2255 fn lint_match_arms<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>) {
2256 if let ExprKind::Match(_, arms, MatchSource::Normal) = expr.kind {
2257 let hash = |&(_, arm): &(usize, &Arm<'_>)| -> u64 {
2258 let mut h = SpanlessHash::new(cx);
2259 h.hash_expr(arm.body);
2263 let eq = |&(lindex, lhs): &(usize, &Arm<'_>), &(rindex, rhs): &(usize, &Arm<'_>)| -> bool {
2264 let min_index = usize::min(lindex, rindex);
2265 let max_index = usize::max(lindex, rindex);
2267 let mut local_map: HirIdMap<HirId> = HirIdMap::default();
2268 let eq_fallback = |a: &Expr<'_>, b: &Expr<'_>| {
2270 if let Some(a_id) = path_to_local(a);
2271 if let Some(b_id) = path_to_local(b);
2272 let entry = match local_map.entry(a_id) {
2273 Entry::Vacant(entry) => entry,
2274 // check if using the same bindings as before
2275 Entry::Occupied(entry) => return *entry.get() == b_id,
2277 // the names technically don't have to match; this makes the lint more conservative
2278 if cx.tcx.hir().name(a_id) == cx.tcx.hir().name(b_id);
2279 if TyS::same_type(cx.typeck_results().expr_ty(a), cx.typeck_results().expr_ty(b));
2280 if pat_contains_local(lhs.pat, a_id);
2281 if pat_contains_local(rhs.pat, b_id);
2290 // Arms with a guard are ignored, those can’t always be merged together
2291 // This is also the case for arms in-between each there is an arm with a guard
2292 (min_index..=max_index).all(|index| arms[index].guard.is_none())
2293 && SpanlessEq::new(cx)
2294 .expr_fallback(eq_fallback)
2295 .eq_expr(lhs.body, rhs.body)
2296 // these checks could be removed to allow unused bindings
2297 && bindings_eq(lhs.pat, local_map.keys().copied().collect())
2298 && bindings_eq(rhs.pat, local_map.values().copied().collect())
2301 let indexed_arms: Vec<(usize, &Arm<'_>)> = arms.iter().enumerate().collect();
2302 for (&(_, i), &(_, j)) in search_same(&indexed_arms, hash, eq) {
2307 "this `match` has identical arm bodies",
2309 diag.span_note(i.body.span, "same as this");
2311 // Note: this does not use `span_suggestion` on purpose:
2312 // there is no clean way
2313 // to remove the other arm. Building a span and suggest to replace it to ""
2314 // makes an even more confusing error message. Also in order not to make up a
2315 // span for the whole pattern, the suggestion is only shown when there is only
2316 // one pattern. The user should know about `|` if they are already using it…
2318 let lhs = snippet(cx, i.pat.span, "<pat1>");
2319 let rhs = snippet(cx, j.pat.span, "<pat2>");
2321 if let PatKind::Wild = j.pat.kind {
2322 // if the last arm is _, then i could be integrated into _
2323 // note that i.pat cannot be _, because that would mean that we're
2324 // hiding all the subsequent arms, and rust won't compile
2328 "`{}` has the same arm body as the `_` wildcard, consider removing it",
2333 diag.span_help(i.pat.span, &format!("consider refactoring into `{} | {}`", lhs, rhs,))
2334 .help("...or consider changing the match arm bodies");
2342 fn pat_contains_local(pat: &Pat<'_>, id: HirId) -> bool {
2343 let mut result = false;
2344 pat.walk_short(|p| {
2345 result |= matches!(p.kind, PatKind::Binding(_, binding_id, ..) if binding_id == id);
2351 /// Returns true if all the bindings in the `Pat` are in `ids` and vice versa
2352 fn bindings_eq(pat: &Pat<'_>, mut ids: HirIdSet) -> bool {
2353 let mut result = true;
2354 pat.each_binding_or_first(&mut |_, id, _, _| result &= ids.remove(&id));
2355 result && ids.is_empty()