Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion crates/ide-assists/src/handlers/merge_match_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,14 @@ fn get_arm_types<'db>(
}
}
ast::Pat::IdentPat(ident_pat) => {
let has_type = ctx.sema.type_of_pat(local_pat).is_some();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a pattern without a type? This is weird. Every pattern has a type, even binding patterns. Are you sure this code is treating the other way around, that is binding patterns, correctly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this code is treating the other way around, that is binding patterns, correctly?

There are behavioral differences in patterns and bindings:

pattern type_of_pat type_of_binding_in_pat
None Some(Ty) None
x Some(Ty) Some(Ty)

if let Some(name) = ident_pat.name() {
let pat_type = ctx.sema.type_of_binding_in_pat(ident_pat);
map.insert(name.text().to_string(), pat_type);
let is_local_variable = !has_type || pat_type.is_some();

if is_local_variable {
map.insert(name.text().to_string(), pat_type);
}
}
}
_ => (),
Expand Down Expand Up @@ -212,6 +217,40 @@ fn main() {
);
}

#[test]
fn merge_match_arms_ambiguous_ident_patterns() {
check_assist(
merge_match_arms,
r#"
#[derive(Debug)]
enum X { A, B, C }
use X::*;

fn main() {
let x = A;
let y = match x {
A => { 1i32$0 }
B => { 1i32 }
C => { 2i32 }
}
}
"#,
r#"
#[derive(Debug)]
enum X { A, B, C }
use X::*;

fn main() {
let x = A;
let y = match x {
A | B => { 1i32 },
C => { 2i32 }
}
}
"#,
);
}

#[test]
fn merge_match_arms_multiple_patterns() {
check_assist(
Expand Down