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
@@ -170,12 +173,39 @@ pub(super) fn hints(
170173 if needs_outer_parens || ( !postfix && needs_inner_parens) {
171174 post. label . append_str ( ")" ) ;
172175 }
173- if !pre. label . parts . is_empty ( ) {
174- acc. push ( pre) ;
176+
177+ let mut pre = pre. label . parts . is_empty ( ) . not ( ) . then_some ( pre) ;
178+ let mut post = post. label . parts . is_empty ( ) . not ( ) . then_some ( post) ;
179+ if pre. is_none ( ) && post. is_none ( ) {
180+ return None ;
175181 }
176- if !post. label . parts . is_empty ( ) {
177- acc. push ( post) ;
182+ let edit = {
183+ let mut b = TextEditBuilder :: default ( ) ;
184+ if let Some ( pre) = & pre {
185+ b. insert (
186+ pre. range . start ( ) ,
187+ pre. label . parts . iter ( ) . map ( |part| & * part. text ) . collect :: < String > ( ) ,
188+ ) ;
189+ }
190+ if let Some ( post) = & post {
191+ b. insert (
192+ post. range . end ( ) ,
193+ post. label . parts . iter ( ) . map ( |part| & * part. text ) . collect :: < String > ( ) ,
194+ ) ;
195+ }
196+ b. finish ( )
197+ } ;
198+ match ( & mut pre, & mut post) {
199+ ( Some ( pre) , Some ( post) ) => {
200+ pre. text_edit = Some ( edit. clone ( ) ) ;
201+ post. text_edit = Some ( edit) ;
202+ }
203+ ( Some ( pre) , None ) => pre. text_edit = Some ( edit) ,
204+ ( None , Some ( post) ) => post. text_edit = Some ( edit) ,
205+ ( None , None ) => ( ) ,
178206 }
207+ acc. extend ( pre) ;
208+ acc. extend ( post) ;
179209 Some ( ( ) )
180210}
181211
0 commit comments