1 use crate::consts::{constant, miri_to_const, Constant};
2 use crate::utils::paths;
3 use crate::utils::sugg::Sugg;
4 use crate::utils::usage::is_unused;
6 expr_block, get_arg_name, get_parent_expr, in_macro, indent_of, is_allowed, is_expn_of, is_refutable,
7 is_type_diagnostic_item, is_wild, match_qpath, match_type, match_var, multispan_sugg, remove_blocks, snippet,
8 snippet_block, snippet_with_applicability, span_lint_and_help, span_lint_and_note, span_lint_and_sugg,
9 span_lint_and_then, walk_ptrs_ty,
11 use if_chain::if_chain;
12 use rustc_ast::ast::LitKind;
13 use rustc_errors::Applicability;
14 use rustc_hir::def::CtorKind;
16 Arm, BindingAnnotation, Block, BorrowKind, Expr, ExprKind, Guard, Local, MatchSource, Mutability, Node, Pat,
17 PatKind, QPath, RangeEnd,
19 use rustc_lint::{LateContext, LateLintPass, LintContext};
20 use rustc_middle::lint::in_external_macro;
21 use rustc_middle::ty::{self, Ty};
22 use rustc_session::{declare_tool_lint, impl_lint_pass};
23 use rustc_span::source_map::{Span, Spanned};
24 use std::cmp::Ordering;
25 use std::collections::Bound;
27 declare_clippy_lint! {
28 /// **What it does:** Checks for matches with a single arm where an `if let`
29 /// will usually suffice.
31 /// **Why is this bad?** Just readability – `if let` nests less than a `match`.
33 /// **Known problems:** None.
37 /// # fn bar(stool: &str) {}
38 /// # let x = Some("abc");
42 /// Some(ref foo) => bar(foo),
47 /// if let Some(ref foo) = x {
53 "a `match` statement with a single nontrivial arm (i.e., where the other arm is `_ => {}`) instead of `if let`"
56 declare_clippy_lint! {
57 /// **What it does:** Checks for matches with two arms where an `if let else` will
60 /// **Why is this bad?** Just readability – `if let` nests less than a `match`.
62 /// **Known problems:** Personal style preferences may differ.
69 /// # fn bar(foo: &usize) {}
70 /// # let other_ref: usize = 1;
71 /// # let x: Option<&usize> = Some(&1);
73 /// Some(ref foo) => bar(foo),
74 /// _ => bar(&other_ref),
78 /// Using `if let` with `else`:
81 /// # fn bar(foo: &usize) {}
82 /// # let other_ref: usize = 1;
83 /// # let x: Option<&usize> = Some(&1);
84 /// if let Some(ref foo) = x {
90 pub SINGLE_MATCH_ELSE,
92 "a `match` statement with two arms where the second arm's pattern is a placeholder instead of a specific match pattern"
95 declare_clippy_lint! {
96 /// **What it does:** Checks for matches where all arms match a reference,
97 /// suggesting to remove the reference and deref the matched expression
98 /// instead. It also checks for `if let &foo = bar` blocks.
100 /// **Why is this bad?** It just makes the code less readable. That reference
101 /// destructuring adds nothing to the code.
103 /// **Known problems:** None.
109 /// &A(ref y) => foo(y),
116 /// A(ref y) => foo(y),
123 "a `match` or `if let` with all arms prefixed with `&` instead of deref-ing the match expression"
126 declare_clippy_lint! {
127 /// **What it does:** Checks for matches where match expression is a `bool`. It
128 /// suggests to replace the expression with an `if...else` block.
130 /// **Why is this bad?** It makes the code less readable.
132 /// **Known problems:** None.
138 /// let condition: bool = true;
139 /// match condition {
144 /// Use if/else instead:
148 /// let condition: bool = true;
157 "a `match` on a boolean expression instead of an `if..else` block"
160 declare_clippy_lint! {
161 /// **What it does:** Checks for overlapping match arms.
163 /// **Why is this bad?** It is likely to be an error and if not, makes the code
166 /// **Known problems:** None.
172 /// 1...10 => println!("1 ... 10"),
173 /// 5...15 => println!("5 ... 15"),
177 pub MATCH_OVERLAPPING_ARM,
179 "a `match` with overlapping arms"
182 declare_clippy_lint! {
183 /// **What it does:** Checks for arm which matches all errors with `Err(_)`
184 /// and take drastic actions like `panic!`.
186 /// **Why is this bad?** It is generally a bad practice, similar to
187 /// catching all exceptions in java with `catch(Exception)`
189 /// **Known problems:** None.
193 /// let x: Result<i32, &str> = Ok(3);
195 /// Ok(_) => println!("ok"),
196 /// Err(_) => panic!("err"),
199 pub MATCH_WILD_ERR_ARM,
201 "a `match` with `Err(_)` arm and take drastic actions"
204 declare_clippy_lint! {
205 /// **What it does:** Checks for match which is used to add a reference to an
208 /// **Why is this bad?** Using `as_ref()` or `as_mut()` instead is shorter.
210 /// **Known problems:** None.
214 /// let x: Option<()> = None;
217 /// let r: Option<&()> = match x {
219 /// Some(ref v) => Some(v),
223 /// let r: Option<&()> = x.as_ref();
227 "a `match` on an Option value instead of using `as_ref()` or `as_mut`"
230 declare_clippy_lint! {
231 /// **What it does:** Checks for wildcard enum matches using `_`.
233 /// **Why is this bad?** New enum variants added by library updates can be missed.
235 /// **Known problems:** Suggested replacements may be incorrect if guards exhaustively cover some
236 /// variants, and also may not use correct path to enum if it's not present in the current scope.
240 /// # enum Foo { A(usize), B(usize) }
241 /// # let x = Foo::B(1);
255 pub WILDCARD_ENUM_MATCH_ARM,
257 "a wildcard enum match arm using `_`"
260 declare_clippy_lint! {
261 /// **What it does:** Checks for wildcard enum matches for a single variant.
263 /// **Why is this bad?** New enum variants added by library updates can be missed.
265 /// **Known problems:** Suggested replacements may not use correct path to enum
266 /// if it's not present in the current scope.
271 /// # enum Foo { A, B, C }
272 /// # let x = Foo::B;
287 pub MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
289 "a wildcard enum match for a single variant"
292 declare_clippy_lint! {
293 /// **What it does:** Checks for wildcard pattern used with others patterns in same match arm.
295 /// **Why is this bad?** Wildcard pattern already covers any other pattern as it will match anyway.
296 /// It makes the code less readable, especially to spot wildcard pattern use in match arm.
298 /// **Known problems:** None.
314 pub WILDCARD_IN_OR_PATTERNS,
316 "a wildcard pattern used with others patterns in same match arm"
319 declare_clippy_lint! {
320 /// **What it does:** Checks for matches being used to destructure a single-variant enum
321 /// or tuple struct where a `let` will suffice.
323 /// **Why is this bad?** Just readability – `let` doesn't nest, whereas a `match` does.
325 /// **Known problems:** None.
333 /// let wrapper = Wrapper::Data(42);
335 /// let data = match wrapper {
336 /// Wrapper::Data(i) => i,
340 /// The correct use would be:
346 /// let wrapper = Wrapper::Data(42);
347 /// let Wrapper::Data(data) = wrapper;
349 pub INFALLIBLE_DESTRUCTURING_MATCH,
351 "a `match` statement with a single infallible arm instead of a `let`"
354 declare_clippy_lint! {
355 /// **What it does:** Checks for useless match that binds to only one value.
357 /// **Why is this bad?** Readability and needless complexity.
359 /// **Known problems:** Suggested replacements may be incorrect when `match`
360 /// is actually binding temporary value, bringing a 'dropped while borrowed' error.
375 /// let (c, d) = (a, b);
377 pub MATCH_SINGLE_BINDING,
379 "a match with a single binding instead of using `let` statement"
382 declare_clippy_lint! {
383 /// **What it does:** Checks for unnecessary '..' pattern binding on struct when all fields are explicitly matched.
385 /// **Why is this bad?** Correctness and readability. It's like having a wildcard pattern after
386 /// matching all enum variants explicitly.
388 /// **Known problems:** None.
392 /// # struct A { a: i32 }
393 /// let a = A { a: 5 };
397 /// A { a: 5, .. } => {},
403 /// A { a: 5 } => {},
407 pub REST_PAT_IN_FULLY_BOUND_STRUCTS,
409 "a match on a struct that binds all fields but still uses the wildcard pattern"
412 declare_clippy_lint! {
413 /// **What it does:** Lint for redundant pattern matching over `Result` or
416 /// **Why is this bad?** It's more concise and clear to just use the proper
419 /// **Known problems:** None.
424 /// if let Ok(_) = Ok::<i32, i32>(42) {}
425 /// if let Err(_) = Err::<i32, i32>(42) {}
426 /// if let None = None::<()> {}
427 /// if let Some(_) = Some(42) {}
428 /// match Ok::<i32, i32>(42) {
434 /// The more idiomatic use would be:
437 /// if Ok::<i32, i32>(42).is_ok() {}
438 /// if Err::<i32, i32>(42).is_err() {}
439 /// if None::<()>.is_none() {}
440 /// if Some(42).is_some() {}
441 /// Ok::<i32, i32>(42).is_ok();
443 pub REDUNDANT_PATTERN_MATCHING,
445 "use the proper utility function avoiding an `if let`"
448 declare_clippy_lint! {
449 /// **What it does:** Checks for `match` or `if let` expressions producing a
450 /// `bool` that could be written using `matches!`
452 /// **Why is this bad?** Readability and needless complexity.
454 /// **Known problems:** None
461 /// let a = match x {
466 /// let a = if let Some(0) = x {
473 /// let a = matches!(x, Some(0));
475 pub MATCH_LIKE_MATCHES_MACRO,
477 "a match that could be written with the matches! macro"
482 infallible_destructuring_match_linted: bool,
485 impl_lint_pass!(Matches => [
490 MATCH_OVERLAPPING_ARM,
493 WILDCARD_ENUM_MATCH_ARM,
494 MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
495 WILDCARD_IN_OR_PATTERNS,
496 MATCH_SINGLE_BINDING,
497 INFALLIBLE_DESTRUCTURING_MATCH,
498 REST_PAT_IN_FULLY_BOUND_STRUCTS,
499 REDUNDANT_PATTERN_MATCHING,
500 MATCH_LIKE_MATCHES_MACRO
503 impl<'tcx> LateLintPass<'tcx> for Matches {
504 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
505 if in_external_macro(cx.sess(), expr.span) {
509 redundant_pattern_match::check(cx, expr);
510 check_match_like_matches(cx, expr);
512 if let ExprKind::Match(ref ex, ref arms, MatchSource::Normal) = expr.kind {
513 check_single_match(cx, ex, arms, expr);
514 check_match_bool(cx, ex, arms, expr);
515 check_overlapping_arms(cx, ex, arms);
516 check_wild_err_arm(cx, ex, arms);
517 check_wild_enum_match(cx, ex, arms);
518 check_match_as_ref(cx, ex, arms, expr);
519 check_wild_in_or_pats(cx, arms);
521 if self.infallible_destructuring_match_linted {
522 self.infallible_destructuring_match_linted = false;
524 check_match_single_binding(cx, ex, arms, expr);
527 if let ExprKind::Match(ref ex, ref arms, _) = expr.kind {
528 check_match_ref_pats(cx, ex, arms, expr);
532 fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'_>) {
534 if !in_external_macro(cx.sess(), local.span);
535 if !in_macro(local.span);
536 if let Some(ref expr) = local.init;
537 if let ExprKind::Match(ref target, ref arms, MatchSource::Normal) = expr.kind;
538 if arms.len() == 1 && arms[0].guard.is_none();
539 if let PatKind::TupleStruct(
540 QPath::Resolved(None, ref variant_name), ref args, _) = arms[0].pat.kind;
542 if let Some(arg) = get_arg_name(&args[0]);
543 let body = remove_blocks(&arms[0].body);
544 if match_var(body, arg);
547 let mut applicability = Applicability::MachineApplicable;
548 self.infallible_destructuring_match_linted = true;
551 INFALLIBLE_DESTRUCTURING_MATCH,
553 "you seem to be trying to use `match` to destructure a single infallible pattern. \
554 Consider using `let`",
558 snippet_with_applicability(cx, variant_name.span, "..", &mut applicability),
559 snippet_with_applicability(cx, local.pat.span, "..", &mut applicability),
560 snippet_with_applicability(cx, target.span, "..", &mut applicability),
568 fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
570 if !in_external_macro(cx.sess(), pat.span);
571 if !in_macro(pat.span);
572 if let PatKind::Struct(ref qpath, fields, true) = pat.kind;
573 if let QPath::Resolved(_, ref path) = qpath;
574 if let Some(def_id) = path.res.opt_def_id();
575 let ty = cx.tcx.type_of(def_id);
576 if let ty::Adt(def, _) = ty.kind;
577 if def.is_struct() || def.is_union();
578 if fields.len() == def.non_enum_variant().fields.len();
583 REST_PAT_IN_FULLY_BOUND_STRUCTS,
585 "unnecessary use of `..` pattern in struct binding. All fields were already bound",
587 "consider removing `..` from this binding",
595 fn check_single_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>) {
596 if arms.len() == 2 && arms[0].guard.is_none() && arms[1].guard.is_none() {
597 if in_macro(expr.span) {
598 // Don't lint match expressions present in
599 // macro_rules! block
602 if let PatKind::Or(..) = arms[0].pat.kind {
603 // don't lint for or patterns for now, this makes
604 // the lint noisy in unnecessary situations
607 let els = arms[1].body;
608 let els = if is_unit_expr(remove_blocks(els)) {
610 } else if let ExprKind::Block(Block { stmts, expr: block_expr, .. }, _) = els.kind {
611 if stmts.len() == 1 && block_expr.is_none() || stmts.is_empty() && block_expr.is_some() {
612 // single statement/expr "else" block, don't lint
615 // block with 2+ statements or 1 expr and 1+ statement
619 // not a block, don't lint
623 let ty = cx.tables().expr_ty(ex);
624 if ty.kind != ty::Bool || is_allowed(cx, MATCH_BOOL, ex.hir_id) {
625 check_single_match_single_pattern(cx, ex, arms, expr, els);
626 check_single_match_opt_like(cx, ex, arms, expr, ty, els);
631 fn check_single_match_single_pattern(
632 cx: &LateContext<'_>,
636 els: Option<&Expr<'_>>,
638 if is_wild(&arms[1].pat) {
639 report_single_match_single_pattern(cx, ex, arms, expr, els);
643 fn report_single_match_single_pattern(
644 cx: &LateContext<'_>,
648 els: Option<&Expr<'_>>,
650 let lint = if els.is_some() { SINGLE_MATCH_ELSE } else { SINGLE_MATCH };
651 let els_str = els.map_or(String::new(), |els| {
652 format!(" else {}", expr_block(cx, els, None, "..", Some(expr.span)))
658 "you seem to be trying to use match for destructuring a single pattern. Consider using `if \
662 "if let {} = {} {}{}",
663 snippet(cx, arms[0].pat.span, ".."),
664 snippet(cx, ex.span, ".."),
665 expr_block(cx, &arms[0].body, None, "..", Some(expr.span)),
668 Applicability::HasPlaceholders,
672 fn check_single_match_opt_like(
673 cx: &LateContext<'_>,
678 els: Option<&Expr<'_>>,
680 // list of candidate `Enum`s we know will never get any more members
682 (&paths::COW, "Borrowed"),
683 (&paths::COW, "Cow::Borrowed"),
684 (&paths::COW, "Cow::Owned"),
685 (&paths::COW, "Owned"),
686 (&paths::OPTION, "None"),
687 (&paths::RESULT, "Err"),
688 (&paths::RESULT, "Ok"),
691 let path = match arms[1].pat.kind {
692 PatKind::TupleStruct(ref path, ref inner, _) => {
693 // Contains any non wildcard patterns (e.g., `Err(err)`)?
694 if !inner.iter().all(is_wild) {
697 rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false))
699 PatKind::Binding(BindingAnnotation::Unannotated, .., ident, None) => ident.to_string(),
700 PatKind::Path(ref path) => {
701 rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false))
706 for &(ty_path, pat_path) in candidates {
707 if path == *pat_path && match_type(cx, ty, ty_path) {
708 report_single_match_single_pattern(cx, ex, arms, expr, els);
713 fn check_match_bool(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>) {
714 // Type of expression is `bool`.
715 if cx.tables().expr_ty(ex).kind == ty::Bool {
720 "you seem to be trying to match on a boolean expression",
724 let exprs = if let PatKind::Lit(ref arm_bool) = arms[0].pat.kind {
725 if let ExprKind::Lit(ref lit) = arm_bool.kind {
727 LitKind::Bool(true) => Some((&*arms[0].body, &*arms[1].body)),
728 LitKind::Bool(false) => Some((&*arms[1].body, &*arms[0].body)),
738 if let Some((true_expr, false_expr)) = exprs {
739 let sugg = match (is_unit_expr(true_expr), is_unit_expr(false_expr)) {
740 (false, false) => Some(format!(
742 snippet(cx, ex.span, "b"),
743 expr_block(cx, true_expr, None, "..", Some(expr.span)),
744 expr_block(cx, false_expr, None, "..", Some(expr.span))
746 (false, true) => Some(format!(
748 snippet(cx, ex.span, "b"),
749 expr_block(cx, true_expr, None, "..", Some(expr.span))
752 let test = Sugg::hir(cx, ex, "..");
756 expr_block(cx, false_expr, None, "..", Some(expr.span))
759 (true, true) => None,
762 if let Some(sugg) = sugg {
763 diag.span_suggestion(
765 "consider using an `if`/`else` expression",
767 Applicability::HasPlaceholders,
777 fn check_overlapping_arms<'tcx>(cx: &LateContext<'tcx>, ex: &'tcx Expr<'_>, arms: &'tcx [Arm<'_>]) {
778 if arms.len() >= 2 && cx.tables().expr_ty(ex).is_integral() {
779 let ranges = all_ranges(cx, arms, cx.tables().expr_ty(ex));
780 let type_ranges = type_ranges(&ranges);
781 if !type_ranges.is_empty() {
782 if let Some((start, end)) = overlapping(&type_ranges) {
785 MATCH_OVERLAPPING_ARM,
787 "some ranges overlap",
789 "overlaps with this",
796 fn check_wild_err_arm(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
797 let ex_ty = walk_ptrs_ty(cx.tables().expr_ty(ex));
798 if is_type_diagnostic_item(cx, ex_ty, sym!(result_type)) {
800 if let PatKind::TupleStruct(ref path, ref inner, _) = arm.pat.kind {
801 let path_str = rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false));
802 if path_str == "Err" {
803 let mut matching_wild = inner.iter().any(is_wild);
804 let mut ident_bind_name = String::from("_");
806 // Looking for unused bindings (i.e.: `_e`)
807 inner.iter().for_each(|pat| {
808 if let PatKind::Binding(.., ident, None) = &pat.kind {
809 if ident.as_str().starts_with('_') && is_unused(ident, arm.body) {
810 ident_bind_name = (&ident.name.as_str()).to_string();
811 matching_wild = true;
818 if let ExprKind::Block(ref block, _) = arm.body.kind;
819 if is_panic_block(block);
821 // `Err(_)` or `Err(_e)` arm with `panic!` found
822 span_lint_and_note(cx,
825 &format!("`Err({})` matches all errors", &ident_bind_name),
827 "match each error separately or use the error output, or use `.except(msg)` if the error case is unreachable",
837 fn check_wild_enum_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
838 let ty = cx.tables().expr_ty(ex);
840 // If there isn't a nice closed set of possible values that can be conveniently enumerated,
841 // don't complain about not enumerating the mall.
845 // First pass - check for violation, but don't do much book-keeping because this is hopefully
846 // the uncommon case, and the book-keeping is slightly expensive.
847 let mut wildcard_span = None;
848 let mut wildcard_ident = None;
850 if let PatKind::Wild = arm.pat.kind {
851 wildcard_span = Some(arm.pat.span);
852 } else if let PatKind::Binding(_, _, ident, None) = arm.pat.kind {
853 wildcard_span = Some(arm.pat.span);
854 wildcard_ident = Some(ident);
858 if let Some(wildcard_span) = wildcard_span {
859 // Accumulate the variants which should be put in place of the wildcard because they're not
862 let mut missing_variants = vec![];
863 if let ty::Adt(def, _) = ty.kind {
864 for variant in &def.variants {
865 missing_variants.push(variant);
870 if arm.guard.is_some() {
871 // Guards mean that this case probably isn't exhaustively covered. Technically
872 // this is incorrect, as we should really check whether each variant is exhaustively
873 // covered by the set of guards that cover it, but that's really hard to do.
876 if let PatKind::Path(ref path) = arm.pat.kind {
877 if let QPath::Resolved(_, p) = path {
878 missing_variants.retain(|e| e.ctor_def_id != Some(p.res.def_id()));
880 } else if let PatKind::TupleStruct(ref path, ref patterns, ..) = arm.pat.kind {
881 if let QPath::Resolved(_, p) = path {
882 // Some simple checks for exhaustive patterns.
883 // There is a room for improvements to detect more cases,
884 // but it can be more expensive to do so.
885 let is_pattern_exhaustive =
886 |pat: &&Pat<'_>| matches!(pat.kind, PatKind::Wild | PatKind::Binding(.., None));
887 if patterns.iter().all(is_pattern_exhaustive) {
888 missing_variants.retain(|e| e.ctor_def_id != Some(p.res.def_id()));
894 let mut suggestion: Vec<String> = missing_variants
897 let suffix = match v.ctor_kind {
898 CtorKind::Fn => "(..)",
899 CtorKind::Const | CtorKind::Fictive => "",
901 let ident_str = if let Some(ident) = wildcard_ident {
902 format!("{} @ ", ident.name)
906 // This path assumes that the enum type is imported into scope.
907 format!("{}{}{}", ident_str, cx.tcx.def_path_str(v.def_id), suffix)
911 if suggestion.is_empty() {
915 let mut message = "wildcard match will miss any future added variants";
917 if let ty::Adt(def, _) = ty.kind {
918 if def.is_variant_list_non_exhaustive() {
919 message = "match on non-exhaustive enum doesn't explicitly match all known variants";
920 suggestion.push(String::from("_"));
924 if suggestion.len() == 1 {
925 // No need to check for non-exhaustive enum as in that case len would be greater than 1
928 MATCH_WILDCARD_FOR_SINGLE_VARIANTS,
932 suggestion[0].clone(),
933 Applicability::MaybeIncorrect,
939 WILDCARD_ENUM_MATCH_ARM,
943 suggestion.join(" | "),
944 Applicability::MaybeIncorrect,
949 // If the block contains only a `panic!` macro (as expression or statement)
950 fn is_panic_block(block: &Block<'_>) -> bool {
951 match (&block.expr, block.stmts.len(), block.stmts.first()) {
952 (&Some(ref exp), 0, _) => {
953 is_expn_of(exp.span, "panic").is_some() && is_expn_of(exp.span, "unreachable").is_none()
955 (&None, 1, Some(stmt)) => {
956 is_expn_of(stmt.span, "panic").is_some() && is_expn_of(stmt.span, "unreachable").is_none()
962 fn check_match_ref_pats(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>) {
963 if has_only_ref_pats(arms) {
964 let mut suggs = Vec::with_capacity(arms.len() + 1);
965 let (title, msg) = if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, ref inner) = ex.kind {
966 let span = ex.span.source_callsite();
967 suggs.push((span, Sugg::hir_with_macro_callsite(cx, inner, "..").to_string()));
969 "you don't need to add `&` to both the expression and the patterns",
973 let span = ex.span.source_callsite();
974 suggs.push((span, Sugg::hir_with_macro_callsite(cx, ex, "..").deref().to_string()));
976 "you don't need to add `&` to all patterns",
977 "instead of prefixing all patterns with `&`, you can dereference the expression",
981 suggs.extend(arms.iter().filter_map(|a| {
982 if let PatKind::Ref(ref refp, _) = a.pat.kind {
983 Some((a.pat.span, snippet(cx, refp.span, "..").to_string()))
989 span_lint_and_then(cx, MATCH_REF_PATS, expr.span, title, |diag| {
990 if !expr.span.from_expansion() {
991 multispan_sugg(diag, msg, suggs);
997 fn check_match_as_ref(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>) {
998 if arms.len() == 2 && arms[0].guard.is_none() && arms[1].guard.is_none() {
999 let arm_ref: Option<BindingAnnotation> = if is_none_arm(&arms[0]) {
1000 is_ref_some_arm(&arms[1])
1001 } else if is_none_arm(&arms[1]) {
1002 is_ref_some_arm(&arms[0])
1006 if let Some(rb) = arm_ref {
1007 let suggestion = if rb == BindingAnnotation::Ref {
1013 let output_ty = cx.tables().expr_ty(expr);
1014 let input_ty = cx.tables().expr_ty(ex);
1016 let cast = if_chain! {
1017 if let ty::Adt(_, substs) = input_ty.kind;
1018 let input_ty = substs.type_at(0);
1019 if let ty::Adt(_, substs) = output_ty.kind;
1020 let output_ty = substs.type_at(0);
1021 if let ty::Ref(_, output_ty, _) = output_ty.kind;
1022 if input_ty != output_ty;
1030 let mut applicability = Applicability::MachineApplicable;
1035 &format!("use `{}()` instead", suggestion),
1039 snippet_with_applicability(cx, ex.span, "_", &mut applicability),
1049 fn check_wild_in_or_pats(cx: &LateContext<'_>, arms: &[Arm<'_>]) {
1051 if let PatKind::Or(ref fields) = arm.pat.kind {
1052 // look for multiple fields in this arm that contains at least one Wild pattern
1053 if fields.len() > 1 && fields.iter().any(is_wild) {
1056 WILDCARD_IN_OR_PATTERNS,
1058 "wildcard pattern covers any other pattern as it will match anyway.",
1060 "Consider handling `_` separately.",
1067 /// Lint a `match` or `if let .. { .. } else { .. }` expr that could be replaced by `matches!`
1068 fn check_match_like_matches<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
1069 if let ExprKind::Match(ex, arms, ref match_source) = &expr.kind {
1070 match match_source {
1071 MatchSource::Normal => find_matches_sugg(cx, ex, arms, expr, false),
1072 MatchSource::IfLetDesugar { .. } => find_matches_sugg(cx, ex, arms, expr, true),
1078 /// Lint a `match` or desugared `if let` for replacement by `matches!`
1079 fn find_matches_sugg(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr: &Expr<'_>, desugared: bool) {
1082 if cx.tables().expr_ty(expr).is_bool();
1083 if is_wild(&arms[1].pat);
1084 if let Some(first) = find_bool_lit(&arms[0].body.kind, desugared);
1085 if let Some(second) = find_bool_lit(&arms[1].body.kind, desugared);
1088 let mut applicability = Applicability::MachineApplicable;
1090 let pat_and_guard = if let Some(Guard::If(g)) = arms[0].guard {
1091 format!("{} if {}", snippet_with_applicability(cx, arms[0].pat.span, "..", &mut applicability), snippet_with_applicability(cx, g.span, "..", &mut applicability))
1093 format!("{}", snippet_with_applicability(cx, arms[0].pat.span, "..", &mut applicability))
1097 MATCH_LIKE_MATCHES_MACRO,
1099 &format!("{} expression looks like `matches!` macro", if desugared { "if let .. else" } else { "match" }),
1102 "{}matches!({}, {})",
1103 if first { "" } else { "!" },
1104 snippet_with_applicability(cx, ex.span, "..", &mut applicability),
1113 /// Extract a `bool` or `{ bool }`
1114 fn find_bool_lit(ex: &ExprKind<'_>, desugared: bool) -> Option<bool> {
1116 ExprKind::Lit(Spanned {
1117 node: LitKind::Bool(b), ..
1127 if let ExprKind::Lit(Spanned {
1128 node: LitKind::Bool(b), ..
1140 fn check_match_single_binding<'a>(cx: &LateContext<'a>, ex: &Expr<'a>, arms: &[Arm<'_>], expr: &Expr<'_>) {
1141 if in_macro(expr.span) || arms.len() != 1 || is_refutable(cx, arms[0].pat) {
1144 let matched_vars = ex.span;
1145 let bind_names = arms[0].pat.span;
1146 let match_body = remove_blocks(&arms[0].body);
1147 let mut snippet_body = if match_body.span.from_expansion() {
1148 Sugg::hir_with_macro_callsite(cx, match_body, "..").to_string()
1150 snippet_block(cx, match_body.span, "..", Some(expr.span)).to_string()
1153 // Do we need to add ';' to suggestion ?
1154 match match_body.kind {
1155 ExprKind::Block(block, _) => {
1156 // macro + expr_ty(body) == ()
1157 if block.span.from_expansion() && cx.tables().expr_ty(&match_body).is_unit() {
1158 snippet_body.push(';');
1162 // expr_ty(body) == ()
1163 if cx.tables().expr_ty(&match_body).is_unit() {
1164 snippet_body.push(';');
1169 let mut applicability = Applicability::MaybeIncorrect;
1170 match arms[0].pat.kind {
1171 PatKind::Binding(..) | PatKind::Tuple(_, _) | PatKind::Struct(..) => {
1172 // If this match is in a local (`let`) stmt
1173 let (target_span, sugg) = if let Some(parent_let_node) = opt_parent_let(cx, ex) {
1175 parent_let_node.span,
1177 "let {} = {};\n{}let {} = {};",
1178 snippet_with_applicability(cx, bind_names, "..", &mut applicability),
1179 snippet_with_applicability(cx, matched_vars, "..", &mut applicability),
1180 " ".repeat(indent_of(cx, expr.span).unwrap_or(0)),
1181 snippet_with_applicability(cx, parent_let_node.pat.span, "..", &mut applicability),
1186 // If we are in closure, we need curly braces around suggestion
1187 let mut indent = " ".repeat(indent_of(cx, ex.span).unwrap_or(0));
1188 let (mut cbrace_start, mut cbrace_end) = ("".to_string(), "".to_string());
1189 if let Some(parent_expr) = get_parent_expr(cx, expr) {
1190 if let ExprKind::Closure(..) = parent_expr.kind {
1191 cbrace_end = format!("\n{}}}", indent);
1192 // Fix body indent due to the closure
1193 indent = " ".repeat(indent_of(cx, bind_names).unwrap_or(0));
1194 cbrace_start = format!("{{\n{}", indent);
1200 "{}let {} = {};\n{}{}{}",
1202 snippet_with_applicability(cx, bind_names, "..", &mut applicability),
1203 snippet_with_applicability(cx, matched_vars, "..", &mut applicability),
1212 MATCH_SINGLE_BINDING,
1214 "this match could be written as a `let` statement",
1215 "consider using `let` statement",
1223 MATCH_SINGLE_BINDING,
1225 "this match could be replaced by its body itself",
1226 "consider using the match body instead",
1228 Applicability::MachineApplicable,
1235 /// Returns true if the `ex` match expression is in a local (`let`) statement
1236 fn opt_parent_let<'a>(cx: &LateContext<'a>, ex: &Expr<'a>) -> Option<&'a Local<'a>> {
1238 let map = &cx.tcx.hir();
1239 if let Some(Node::Expr(parent_arm_expr)) = map.find(map.get_parent_node(ex.hir_id));
1240 if let Some(Node::Local(parent_let_expr)) = map.find(map.get_parent_node(parent_arm_expr.hir_id));
1242 return Some(parent_let_expr);
1248 /// Gets all arms that are unbounded `PatRange`s.
1249 fn all_ranges<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>], ty: Ty<'tcx>) -> Vec<SpannedRange<Constant>> {
1253 ref pat, guard: None, ..
1256 if let PatKind::Range(ref lhs, ref rhs, range_end) = pat.kind {
1257 let lhs = match lhs {
1258 Some(lhs) => constant(cx, cx.tables(), lhs)?.0,
1259 None => miri_to_const(ty.numeric_min_val(cx.tcx)?)?,
1261 let rhs = match rhs {
1262 Some(rhs) => constant(cx, cx.tables(), rhs)?.0,
1263 None => miri_to_const(ty.numeric_max_val(cx.tcx)?)?,
1265 let rhs = match range_end {
1266 RangeEnd::Included => Bound::Included(rhs),
1267 RangeEnd::Excluded => Bound::Excluded(rhs),
1269 return Some(SpannedRange {
1275 if let PatKind::Lit(ref value) = pat.kind {
1276 let value = constant(cx, cx.tables(), value)?.0;
1277 return Some(SpannedRange {
1279 node: (value.clone(), Bound::Included(value)),
1288 #[derive(Debug, Eq, PartialEq)]
1289 pub struct SpannedRange<T> {
1291 pub node: (T, Bound<T>),
1294 type TypedRanges = Vec<SpannedRange<u128>>;
1296 /// Gets all `Int` ranges or all `Uint` ranges. Mixed types are an error anyway
1297 /// and other types than
1298 /// `Uint` and `Int` probably don't make sense.
1299 fn type_ranges(ranges: &[SpannedRange<Constant>]) -> TypedRanges {
1302 .filter_map(|range| match range.node {
1303 (Constant::Int(start), Bound::Included(Constant::Int(end))) => Some(SpannedRange {
1305 node: (start, Bound::Included(end)),
1307 (Constant::Int(start), Bound::Excluded(Constant::Int(end))) => Some(SpannedRange {
1309 node: (start, Bound::Excluded(end)),
1311 (Constant::Int(start), Bound::Unbounded) => Some(SpannedRange {
1313 node: (start, Bound::Unbounded),
1320 fn is_unit_expr(expr: &Expr<'_>) -> bool {
1322 ExprKind::Tup(ref v) if v.is_empty() => true,
1323 ExprKind::Block(ref b, _) if b.stmts.is_empty() && b.expr.is_none() => true,
1328 // Checks if arm has the form `None => None`
1329 fn is_none_arm(arm: &Arm<'_>) -> bool {
1330 matches!(arm.pat.kind, PatKind::Path(ref path) if match_qpath(path, &paths::OPTION_NONE))
1333 // Checks if arm has the form `Some(ref v) => Some(v)` (checks for `ref` and `ref mut`)
1334 fn is_ref_some_arm(arm: &Arm<'_>) -> Option<BindingAnnotation> {
1336 if let PatKind::TupleStruct(ref path, ref pats, _) = arm.pat.kind;
1337 if pats.len() == 1 && match_qpath(path, &paths::OPTION_SOME);
1338 if let PatKind::Binding(rb, .., ident, _) = pats[0].kind;
1339 if rb == BindingAnnotation::Ref || rb == BindingAnnotation::RefMut;
1340 if let ExprKind::Call(ref e, ref args) = remove_blocks(&arm.body).kind;
1341 if let ExprKind::Path(ref some_path) = e.kind;
1342 if match_qpath(some_path, &paths::OPTION_SOME) && args.len() == 1;
1343 if let ExprKind::Path(ref qpath) = args[0].kind;
1344 if let &QPath::Resolved(_, ref path2) = qpath;
1345 if path2.segments.len() == 1 && ident.name == path2.segments[0].ident.name;
1353 fn has_only_ref_pats(arms: &[Arm<'_>]) -> bool {
1358 PatKind::Ref(..) => Some(true), // &-patterns
1359 PatKind::Wild => Some(false), // an "anything" wildcard is also fine
1360 _ => None, // any other pattern is not fine
1363 .collect::<Option<Vec<bool>>>();
1364 // look for Some(v) where there's at least one true element
1365 mapped.map_or(false, |v| v.iter().any(|el| *el))
1368 pub fn overlapping<T>(ranges: &[SpannedRange<T>]) -> Option<(&SpannedRange<T>, &SpannedRange<T>)>
1372 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
1374 Start(T, &'a SpannedRange<T>),
1375 End(Bound<T>, &'a SpannedRange<T>),
1378 impl<'a, T: Copy> Kind<'a, T> {
1379 fn range(&self) -> &'a SpannedRange<T> {
1381 Kind::Start(_, r) | Kind::End(_, r) => r,
1385 fn value(self) -> Bound<T> {
1387 Kind::Start(t, _) => Bound::Included(t),
1388 Kind::End(t, _) => t,
1393 impl<'a, T: Copy + Ord> PartialOrd for Kind<'a, T> {
1394 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1395 Some(self.cmp(other))
1399 impl<'a, T: Copy + Ord> Ord for Kind<'a, T> {
1400 fn cmp(&self, other: &Self) -> Ordering {
1401 match (self.value(), other.value()) {
1402 (Bound::Included(a), Bound::Included(b)) | (Bound::Excluded(a), Bound::Excluded(b)) => a.cmp(&b),
1403 // Range patterns cannot be unbounded (yet)
1404 (Bound::Unbounded, _) | (_, Bound::Unbounded) => unimplemented!(),
1405 (Bound::Included(a), Bound::Excluded(b)) => match a.cmp(&b) {
1406 Ordering::Equal => Ordering::Greater,
1409 (Bound::Excluded(a), Bound::Included(b)) => match a.cmp(&b) {
1410 Ordering::Equal => Ordering::Less,
1417 let mut values = Vec::with_capacity(2 * ranges.len());
1420 values.push(Kind::Start(r.node.0, r));
1421 values.push(Kind::End(r.node.1, r));
1426 for (a, b) in values.iter().zip(values.iter().skip(1)) {
1428 (&Kind::Start(_, ra), &Kind::End(_, rb)) => {
1429 if ra.node != rb.node {
1430 return Some((ra, rb));
1433 (&Kind::End(a, _), &Kind::Start(b, _)) if a != Bound::Included(b) => (),
1434 _ => return Some((a.range(), b.range())),
1441 mod redundant_pattern_match {
1442 use super::REDUNDANT_PATTERN_MATCHING;
1443 use crate::utils::{in_constant, match_qpath, match_trait_method, paths, snippet, span_lint_and_then};
1444 use if_chain::if_chain;
1445 use rustc_ast::ast::LitKind;
1446 use rustc_errors::Applicability;
1447 use rustc_hir::{Arm, Expr, ExprKind, HirId, MatchSource, PatKind, QPath};
1448 use rustc_lint::LateContext;
1449 use rustc_middle::ty;
1450 use rustc_mir::const_eval::is_const_fn;
1451 use rustc_span::source_map::Symbol;
1453 pub fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
1454 if let ExprKind::Match(op, arms, ref match_source) = &expr.kind {
1455 match match_source {
1456 MatchSource::Normal => find_sugg_for_match(cx, expr, op, arms),
1457 MatchSource::IfLetDesugar { .. } => find_sugg_for_if_let(cx, expr, op, arms, "if"),
1458 MatchSource::WhileLetDesugar => find_sugg_for_if_let(cx, expr, op, arms, "while"),
1464 fn find_sugg_for_if_let<'tcx>(
1465 cx: &LateContext<'tcx>,
1466 expr: &'tcx Expr<'_>,
1469 keyword: &'static str,
1471 fn find_suggestion(cx: &LateContext<'_>, hir_id: HirId, path: &QPath<'_>) -> Option<&'static str> {
1472 if match_qpath(path, &paths::RESULT_OK) && can_suggest(cx, hir_id, sym!(result_type), "is_ok") {
1473 return Some("is_ok()");
1475 if match_qpath(path, &paths::RESULT_ERR) && can_suggest(cx, hir_id, sym!(result_type), "is_err") {
1476 return Some("is_err()");
1478 if match_qpath(path, &paths::OPTION_SOME) && can_suggest(cx, hir_id, sym!(option_type), "is_some") {
1479 return Some("is_some()");
1481 if match_qpath(path, &paths::OPTION_NONE) && can_suggest(cx, hir_id, sym!(option_type), "is_none") {
1482 return Some("is_none()");
1487 let hir_id = expr.hir_id;
1488 let good_method = match arms[0].pat.kind {
1489 PatKind::TupleStruct(ref path, ref patterns, _) if patterns.len() == 1 => {
1490 if let PatKind::Wild = patterns[0].kind {
1491 find_suggestion(cx, hir_id, path)
1496 PatKind::Path(ref path) => find_suggestion(cx, hir_id, path),
1499 let good_method = match good_method {
1500 Some(method) => method,
1504 // check that `while_let_on_iterator` lint does not trigger
1506 if keyword == "while";
1507 if let ExprKind::MethodCall(method_path, _, _, _) = op.kind;
1508 if method_path.ident.name == sym!(next);
1509 if match_trait_method(cx, op, &paths::ITERATOR);
1517 REDUNDANT_PATTERN_MATCHING,
1519 &format!("redundant pattern matching, consider using `{}`", good_method),
1521 // while let ... = ... { ... }
1522 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1523 let expr_span = expr.span;
1525 // while let ... = ... { ... }
1527 let op_span = op.span.source_callsite();
1529 // while let ... = ... { ... }
1530 // ^^^^^^^^^^^^^^^^^^^
1531 let span = expr_span.until(op_span.shrink_to_hi());
1532 diag.span_suggestion(
1535 format!("{} {}.{}", keyword, snippet(cx, op_span, "_"), good_method),
1536 Applicability::MachineApplicable, // snippet
1542 fn find_sugg_for_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op: &Expr<'_>, arms: &[Arm<'_>]) {
1543 if arms.len() == 2 {
1544 let node_pair = (&arms[0].pat.kind, &arms[1].pat.kind);
1546 let hir_id = expr.hir_id;
1547 let found_good_method = match node_pair {
1549 PatKind::TupleStruct(ref path_left, ref patterns_left, _),
1550 PatKind::TupleStruct(ref path_right, ref patterns_right, _),
1551 ) if patterns_left.len() == 1 && patterns_right.len() == 1 => {
1552 if let (PatKind::Wild, PatKind::Wild) = (&patterns_left[0].kind, &patterns_right[0].kind) {
1553 find_good_method_for_match(
1561 || can_suggest(cx, hir_id, sym!(result_type), "is_ok"),
1562 || can_suggest(cx, hir_id, sym!(result_type), "is_err"),
1568 (PatKind::TupleStruct(ref path_left, ref patterns, _), PatKind::Path(ref path_right))
1569 | (PatKind::Path(ref path_left), PatKind::TupleStruct(ref path_right, ref patterns, _))
1570 if patterns.len() == 1 =>
1572 if let PatKind::Wild = patterns[0].kind {
1573 find_good_method_for_match(
1577 &paths::OPTION_SOME,
1578 &paths::OPTION_NONE,
1581 || can_suggest(cx, hir_id, sym!(option_type), "is_some"),
1582 || can_suggest(cx, hir_id, sym!(option_type), "is_none"),
1591 if let Some(good_method) = found_good_method {
1594 REDUNDANT_PATTERN_MATCHING,
1596 &format!("redundant pattern matching, consider using `{}`", good_method),
1598 let span = expr.span.to(op.span);
1599 diag.span_suggestion(
1602 format!("{}.{}", snippet(cx, op.span, "_"), good_method),
1603 Applicability::MaybeIncorrect, // snippet
1611 #[allow(clippy::too_many_arguments)]
1612 fn find_good_method_for_match<'a>(
1614 path_left: &QPath<'_>,
1615 path_right: &QPath<'_>,
1616 expected_left: &[&str],
1617 expected_right: &[&str],
1618 should_be_left: &'a str,
1619 should_be_right: &'a str,
1620 can_suggest_left: impl Fn() -> bool,
1621 can_suggest_right: impl Fn() -> bool,
1622 ) -> Option<&'a str> {
1623 let body_node_pair = if match_qpath(path_left, expected_left) && match_qpath(path_right, expected_right) {
1624 (&(*arms[0].body).kind, &(*arms[1].body).kind)
1625 } else if match_qpath(path_right, expected_left) && match_qpath(path_left, expected_right) {
1626 (&(*arms[1].body).kind, &(*arms[0].body).kind)
1631 match body_node_pair {
1632 (ExprKind::Lit(ref lit_left), ExprKind::Lit(ref lit_right)) => match (&lit_left.node, &lit_right.node) {
1633 (LitKind::Bool(true), LitKind::Bool(false)) if can_suggest_left() => Some(should_be_left),
1634 (LitKind::Bool(false), LitKind::Bool(true)) if can_suggest_right() => Some(should_be_right),
1641 fn can_suggest(cx: &LateContext<'_>, hir_id: HirId, diag_item: Symbol, name: &str) -> bool {
1642 if !in_constant(cx, hir_id) {
1646 // Avoid suggesting calls to non-`const fn`s in const contexts, see #5697.
1648 .get_diagnostic_item(diag_item)
1649 .and_then(|def_id| {
1650 cx.tcx.inherent_impls(def_id).iter().find_map(|imp| {
1652 .associated_items(*imp)
1653 .in_definition_order()
1654 .find_map(|item| match item.kind {
1655 ty::AssocKind::Fn if item.ident.name.as_str() == name => Some(item.def_id),
1660 .map_or(false, |def_id| is_const_fn(cx.tcx, def_id))
1665 fn test_overlapping() {
1666 use rustc_span::source_map::DUMMY_SP;
1668 let sp = |s, e| SpannedRange {
1673 assert_eq!(None, overlapping::<u8>(&[]));
1674 assert_eq!(None, overlapping(&[sp(1, Bound::Included(4))]));
1677 overlapping(&[sp(1, Bound::Included(4)), sp(5, Bound::Included(6))])
1682 sp(1, Bound::Included(4)),
1683 sp(5, Bound::Included(6)),
1684 sp(10, Bound::Included(11))
1688 Some((&sp(1, Bound::Included(4)), &sp(3, Bound::Included(6)))),
1689 overlapping(&[sp(1, Bound::Included(4)), sp(3, Bound::Included(6))])
1692 Some((&sp(5, Bound::Included(6)), &sp(6, Bound::Included(11)))),
1694 sp(1, Bound::Included(4)),
1695 sp(5, Bound::Included(6)),
1696 sp(6, Bound::Included(11))