11use 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 } ;
44use if_chain:: if_chain;
55use rustc_ast:: ast:: LitKind ;
66use rustc_errors:: Applicability ;
77use rustc_hir:: def_id:: DefIdSet ;
88use rustc_hir:: {
99 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 ,
1111} ;
1212use rustc_lint:: { LateContext , LateLintPass } ;
1313use rustc_middle:: ty:: { self , AssocKind , FnSig , Ty } ;
@@ -16,7 +16,6 @@ use rustc_span::{
1616 source_map:: { Span , Spanned , Symbol } ,
1717 symbol:: sym,
1818} ;
19- use std:: borrow:: Cow ;
2019
2120declare_clippy_lint ! {
2221 /// ### What it does
@@ -431,7 +430,7 @@ fn check_len(
431430 & format ! ( "using `{op}is_empty` is clearer and more explicit" ) ,
432431 format ! (
433432 "{op}{}.is_empty()" ,
434- snippet_with_applicability ( cx, receiver. span, "_" , & mut applicability)
433+ snippet_with_context ( cx, receiver. span, span . ctxt ( ) , "_" , & mut applicability) . 0 ,
435434 ) ,
436435 applicability,
437436 ) ;
@@ -444,13 +443,7 @@ fn check_empty_expr(cx: &LateContext<'_>, span: Span, lit1: &Expr<'_>, lit2: &Ex
444443 let mut applicability = Applicability :: MachineApplicable ;
445444
446445 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 ( ) ;
454447
455448 span_lint_and_sugg (
456449 cx,
0 commit comments