33//! This lint is **warn** by default
44
55use crate :: utils:: sugg:: Sugg ;
6- use crate :: utils:: { higher, parent_node_is_if_expr, span_lint , span_lint_and_help , span_lint_and_sugg, snippet_with_applicability } ;
6+ use crate :: utils:: { higher, parent_node_is_if_expr, snippet_with_applicability , span_lint , span_lint_and_sugg} ;
77use if_chain:: if_chain;
88use rustc_ast:: ast:: LitKind ;
99use rustc_errors:: Applicability ;
1010use rustc_hir:: { BinOpKind , Block , Expr , ExprKind , StmtKind , UnOp } ;
1111use rustc_lint:: { LateContext , LateLintPass } ;
1212use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
1313use rustc_span:: source_map:: Spanned ;
14+ use rustc_span:: Span ;
1415
1516declare_clippy_lint ! {
1617 /// **What it does:** Checks for expressions of the form `if c { true } else {
@@ -189,7 +190,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoolComparison {
189190 }
190191}
191192
192- fn is_unary_not < ' tcx > ( e : & ' tcx Expr < ' _ > ) -> ( bool , rustc_span:: Span ) {
193+ struct ExpressionInfoWithSpan {
194+ one_side_is_unary_not : bool ,
195+ left_span : Span ,
196+ right_span : Span ,
197+ }
198+
199+ fn is_unary_not ( e : & Expr < ' _ > ) -> ( bool , Span ) {
193200 if_chain ! {
194201 if let ExprKind :: Unary ( unop, operand) = e. kind;
195202 if let UnOp :: UnNot = unop;
@@ -200,12 +207,15 @@ fn is_unary_not<'tcx>(e: &'tcx Expr<'_>) -> (bool, rustc_span::Span) {
200207 ( false , e. span )
201208}
202209
203- fn one_side_is_unary_not < ' tcx > ( left_side : & ' tcx Expr < ' _ > , right_side : & ' tcx Expr < ' _ > ) -> ( bool , rustc_span :: Span , rustc_span :: Span ) {
210+ fn one_side_is_unary_not < ' tcx > ( left_side : & ' tcx Expr < ' _ > , right_side : & ' tcx Expr < ' _ > ) -> ExpressionInfoWithSpan {
204211 let left = is_unary_not ( left_side) ;
205212 let right = is_unary_not ( right_side) ;
206213
207- let retval = left. 0 ^ right. 0 ;
208- ( retval, left. 1 , right. 1 )
214+ ExpressionInfoWithSpan {
215+ one_side_is_unary_not : left. 0 ^ right. 0 ,
216+ left_span : left. 1 ,
217+ right_span : right. 1 ,
218+ }
209219}
210220
211221fn check_comparison < ' a , ' tcx > (
@@ -224,20 +234,20 @@ fn check_comparison<'a, 'tcx>(
224234 if l_ty. is_bool ( ) && r_ty. is_bool ( ) {
225235 let mut applicability = Applicability :: MachineApplicable ;
226236
227- if let BinOpKind :: Eq = op. node
228- {
229- let xxx = one_side_is_unary_not ( & left_side, & right_side) ;
230- if xxx. 0
231- {
237+ if let BinOpKind :: Eq = op. node {
238+ let expression_info = one_side_is_unary_not ( & left_side, & right_side) ;
239+ if expression_info. one_side_is_unary_not {
232240 span_lint_and_sugg (
233241 cx,
234242 BOOL_COMPARISON ,
235243 e. span ,
236244 "This comparison might be written more concisely" ,
237245 "try simplifying it as shown" ,
238- format ! ( "{} != {}" ,
239- snippet_with_applicability( cx, xxx. 1 , ".." , & mut applicability) ,
240- snippet_with_applicability( cx, xxx. 2 , ".." , & mut applicability) ) ,
246+ format ! (
247+ "{} != {}" ,
248+ snippet_with_applicability( cx, expression_info. left_span, ".." , & mut applicability) ,
249+ snippet_with_applicability( cx, expression_info. right_span, ".." , & mut applicability)
250+ ) ,
241251 applicability,
242252 )
243253 }
0 commit comments