1
1
use crate :: reference:: DEREF_ADDROF ;
2
2
use clippy_utils:: diagnostics:: span_lint_and_then;
3
3
use clippy_utils:: source:: snippet_opt;
4
+ use clippy_utils:: source:: snippet_with_context;
4
5
use clippy_utils:: ty:: implements_trait;
5
6
use clippy_utils:: { get_parent_expr, is_lint_allowed} ;
7
+ use rustc_errors:: Applicability ;
6
8
use rustc_hir:: { ExprKind , UnOp } ;
7
9
use rustc_lint:: { LateContext , LateLintPass } ;
8
10
use rustc_middle:: mir:: Mutability ;
@@ -43,6 +45,7 @@ declare_clippy_lint! {
43
45
///
44
46
/// fn foo(_: &str){ }
45
47
/// ```
48
+ #[ clippy:: version = "1.59.0" ]
46
49
pub BORROW_DEREF_REF ,
47
50
complexity,
48
51
"deref on an immutable reference returns the same type as itself"
@@ -55,19 +58,26 @@ impl LateLintPass<'_> for BorrowDerefRef {
55
58
if_chain ! {
56
59
if !e. span. from_expansion( ) ;
57
60
if let ExprKind :: AddrOf ( _, Mutability :: Not , addrof_target) = e. kind;
61
+ if !addrof_target. span. from_expansion( ) ;
58
62
if let ExprKind :: Unary ( UnOp :: Deref , deref_target) = addrof_target. kind;
59
63
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) ;
60
68
if !matches!( deref_target. kind, ExprKind :: Unary ( UnOp :: Deref , ..) ) ;
61
69
let ref_ty = cx. typeck_results( ) . expr_ty( deref_target) ;
62
70
if let ty:: Ref ( _, inner_ty, Mutability :: Not ) = ref_ty. kind( ) ;
63
71
then{
64
72
65
73
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
+
71
81
if matches!( deref_target. kind, ExprKind :: Path ( ..) | ExprKind :: Field ( ..) ) {
72
82
if matches!( parent_expr. kind, ExprKind :: AddrOf ( _, Mutability :: Mut , _) ) {
73
83
return ;
0 commit comments