@@ -176,13 +176,18 @@ fn check_inverted_bool_in_condition(
176176    ) ; 
177177} 
178178
179- fn  check_simplify_not ( cx :  & LateContext < ' _ > ,  expr :  & Expr < ' _ > )  { 
179+ fn  check_simplify_not ( cx :  & LateContext < ' _ > ,  expr :  & Expr < ' _ > ,   need_parens :   bool )  { 
180180    if  let  ExprKind :: Unary ( UnOp :: Not ,  inner)  = & expr. kind 
181181        && !expr. span . from_expansion ( ) 
182182        && !inner. span . from_expansion ( ) 
183183        && let  Some ( suggestion)  = simplify_not ( cx,  inner) 
184184        && cx. tcx . lint_level_at_node ( NONMINIMAL_BOOL ,  expr. hir_id ) . 0  != Level :: Allow 
185185    { 
186+         let  suggestion = if  need_parens { 
187+             format ! ( "({suggestion})" ) 
188+         }  else  { 
189+             suggestion
190+         } ; 
186191        span_lint_and_sugg ( 
187192            cx, 
188193            NONMINIMAL_BOOL , 
@@ -476,7 +481,7 @@ fn terminal_stats(b: &Bool) -> Stats {
476481} 
477482
478483impl < ' a ,  ' tcx >  NonminimalBoolVisitor < ' a ,  ' tcx >  { 
479-     fn  bool_expr ( & self ,  e :  & ' tcx  Expr < ' _ > )  { 
484+     fn  bool_expr ( & self ,  e :  & ' tcx  Expr < ' _ > ,   need_parens :   bool )  { 
480485        let  mut  h2q = Hir2Qmm  { 
481486            terminals :  Vec :: new ( ) , 
482487            cx :  self . cx , 
@@ -569,7 +574,7 @@ impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
569574                } 
570575            } ; 
571576            if  improvements. is_empty ( )  { 
572-                 check_simplify_not ( self . cx ,  e) ; 
577+                 check_simplify_not ( self . cx ,  e,  need_parens ) ; 
573578            }  else  { 
574579                nonminimal_bool_lint ( 
575580                    improvements
@@ -586,8 +591,15 @@ impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {
586591    fn  visit_expr ( & mut  self ,  e :  & ' tcx  Expr < ' _ > )  { 
587592        if  !e. span . from_expansion ( )  { 
588593            match  & e. kind  { 
594+                 ExprKind :: Cast ( inner,  _)  => { 
595+                     if  self . cx . typeck_results ( ) . expr_ty ( inner) . is_bool ( )  { 
596+                         // we are casting bool into some type, so we may need a parenthesis (#12761) 
597+                         self . bool_expr ( inner,  true ) ; 
598+                         return ; 
599+                     } 
600+                 } , 
589601                ExprKind :: Binary ( binop,  _,  _)  if  binop. node  == BinOpKind :: Or  || binop. node  == BinOpKind :: And  => { 
590-                     self . bool_expr ( e) ; 
602+                     self . bool_expr ( e,   false ) ; 
591603                } , 
592604                ExprKind :: Unary ( UnOp :: Not ,  inner)  => { 
593605                    if  let  ExprKind :: Unary ( UnOp :: Not ,  ex)  = inner. kind 
@@ -596,7 +608,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {
596608                        return ; 
597609                    } 
598610                    if  self . cx . typeck_results ( ) . node_types ( ) [ inner. hir_id ] . is_bool ( )  { 
599-                         self . bool_expr ( e) ; 
611+                         self . bool_expr ( e,   false ) ; 
600612                    } 
601613                } , 
602614                _ => { } , 
0 commit comments