11use 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;
33use clippy_utils:: is_lint_allowed;
44use clippy_utils:: source:: snippet_opt;
55use clippy_utils:: ty:: implements_trait;
@@ -25,7 +25,7 @@ declare_clippy_lint! {
2525 /// let x = &12;
2626 /// let addr_x = &x as *const _ as usize;
2727 /// 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.
2929 /// assert_ne!(addr_x, addr_y);
3030 /// ```
3131 ///
@@ -80,51 +80,36 @@ impl LateLintPass<'_> for BorrowDerefRef {
8080 }
8181 }
8282
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+ ) ;
8495
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+ }
112102 }
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- }
127103
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+ ) ;
128113
129114 }
130115 }
0 commit comments