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+ & cond. kind ,
93+ cond_inner. span ,
94+ els. span ,
95+ ".." ,
96+ & mut applicability,
97+ ) ,
98+ applicability,
9099 ) ,
91100 _ => span_lint_and_help ( cx, IF_NOT_ELSE , e. span , msg, None , help) ,
92101 }
@@ -97,28 +106,26 @@ impl LateLintPass<'_> for IfNotElse {
97106
98107fn make_sugg < ' a > (
99108 sess : & impl HasSession ,
109+ expr_span : Span ,
100110 cond_kind : & ' a ExprKind < ' a > ,
101111 cond_inner : Span ,
102112 els_span : Span ,
103113 default : & ' a str ,
104- indent_relative_to : Option < Span > ,
114+ applicability : & mut Applicability ,
105115) -> 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 ) ) ;
116+ let ( cond_inner_snip, _ ) = snippet_with_context ( sess, cond_inner, expr_span . ctxt ( ) , default, applicability ) ;
117+ let ( els_snip, _ ) = snippet_with_context ( sess, els_span, expr_span . ctxt ( ) , default, applicability ) ;
118+ let indent = indent_of ( sess, expr_span ) ;
109119
110120 let suggestion = match cond_kind {
111121 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- )
122+ let ( cond_rest_snip, _) =
123+ snippet_with_context ( sess, cond_rest. span , expr_span. ctxt ( ) , default, applicability) ;
124+ format ! ( "if {cond_rest_snip} {els_snip} else {cond_inner_snip}" )
118125 } ,
119126 ExprKind :: Binary ( _, lhs, rhs) => {
120- let lhs_snip = snippet ( sess, lhs. span , default) ;
121- let rhs_snip = snippet ( sess, rhs. span , default) ;
127+ let ( lhs_snip, _ ) = snippet_with_context ( sess, lhs. span , expr_span . ctxt ( ) , default, applicability ) ;
128+ let ( rhs_snip, _ ) = snippet_with_context ( sess, rhs. span , expr_span . ctxt ( ) , default, applicability ) ;
122129
123130 format ! ( "if {lhs_snip} == {rhs_snip} {els_snip} else {cond_inner_snip}" )
124131 } ,
0 commit comments