Skip to content

Commit 99c1df3

Browse files
bors[bot]tobz1000
andauthored
Merge #4880
4880: "fill match arms" assist: Match on bind patterns r=flodiebold a=tobz1000 This prevents duplication of match arms where the pre-existing arm is a bind pattern. Co-authored-by: Toby Dimmick <tobydimmick@pm.me>
2 parents 017331a + 41f5471 commit 99c1df3

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

crates/ra_assists/src/handlers/fill_match_arms.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,20 @@ fn is_variant_missing(existing_arms: &mut Vec<MatchArm>, var: &Pat) -> bool {
136136
}
137137

138138
fn does_pat_match_variant(pat: &Pat, var: &Pat) -> bool {
139-
let pat_head = pat.syntax().first_child().map(|node| node.text());
140-
let var_head = var.syntax().first_child().map(|node| node.text());
139+
let first_node_text = |pat: &Pat| pat.syntax().first_child().map(|node| node.text());
140+
141+
let pat_head = match pat {
142+
Pat::BindPat(bind_pat) => {
143+
if let Some(p) = bind_pat.pat() {
144+
first_node_text(&p)
145+
} else {
146+
return false;
147+
}
148+
}
149+
pat => first_node_text(pat),
150+
};
151+
152+
let var_head = first_node_text(var);
141153

142154
pat_head == var_head
143155
}
@@ -350,6 +362,40 @@ mod tests {
350362
);
351363
}
352364

365+
#[test]
366+
fn partial_fill_bind_pat() {
367+
check_assist(
368+
fill_match_arms,
369+
r#"
370+
enum A {
371+
As,
372+
Bs,
373+
Cs(Option<i32>),
374+
}
375+
fn main() {
376+
match A::As<|> {
377+
A::As(_) => {}
378+
a @ A::Bs(_) => {}
379+
}
380+
}
381+
"#,
382+
r#"
383+
enum A {
384+
As,
385+
Bs,
386+
Cs(Option<i32>),
387+
}
388+
fn main() {
389+
match A::As {
390+
A::As(_) => {}
391+
a @ A::Bs(_) => {}
392+
$0A::Cs(_) => {}
393+
}
394+
}
395+
"#,
396+
);
397+
}
398+
353399
#[test]
354400
fn fill_match_arms_empty_body() {
355401
check_assist(

0 commit comments

Comments
 (0)