3
3
//! let _: u32 = /* <never-to-any> */ loop {};
4
4
//! let _: &u32 = /* &* */ &mut 0;
5
5
//! ```
6
+ use std:: ops:: Not ;
7
+
6
8
use either:: Either ;
7
9
use hir:: {
8
10
Adjust , Adjustment , AutoBorrow , HirDisplay , Mutability , OverloadedDeref , PointerCast , Safety ,
@@ -15,6 +17,7 @@ use syntax::{
15
17
ast:: { self , make, AstNode } ,
16
18
ted,
17
19
} ;
20
+ use text_edit:: TextEditBuilder ;
18
21
19
22
use crate :: {
20
23
AdjustmentHints , AdjustmentHintsMode , InlayHint , InlayHintLabel , InlayHintLabelPart ,
@@ -51,13 +54,13 @@ pub(super) fn hints(
51
54
let adjustments = sema. expr_adjustments ( desc_expr) . filter ( |it| !it. is_empty ( ) ) ?;
52
55
53
56
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 ;
61
64
}
62
65
}
63
66
@@ -170,12 +173,39 @@ pub(super) fn hints(
170
173
if needs_outer_parens || ( !postfix && needs_inner_parens) {
171
174
post. label . append_str ( ")" ) ;
172
175
}
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 ;
175
181
}
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 ) => ( ) ,
178
206
}
207
+ acc. extend ( pre) ;
208
+ acc. extend ( post) ;
179
209
Some ( ( ) )
180
210
}
181
211
0 commit comments