Skip to content

Commit 92630ee

Browse files
Merge pull request #21278 from A4-Tacks/mut-ref-type-match
Fix complete reference for `&mut ty` -> `&ty`
2 parents 76cd4d6 + d13d63c commit 92630ee

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

crates/ide-completion/src/render.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,14 @@ fn compute_type_match(
604604
return None;
605605
}
606606

607+
// &mut ty -> &ty
608+
if completion_ty.is_mutable_reference()
609+
&& let Some(expected_type) = expected_type.remove_ref()
610+
&& let Some(completion_ty) = completion_ty.remove_ref()
611+
{
612+
return match_types(ctx, &expected_type, &completion_ty);
613+
}
614+
607615
match_types(ctx, expected_type, completion_ty)
608616
}
609617

@@ -622,6 +630,7 @@ fn compute_ref_match(
622630
return None;
623631
}
624632
if let Some(expected_without_ref) = &expected_without_ref
633+
&& completion_without_ref.is_none()
625634
&& completion_ty.autoderef(ctx.db).any(|ty| ty == *expected_without_ref)
626635
{
627636
cov_mark::hit!(suggest_ref);
@@ -2049,6 +2058,17 @@ fn go(world: &WorldSnapshot) { go(w$0) }
20492058
);
20502059
}
20512060

2061+
#[test]
2062+
fn prioritize_mutable_ref_as_immutable_ref_match() {
2063+
check_relevance(
2064+
r#"fn foo(r: &mut i32) -> &i32 { $0 }"#,
2065+
expect![[r#"
2066+
lc r &mut i32 [type+local]
2067+
fn foo(…) fn(&mut i32) -> &i32 [type]
2068+
"#]],
2069+
);
2070+
}
2071+
20522072
#[test]
20532073
fn too_many_arguments() {
20542074
cov_mark::check!(too_many_arguments);

0 commit comments

Comments
 (0)