@@ -4,17 +4,16 @@ use clippy_utils::diagnostics::{
44} ;
55use clippy_utils:: macros:: { is_panic, root_macro_call} ;
66use clippy_utils:: peel_blocks_with_stmt;
7- use clippy_utils:: source:: { expr_block , indent_of, snippet, snippet_block, snippet_opt, snippet_with_applicability} ;
7+ use clippy_utils:: source:: { indent_of, snippet, snippet_block, snippet_opt, snippet_with_applicability} ;
88use clippy_utils:: sugg:: Sugg ;
99use clippy_utils:: ty:: is_type_diagnostic_item;
1010use clippy_utils:: visitors:: is_local_used;
1111use clippy_utils:: {
12- get_parent_expr, is_lang_ctor, is_refutable, is_unit_expr , is_wild, meets_msrv, msrvs, path_to_local_id,
13- peel_blocks , peel_hir_pat_refs, recurse_or_patterns, strip_pat_refs,
12+ get_parent_expr, is_lang_ctor, is_refutable, is_wild, meets_msrv, msrvs, path_to_local_id, peel_blocks ,
13+ peel_hir_pat_refs, recurse_or_patterns, strip_pat_refs,
1414} ;
1515use core:: iter:: once;
1616use if_chain:: if_chain;
17- use rustc_ast:: ast:: LitKind ;
1817use rustc_errors:: Applicability ;
1918use rustc_hir:: def:: { CtorKind , DefKind , Res } ;
2019use rustc_hir:: LangItem :: { OptionNone , OptionSome } ;
@@ -29,6 +28,7 @@ use rustc_session::{declare_tool_lint, impl_lint_pass};
2928use rustc_span:: { sym, symbol:: kw, Span } ;
3029use std:: cmp:: Ordering ;
3130
31+ mod match_bool;
3232mod match_like_matches;
3333mod match_same_arms;
3434mod redundant_pattern_match;
@@ -631,7 +631,7 @@ impl<'tcx> LateLintPass<'tcx> for Matches {
631631
632632 if let ExprKind :: Match ( ex, arms, MatchSource :: Normal ) = expr. kind {
633633 single_match:: check ( cx, ex, arms, expr) ;
634- check_match_bool ( cx, ex, arms, expr) ;
634+ match_bool :: check ( cx, ex, arms, expr) ;
635635 check_overlapping_arms ( cx, ex, arms) ;
636636 check_wild_err_arm ( cx, ex, arms) ;
637637 check_wild_enum_match ( cx, ex, arms) ;
@@ -710,70 +710,6 @@ impl<'tcx> LateLintPass<'tcx> for Matches {
710710 extract_msrv_attr ! ( LateContext ) ;
711711}
712712
713- fn check_match_bool ( cx : & LateContext < ' _ > , ex : & Expr < ' _ > , arms : & [ Arm < ' _ > ] , expr : & Expr < ' _ > ) {
714- // Type of expression is `bool`.
715- if * cx. typeck_results ( ) . expr_ty ( ex) . kind ( ) == ty:: Bool {
716- span_lint_and_then (
717- cx,
718- MATCH_BOOL ,
719- expr. span ,
720- "you seem to be trying to match on a boolean expression" ,
721- move |diag| {
722- if arms. len ( ) == 2 {
723- // no guards
724- let exprs = if let PatKind :: Lit ( arm_bool) = arms[ 0 ] . pat . kind {
725- if let ExprKind :: Lit ( ref lit) = arm_bool. kind {
726- match lit. node {
727- LitKind :: Bool ( true ) => Some ( ( & * arms[ 0 ] . body , & * arms[ 1 ] . body ) ) ,
728- LitKind :: Bool ( false ) => Some ( ( & * arms[ 1 ] . body , & * arms[ 0 ] . body ) ) ,
729- _ => None ,
730- }
731- } else {
732- None
733- }
734- } else {
735- None
736- } ;
737-
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 ! (
741- "if {} {} else {}" ,
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) )
745- ) ) ,
746- ( false , true ) => Some ( format ! (
747- "if {} {}" ,
748- snippet( cx, ex. span, "b" ) ,
749- expr_block( cx, true_expr, None , ".." , Some ( expr. span) )
750- ) ) ,
751- ( true , false ) => {
752- let test = Sugg :: hir ( cx, ex, ".." ) ;
753- Some ( format ! (
754- "if {} {}" ,
755- !test,
756- expr_block( cx, false_expr, None , ".." , Some ( expr. span) )
757- ) )
758- } ,
759- ( true , true ) => None ,
760- } ;
761-
762- if let Some ( sugg) = sugg {
763- diag. span_suggestion (
764- expr. span ,
765- "consider using an `if`/`else` expression" ,
766- sugg,
767- Applicability :: HasPlaceholders ,
768- ) ;
769- }
770- }
771- }
772- } ,
773- ) ;
774- }
775- }
776-
777713fn check_overlapping_arms < ' tcx > ( cx : & LateContext < ' tcx > , ex : & ' tcx Expr < ' _ > , arms : & ' tcx [ Arm < ' _ > ] ) {
778714 if arms. len ( ) >= 2 && cx. typeck_results ( ) . expr_ty ( ex) . is_integral ( ) {
779715 let ranges = all_ranges ( cx, arms, cx. typeck_results ( ) . expr_ty ( ex) ) ;
0 commit comments