1
1
use clippy_utils:: diagnostics:: { span_lint, span_lint_and_sugg, span_lint_and_then} ;
2
- use clippy_utils:: source:: snippet_with_applicability ;
3
- use clippy_utils:: { get_item_name, get_parent_as_impl, is_lint_allowed, peel_ref_operators} ;
2
+ use clippy_utils:: source:: snippet_with_context ;
3
+ use clippy_utils:: { get_item_name, get_parent_as_impl, is_lint_allowed, peel_ref_operators, sugg :: Sugg } ;
4
4
use if_chain:: if_chain;
5
5
use rustc_ast:: ast:: LitKind ;
6
6
use rustc_errors:: Applicability ;
7
7
use rustc_hir:: def_id:: DefIdSet ;
8
8
use rustc_hir:: {
9
9
def_id:: DefId , AssocItemKind , BinOpKind , Expr , ExprKind , FnRetTy , ImplItem , ImplItemKind , ImplicitSelfKind , Item ,
10
- ItemKind , Mutability , Node , TraitItemRef , TyKind , UnOp ,
10
+ ItemKind , Mutability , Node , TraitItemRef , TyKind ,
11
11
} ;
12
12
use rustc_lint:: { LateContext , LateLintPass } ;
13
13
use rustc_middle:: ty:: { self , AssocKind , FnSig , Ty } ;
@@ -16,7 +16,6 @@ use rustc_span::{
16
16
source_map:: { Span , Spanned , Symbol } ,
17
17
symbol:: sym,
18
18
} ;
19
- use std:: borrow:: Cow ;
20
19
21
20
declare_clippy_lint ! {
22
21
/// ### What it does
@@ -431,7 +430,7 @@ fn check_len(
431
430
& format ! ( "using `{op}is_empty` is clearer and more explicit" ) ,
432
431
format ! (
433
432
"{op}{}.is_empty()" ,
434
- snippet_with_applicability ( cx, receiver. span, "_" , & mut applicability)
433
+ snippet_with_context ( cx, receiver. span, span . ctxt ( ) , "_" , & mut applicability) . 0 ,
435
434
) ,
436
435
applicability,
437
436
) ;
@@ -444,13 +443,7 @@ fn check_empty_expr(cx: &LateContext<'_>, span: Span, lit1: &Expr<'_>, lit2: &Ex
444
443
let mut applicability = Applicability :: MachineApplicable ;
445
444
446
445
let lit1 = peel_ref_operators ( cx, lit1) ;
447
- let mut lit_str = snippet_with_applicability ( cx, lit1. span , "_" , & mut applicability) ;
448
-
449
- // Wrap the expression in parentheses if it's a deref expression. Otherwise operator precedence will
450
- // cause the code to dereference boolean(won't compile).
451
- if let ExprKind :: Unary ( UnOp :: Deref , _) = lit1. kind {
452
- lit_str = Cow :: from ( format ! ( "({lit_str})" ) ) ;
453
- }
446
+ let lit_str = Sugg :: hir_with_context ( cx, lit1, span. ctxt ( ) , "_" , & mut applicability) . maybe_par ( ) ;
454
447
455
448
span_lint_and_sugg (
456
449
cx,
0 commit comments