11use crate :: reference:: DEREF_ADDROF ;
22use clippy_utils:: diagnostics:: span_lint_and_then;
33use clippy_utils:: source:: snippet_opt;
4+ use clippy_utils:: source:: snippet_with_context;
45use clippy_utils:: ty:: implements_trait;
56use clippy_utils:: { get_parent_expr, is_lint_allowed} ;
7+ use rustc_errors:: Applicability ;
68use rustc_hir:: { ExprKind , UnOp } ;
79use rustc_lint:: { LateContext , LateLintPass } ;
810use rustc_middle:: mir:: Mutability ;
@@ -43,6 +45,7 @@ declare_clippy_lint! {
4345 ///
4446 /// fn foo(_: &str){ }
4547 /// ```
48+ #[ clippy:: version = "1.59.0" ]
4649 pub BORROW_DEREF_REF ,
4750 complexity,
4851 "deref on an immutable reference returns the same type as itself"
@@ -55,19 +58,26 @@ impl LateLintPass<'_> for BorrowDerefRef {
5558 if_chain ! {
5659 if !e. span. from_expansion( ) ;
5760 if let ExprKind :: AddrOf ( _, Mutability :: Not , addrof_target) = e. kind;
61+ if !addrof_target. span. from_expansion( ) ;
5862 if let ExprKind :: Unary ( UnOp :: Deref , deref_target) = addrof_target. kind;
5963 if !deref_target. span. from_expansion( ) ;
64+ let mut app = Applicability :: MachineApplicable ;
65+ if let ( _, false ) =snippet_with_context( cx, deref_target. span , e. span. ctxt( ) , ".." , & mut app) ;
66+ if let ( _, false ) =snippet_with_context( cx, deref_target. span , addrof_target. span. ctxt( ) , ".." , & mut app) ;
67+ if let ( _, false ) =snippet_with_context( cx, addrof_target. span , e. span. ctxt( ) , ".." , & mut app) ;
6068 if !matches!( deref_target. kind, ExprKind :: Unary ( UnOp :: Deref , ..) ) ;
6169 let ref_ty = cx. typeck_results( ) . expr_ty( deref_target) ;
6270 if let ty:: Ref ( _, inner_ty, Mutability :: Not ) = ref_ty. kind( ) ;
6371 then{
6472
6573 if let Some ( parent_expr) = get_parent_expr( cx, e) {
66- let map = cx. tcx. hir( ) ;
67- let span = map. span( parent_expr. hir_id) ;
68- if span. from_expansion( ) {
69- return ;
70- }
74+
75+ // let map = cx.tcx.hir();
76+ // let span = map.span(parent_expr.hir_id);
77+ // if span.from_expansion() {
78+ // return;
79+ // }
80+
7181 if matches!( deref_target. kind, ExprKind :: Path ( ..) | ExprKind :: Field ( ..) ) {
7282 if matches!( parent_expr. kind, ExprKind :: AddrOf ( _, Mutability :: Mut , _) ) {
7383 return ;
0 commit comments