1
1
use crate :: reference:: DEREF_ADDROF ;
2
- use clippy_utils:: diagnostics:: { span_lint_and_help , span_lint_and_then} ;
2
+ use clippy_utils:: diagnostics:: span_lint_and_then;
3
3
use clippy_utils:: is_lint_allowed;
4
4
use clippy_utils:: source:: snippet_opt;
5
5
use clippy_utils:: ty:: implements_trait;
@@ -25,7 +25,7 @@ declare_clippy_lint! {
25
25
/// let x = &12;
26
26
/// let addr_x = &x as *const _ as usize;
27
27
/// let addr_y = &&*x as *const _ as usize; // assert ok now, and lint triggerd.
28
- /// // But if we apply it, it will assert fail.
28
+ /// // But if we fix it, assert will fail.
29
29
/// assert_ne!(addr_x, addr_y);
30
30
/// ```
31
31
///
@@ -80,51 +80,36 @@ impl LateLintPass<'_> for BorrowDerefRef {
80
80
}
81
81
}
82
82
83
- let mut give_2_help = true ;
83
+ span_lint_and_then(
84
+ cx,
85
+ BORROW_DEREF_REF ,
86
+ e. span,
87
+ "deref on an immutable reference" ,
88
+ |diag| {
89
+ diag. help(
90
+ & format!(
91
+ "consider using `{}` if you would like to reborrow" ,
92
+ & snippet_opt( cx, deref_expr. span) . unwrap( ) ,
93
+ )
94
+ ) ;
84
95
85
- // has deref trait -> give 2 help
86
- // doesn't have deref trait -> give 1 help
87
- if let Some ( deref_trait_id) = cx. tcx. lang_items( ) . deref_trait( ) {
88
- if !implements_trait( cx, inner_ty, deref_trait_id, & [ ] ) {
89
- give_2_help = false ;
90
- }
91
- }
92
-
93
- if give_2_help {
94
- span_lint_and_then(
95
- cx,
96
- BORROW_DEREF_REF ,
97
- e. span,
98
- "deref on an immutable reference" ,
99
- |diag| {
100
- diag. help(
101
- & format!(
102
- "consider using `{}` if you would like to deref" ,
103
- "&**" . to_owned( ) + & snippet_opt( cx, deref_expr. span) . unwrap( ) ,
104
- )
105
- ) ;
106
- diag. help(
107
- & format!(
108
- "consider using `{}` if you would like to reborrow" ,
109
- & snippet_opt( cx, deref_expr. span) . unwrap( ) ,
110
- )
111
- ) ;
96
+ // has deref trait -> give 2 help
97
+ // doesn't have deref trait -> give 1 help
98
+ if let Some ( deref_trait_id) = cx. tcx. lang_items( ) . deref_trait( ) {
99
+ if !implements_trait( cx, inner_ty, deref_trait_id, & [ ] ) {
100
+ return ;
101
+ }
112
102
}
113
- ) ;
114
- } else {
115
- span_lint_and_help(
116
- cx,
117
- BORROW_DEREF_REF ,
118
- e. span,
119
- "deref on an immutable reference" ,
120
- None ,
121
- & format!(
122
- "consider using `{}` if you would like to reborrow" ,
123
- & snippet_opt( cx, deref_expr. span) . unwrap( ) ,
124
- )
125
- ) ;
126
- }
127
103
104
+ diag. help(
105
+ & format!(
106
+ "consider using `{}` if you would like to deref" ,
107
+ "&**" . to_owned( ) + & snippet_opt( cx, deref_expr. span) . unwrap( ) ,
108
+ )
109
+ ) ;
110
+
111
+ }
112
+ ) ;
128
113
129
114
}
130
115
}
0 commit comments