33//! let _: u32 = /* <never-to-any> */ loop {};
44//! let _: &u32 = /* &* */ &mut 0;
55//! ```
6+ use std:: ops:: Not ;
7+
68use either:: Either ;
79use hir:: {
810 Adjust , Adjustment , AutoBorrow , HirDisplay , Mutability , OverloadedDeref , PointerCast , Safety ,
@@ -15,6 +17,7 @@ use syntax::{
1517 ast:: { self , make, AstNode } ,
1618 ted,
1719} ;
20+ use text_edit:: TextEditBuilder ;
1821
1922use crate :: {
2023 AdjustmentHints , AdjustmentHintsMode , InlayHint , InlayHintLabel , InlayHintLabelPart ,
@@ -51,13 +54,13 @@ pub(super) fn hints(
5154 let adjustments = sema. expr_adjustments ( desc_expr) . filter ( |it| !it. is_empty ( ) ) ?;
5255
5356 if let ast:: Expr :: BlockExpr ( _) | ast:: Expr :: IfExpr ( _) | ast:: Expr :: MatchExpr ( _) = desc_expr {
54- if let [ Adjustment { kind : Adjust :: Deref ( _ ) , source , .. } , Adjustment { kind : Adjust :: Borrow ( _ ) , source : _ , target } ] =
55- & * adjustments
56- {
57- // Don't show unnecessary reborrows for these, they will just repeat the inner ones again
58- if source == target {
59- return None ;
60- }
57+ // Don't show unnecessary reborrows for these, they will just repeat the inner ones again
58+ if matches ! (
59+ & * adjustments ,
60+ [ Adjustment { kind : Adjust :: Deref ( _ ) , source , .. } , Adjustment { kind : Adjust :: Borrow ( _ ) , target , .. } ]
61+ if source == target
62+ ) {
63+ return None ;
6164 }
6265 }
6366
@@ -101,6 +104,7 @@ pub(super) fn hints(
101104 } ;
102105 let iter: & mut dyn Iterator < Item = _ > = iter. as_mut ( ) . either ( |it| it as _ , |it| it as _ ) ;
103106
107+ let mut allow_edit = !postfix;
104108 for Adjustment { source, target, kind } in iter {
105109 if source == target {
106110 cov_mark:: hit!( same_type_adjustment) ;
@@ -110,6 +114,7 @@ pub(super) fn hints(
110114 // FIXME: Add some nicer tooltips to each of these
111115 let ( text, coercion) = match kind {
112116 Adjust :: NeverToAny if config. adjustment_hints == AdjustmentHints :: Always => {
117+ allow_edit = false ;
113118 ( "<never-to-any>" , "never to any" )
114119 }
115120 Adjust :: Deref ( None ) => ( "*" , "dereference" ) ,
@@ -130,6 +135,7 @@ pub(super) fn hints(
130135 // some of these could be represented via `as` casts, but that's not too nice and
131136 // handling everything as a prefix expr makes the `(` and `)` insertion easier
132137 Adjust :: Pointer ( cast) if config. adjustment_hints == AdjustmentHints :: Always => {
138+ allow_edit = false ;
133139 match cast {
134140 PointerCast :: ReifyFnPointer => {
135141 ( "<fn-item-to-fn-pointer>" , "fn item to fn pointer" )
@@ -170,12 +176,41 @@ pub(super) fn hints(
170176 if needs_outer_parens || ( !postfix && needs_inner_parens) {
171177 post. label . append_str ( ")" ) ;
172178 }
173- if !pre. label . parts . is_empty ( ) {
174- acc. push ( pre) ;
179+
180+ let mut pre = pre. label . parts . is_empty ( ) . not ( ) . then_some ( pre) ;
181+ let mut post = post. label . parts . is_empty ( ) . not ( ) . then_some ( post) ;
182+ if pre. is_none ( ) && post. is_none ( ) {
183+ return None ;
175184 }
176- if !post. label . parts . is_empty ( ) {
177- acc. push ( post) ;
185+ if allow_edit {
186+ let edit = {
187+ let mut b = TextEditBuilder :: default ( ) ;
188+ if let Some ( pre) = & pre {
189+ b. insert (
190+ pre. range . start ( ) ,
191+ pre. label . parts . iter ( ) . map ( |part| & * part. text ) . collect :: < String > ( ) ,
192+ ) ;
193+ }
194+ if let Some ( post) = & post {
195+ b. insert (
196+ post. range . end ( ) ,
197+ post. label . parts . iter ( ) . map ( |part| & * part. text ) . collect :: < String > ( ) ,
198+ ) ;
199+ }
200+ b. finish ( )
201+ } ;
202+ match ( & mut pre, & mut post) {
203+ ( Some ( pre) , Some ( post) ) => {
204+ pre. text_edit = Some ( edit. clone ( ) ) ;
205+ post. text_edit = Some ( edit) ;
206+ }
207+ ( Some ( pre) , None ) => pre. text_edit = Some ( edit) ,
208+ ( None , Some ( post) ) => post. text_edit = Some ( edit) ,
209+ ( None , None ) => ( ) ,
210+ }
178211 }
212+ acc. extend ( pre) ;
213+ acc. extend ( post) ;
179214 Some ( ( ) )
180215}
181216
0 commit comments