Skip to content

Commit 7fff6d9

Browse files
committed
Fix not applicable on ambigious ident pat for merge_match_arms
Example --- ```rust enum X { A, B, C, } use X::*; fn main() { match A { $0A => todo!(), B => todo!(), C => todo!(), } } ``` **Before this PR** Assist not applicable **After this PR** ```rust enum X { A, B, C, } use X::*; fn main() { match A { A | B => todo!(), C => todo!(), } } ```
1 parent 6a1246b commit 7fff6d9

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

crates/ide-assists/src/handlers/merge_match_arms.rs

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,14 @@ fn get_arm_types<'db>(
160160
}
161161
}
162162
ast::Pat::IdentPat(ident_pat) => {
163+
let has_type = ctx.sema.type_of_pat(local_pat).is_some();
163164
if let Some(name) = ident_pat.name() {
164165
let pat_type = ctx.sema.type_of_binding_in_pat(ident_pat);
165-
map.insert(name.text().to_string(), pat_type);
166+
let is_local_variable = !has_type || pat_type.is_some();
167+
168+
if is_local_variable {
169+
map.insert(name.text().to_string(), pat_type);
170+
}
166171
}
167172
}
168173
_ => (),
@@ -212,6 +217,40 @@ fn main() {
212217
);
213218
}
214219

220+
#[test]
221+
fn merge_match_arms_ambigious_ident_patterns() {
222+
check_assist(
223+
merge_match_arms,
224+
r#"
225+
#[derive(Debug)]
226+
enum X { A, B, C }
227+
use X::*;
228+
229+
fn main() {
230+
let x = A;
231+
let y = match x {
232+
A => { 1i32$0 }
233+
B => { 1i32 }
234+
C => { 2i32 }
235+
}
236+
}
237+
"#,
238+
r#"
239+
#[derive(Debug)]
240+
enum X { A, B, C }
241+
use X::*;
242+
243+
fn main() {
244+
let x = A;
245+
let y = match x {
246+
A | B => { 1i32 },
247+
C => { 2i32 }
248+
}
249+
}
250+
"#,
251+
);
252+
}
253+
215254
#[test]
216255
fn merge_match_arms_multiple_patterns() {
217256
check_assist(

0 commit comments

Comments
 (0)