11use  clippy_utils:: consts:: is_zero_integer_const; 
22use  clippy_utils:: diagnostics:: { span_lint_and_help,  span_lint_and_sugg} ; 
33use  clippy_utils:: is_else_clause; 
4- use  clippy_utils:: source:: { HasSession ,  indent_of,  reindent_multiline,  snippet } ; 
4+ use  clippy_utils:: source:: { HasSession ,  indent_of,  reindent_multiline,  snippet_with_context } ; 
55use  rustc_errors:: Applicability ; 
66use  rustc_hir:: { BinOpKind ,  Expr ,  ExprKind ,  UnOp } ; 
77use  rustc_lint:: { LateContext ,  LateLintPass } ; 
@@ -78,15 +78,24 @@ impl LateLintPass<'_> for IfNotElse {
7878            // } 
7979            // ``` 
8080            if  !e. span . from_expansion ( )  && !is_else_clause ( cx. tcx ,  e)  { 
81+                 let  mut  applicability = Applicability :: MachineApplicable ; 
8182                match  cond. kind  { 
8283                    ExprKind :: Unary ( UnOp :: Not ,  _)  | ExprKind :: Binary ( _,  _,  _)  => span_lint_and_sugg ( 
8384                        cx, 
8485                        IF_NOT_ELSE , 
8586                        e. span , 
8687                        msg, 
8788                        "try" , 
88-                         make_sugg ( cx,  & cond. kind ,  cond_inner. span ,  els. span ,  ".." ,  Some ( e. span ) ) , 
89-                         Applicability :: MachineApplicable , 
89+                         make_sugg ( 
90+                             cx, 
91+                             e. span , 
92+                             & mut  applicability, 
93+                             & cond. kind , 
94+                             cond_inner. span , 
95+                             els. span , 
96+                             ".." , 
97+                         ) , 
98+                         applicability, 
9099                    ) , 
91100                    _ => span_lint_and_help ( cx,  IF_NOT_ELSE ,  e. span ,  msg,  None ,  help) , 
92101                } 
@@ -95,30 +104,29 @@ impl LateLintPass<'_> for IfNotElse {
95104    } 
96105} 
97106
107+ #[ expect( clippy:: too_many_arguments) ]  
98108fn  make_sugg < ' a > ( 
99109    sess :  & impl  HasSession , 
110+     expr_span :  Span , 
111+     applicability :  & mut  Applicability , 
100112    cond_kind :  & ' a  ExprKind < ' a > , 
101113    cond_inner :  Span , 
102114    els_span :  Span , 
103115    default :  & ' a  str , 
104-     indent_relative_to :  Option < Span > , 
105116)  -> String  { 
106-     let  cond_inner_snip =  snippet ( sess,  cond_inner,  default) ; 
107-     let  els_snip =  snippet ( sess,  els_span,  default) ; 
108-     let  indent = indent_relative_to . and_then ( |s|  indent_of ( sess,  s ) ) ; 
117+     let  ( cond_inner_snip,  _ )  =  snippet_with_context ( sess,  cond_inner,  expr_span . ctxt ( ) ,   default,  applicability ) ; 
118+     let  ( els_snip,  _ )  =  snippet_with_context ( sess,  els_span,  expr_span . ctxt ( ) ,   default,  applicability ) ; 
119+     let  indent = indent_of ( sess,  expr_span ) ; 
109120
110121    let  suggestion = match  cond_kind { 
111122        ExprKind :: Unary ( UnOp :: Not ,  cond_rest)  => { 
112-             format ! ( 
113-                 "if {} {} else {}" , 
114-                 snippet( sess,  cond_rest. span,  default ) , 
115-                 els_snip, 
116-                 cond_inner_snip
117-             ) 
123+             let  ( cond_rest_snip,  _)  =
124+                 snippet_with_context ( sess,  cond_rest. span ,  expr_span. ctxt ( ) ,  default,  applicability) ; 
125+             format ! ( "if {cond_rest_snip} {els_snip} else {cond_inner_snip}" ) 
118126        } , 
119127        ExprKind :: Binary ( _,  lhs,  rhs)  => { 
120-             let  lhs_snip =  snippet ( sess,  lhs. span ,  default) ; 
121-             let  rhs_snip =  snippet ( sess,  rhs. span ,  default) ; 
128+             let  ( lhs_snip,  _ )  =  snippet_with_context ( sess,  lhs. span ,  expr_span . ctxt ( ) ,   default,  applicability ) ; 
129+             let  ( rhs_snip,  _ )  =  snippet_with_context ( sess,  rhs. span ,  expr_span . ctxt ( ) ,   default,  applicability ) ; 
122130
123131            format ! ( "if {lhs_snip} == {rhs_snip} {els_snip} else {cond_inner_snip}" ) 
124132        } , 
0 commit comments