Skip to content

Commit 228b44c

Browse files
committed
fix: Only skip adjustment hints for block, if and match expressions for reborrows
1 parent 9fca0a4 commit 228b44c

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

crates/ide/src/inlay_hints/adjustment.rs

+36-10
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,31 @@ pub(super) fn hints(
3131
return None;
3232
}
3333

34-
// These inherit from the inner expression which would result in duplicate hints
35-
if let ast::Expr::ParenExpr(_)
36-
| ast::Expr::IfExpr(_)
37-
| ast::Expr::BlockExpr(_)
38-
| ast::Expr::MatchExpr(_) = expr
39-
{
34+
// ParenExpr resolve to their contained expressions HIR so they will dupe these hints
35+
if let ast::Expr::ParenExpr(_) = expr {
4036
return None;
4137
}
38+
if let ast::Expr::BlockExpr(b) = expr {
39+
if !b.is_standalone() {
40+
return None;
41+
}
42+
}
4243

4344
let descended = sema.descend_node_into_attributes(expr.clone()).pop();
4445
let desc_expr = descended.as_ref().unwrap_or(expr);
4546
let adjustments = sema.expr_adjustments(desc_expr).filter(|it| !it.is_empty())?;
4647

48+
if let ast::Expr::BlockExpr(_) | ast::Expr::IfExpr(_) | ast::Expr::MatchExpr(_) = desc_expr {
49+
if let [Adjustment { kind: Adjust::Deref(_), source, .. }, Adjustment { kind: Adjust::Borrow(_), source: _, target }] =
50+
&*adjustments
51+
{
52+
// Don't show unnecessary reborrows for these, they will just repeat the inner ones again
53+
if source == target {
54+
return None;
55+
}
56+
}
57+
}
58+
4759
let (postfix, needs_outer_parens, needs_inner_parens) =
4860
mode_and_needs_parens_for_adjustment_hints(expr, config.adjustment_hints_mode);
4961

@@ -67,6 +79,7 @@ pub(super) fn hints(
6779

6880
for Adjustment { source, target, kind } in iter {
6981
if source == target {
82+
cov_mark::hit!(same_type_adjustment);
7083
continue;
7184
}
7285

@@ -251,7 +264,7 @@ mod tests {
251264
check_with_config(
252265
InlayHintsConfig { adjustment_hints: AdjustmentHints::Always, ..DISABLED_CONFIG },
253266
r#"
254-
//- minicore: coerce_unsized, fn
267+
//- minicore: coerce_unsized, fn, eq
255268
fn main() {
256269
let _: u32 = loop {};
257270
//^^^^^^^<never-to-any>
@@ -332,7 +345,7 @@ fn main() {
332345
loop {}
333346
//^^^^^^^<never-to-any>
334347
};
335-
let _: &mut [u32] = match () { () => &mut [] }
348+
let _: &mut [u32] = match () { () => &mut [] };
336349
//^^^^^^^<unsize>
337350
//^^^^^^^&mut $
338351
//^^^^^^^*
@@ -341,6 +354,12 @@ fn main() {
341354
//^^^^^^^^^^<unsize>
342355
//^^^^^^^^^^&mut $
343356
//^^^^^^^^^^*
357+
() == ();
358+
// ^^&
359+
// ^^&
360+
(()) == {()};
361+
// ^^&
362+
// ^^^^&
344363
}
345364
346365
#[derive(Copy, Clone)]
@@ -363,7 +382,7 @@ impl Struct {
363382
..DISABLED_CONFIG
364383
},
365384
r#"
366-
//- minicore: coerce_unsized, fn
385+
//- minicore: coerce_unsized, fn, eq
367386
fn main() {
368387
369388
Struct.consume();
@@ -419,7 +438,7 @@ fn main() {
419438
loop {}
420439
//^^^^^^^.<never-to-any>
421440
};
422-
let _: &mut [u32] = match () { () => &mut [] }
441+
let _: &mut [u32] = match () { () => &mut [] };
423442
//^^^^^^^(
424443
//^^^^^^^)
425444
//^^^^^^^.*
@@ -432,6 +451,12 @@ fn main() {
432451
//^^^^^^^^^^.*
433452
//^^^^^^^^^^.&mut
434453
//^^^^^^^^^^.<unsize>
454+
() == ();
455+
// ^^.&
456+
// ^^.&
457+
(()) == {()};
458+
// ^^.&
459+
// ^^^^.&
435460
}
436461
437462
#[derive(Copy, Clone)]
@@ -499,6 +524,7 @@ fn main() {
499524

500525
#[test]
501526
fn never_to_never_is_never_shown() {
527+
cov_mark::check!(same_type_adjustment);
502528
check_with_config(
503529
InlayHintsConfig { adjustment_hints: AdjustmentHints::Always, ..DISABLED_CONFIG },
504530
r#"

crates/syntax/src/ast/expr_ext.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,15 @@ impl ast::BlockExpr {
356356
Some(it) => it,
357357
None => return true,
358358
};
359-
!matches!(parent.kind(), FN | IF_EXPR | WHILE_EXPR | LOOP_EXPR)
359+
match parent.kind() {
360+
FOR_EXPR | IF_EXPR => parent
361+
.children()
362+
.filter(|it| ast::Expr::can_cast(it.kind()))
363+
.next()
364+
.map_or(true, |it| it == *self.syntax()),
365+
LET_ELSE | FN | WHILE_EXPR | LOOP_EXPR | CONST_BLOCK_PAT => false,
366+
_ => true,
367+
}
360368
}
361369
}
362370

0 commit comments

Comments
 (0)