Skip to content

Commit 7369360

Browse files
committed
Don't ascribe types in pattern completion for patterns twice
1 parent 726b4dd commit 7369360

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

crates/ide_completion/src/context.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub(crate) struct PathCompletionContext {
5858
pub(super) struct PatternContext {
5959
pub(super) refutability: PatternRefutability,
6060
pub(super) is_param: Option<ParamKind>,
61+
pub(super) has_type_ascription: bool,
6162
}
6263

6364
#[derive(Debug)]
@@ -708,15 +709,15 @@ impl<'a> CompletionContext<'a> {
708709
return None;
709710
}
710711
let mut is_param = None;
711-
let refutability = bind_pat
712+
let (refutability, has_type_ascription) = bind_pat
712713
.syntax()
713714
.ancestors()
714715
.skip_while(|it| ast::Pat::can_cast(it.kind()))
715716
.next()
716-
.map_or(PatternRefutability::Irrefutable, |node| {
717-
match_ast! {
717+
.map_or((PatternRefutability::Irrefutable, false), |node| {
718+
let refutability = match_ast! {
718719
match node {
719-
ast::LetStmt(__) => PatternRefutability::Irrefutable,
720+
ast::LetStmt(let_) => return (PatternRefutability::Irrefutable, let_.ty().is_some()),
720721
ast::Param(param) => {
721722
let is_closure_param = param
722723
.syntax()
@@ -729,16 +730,17 @@ impl<'a> CompletionContext<'a> {
729730
} else {
730731
ParamKind::Function
731732
});
732-
PatternRefutability::Irrefutable
733+
return (PatternRefutability::Irrefutable, param.ty().is_some())
733734
},
734735
ast::MatchArm(__) => PatternRefutability::Refutable,
735736
ast::Condition(__) => PatternRefutability::Refutable,
736737
ast::ForExpr(__) => PatternRefutability::Irrefutable,
737738
_ => PatternRefutability::Irrefutable,
738739
}
739-
}
740+
};
741+
(refutability, false)
740742
});
741-
Some(PatternContext { refutability, is_param })
743+
Some(PatternContext { refutability, is_param, has_type_ascription })
742744
}
743745

744746
fn classify_name_ref(

crates/ide_completion/src/render/pattern.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ fn render_pat(
8585

8686
if matches!(
8787
ctx.completion.pattern_ctx,
88-
Some(PatternContext { is_param: Some(ParamKind::Function), .. })
88+
Some(PatternContext {
89+
is_param: Some(ParamKind::Function),
90+
has_type_ascription: false,
91+
..
92+
})
8993
) {
9094
pat.push(':');
9195
pat.push(' ');

crates/ide_completion/src/tests/pattern.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,21 @@ fn foo(a$0) {
163163
ma makro!(…) #[macro_export] macro_rules! makro
164164
"##]],
165165
);
166+
check(
167+
r#"
168+
fn foo(a$0: Tuple) {
169+
}
170+
"#,
171+
expect![[r##"
172+
kw mut
173+
bn Record Record { field$1 }$0
174+
st Record
175+
bn Tuple Tuple($1)$0
176+
st Tuple
177+
st Unit
178+
ma makro!(…) #[macro_export] macro_rules! makro
179+
"##]],
180+
);
166181
}
167182

168183
#[test]

0 commit comments

Comments
 (0)