3
3
//! This lint is **warn** by default
4
4
5
5
use 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} ;
7
7
use if_chain:: if_chain;
8
8
use rustc_ast:: ast:: LitKind ;
9
9
use rustc_errors:: Applicability ;
10
10
use rustc_hir:: { BinOpKind , Block , Expr , ExprKind , StmtKind , UnOp } ;
11
11
use rustc_lint:: { LateContext , LateLintPass } ;
12
12
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
13
13
use rustc_span:: source_map:: Spanned ;
14
+ use rustc_span:: Span ;
14
15
15
16
declare_clippy_lint ! {
16
17
/// **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 {
189
190
}
190
191
}
191
192
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 ) {
193
200
if_chain ! {
194
201
if let ExprKind :: Unary ( unop, operand) = e. kind;
195
202
if let UnOp :: UnNot = unop;
@@ -200,12 +207,15 @@ fn is_unary_not<'tcx>(e: &'tcx Expr<'_>) -> (bool, rustc_span::Span) {
200
207
( false , e. span )
201
208
}
202
209
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 {
204
211
let left = is_unary_not ( left_side) ;
205
212
let right = is_unary_not ( right_side) ;
206
213
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
+ }
209
219
}
210
220
211
221
fn check_comparison < ' a , ' tcx > (
@@ -224,20 +234,20 @@ fn check_comparison<'a, 'tcx>(
224
234
if l_ty. is_bool ( ) && r_ty. is_bool ( ) {
225
235
let mut applicability = Applicability :: MachineApplicable ;
226
236
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 {
232
240
span_lint_and_sugg (
233
241
cx,
234
242
BOOL_COMPARISON ,
235
243
e. span ,
236
244
"This comparison might be written more concisely" ,
237
245
"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
+ ) ,
241
251
applicability,
242
252
)
243
253
}
0 commit comments