Skip to content

Commit b30c688

Browse files
committed
fix: extract function panics on more than one usage of variable in macro
1 parent c904df0 commit b30c688

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2060,7 +2060,7 @@ fn fix_param_usages(
20602060
.filter_map(|reference| path_element_of_reference(syntax, reference))
20612061
.map(|expr| tm.make_mut(&expr));
20622062

2063-
usages_for_param.push((param, usages.collect()));
2063+
usages_for_param.push((param, usages.dedup().collect()));
20642064
}
20652065

20662066
let res = tm.make_syntax_mut(syntax);
@@ -6233,4 +6233,42 @@ fn $0fun_name(a: i32, b: i32) {
62336233
cov_mark::check!(extract_function_in_braces_is_not_applicable);
62346234
check_assist_not_applicable(extract_function, r"fn foo(arr: &mut $0[$0i32]) {}");
62356235
}
6236+
6237+
#[test]
6238+
fn issue_20965_panic() {
6239+
check_assist(
6240+
extract_function,
6241+
r#"
6242+
//- minicore: fmt
6243+
#[derive(Debug)]
6244+
struct Foo(&'static str);
6245+
6246+
impl Foo {
6247+
fn text(&self) -> &str { self.0 }
6248+
}
6249+
6250+
fn main() {
6251+
let s = Foo("");
6252+
$0print!("{}{}", s, s);$0
6253+
let _ = s.text() == "";
6254+
}"#,
6255+
r#"
6256+
#[derive(Debug)]
6257+
struct Foo(&'static str);
6258+
6259+
impl Foo {
6260+
fn text(&self) -> &str { self.0 }
6261+
}
6262+
6263+
fn main() {
6264+
let s = Foo("");
6265+
fun_name(&s);
6266+
let _ = s.text() == "";
6267+
}
6268+
6269+
fn $0fun_name(s: &Foo) {
6270+
*print!("{}{}", s, s);
6271+
}"#,
6272+
);
6273+
}
62366274
}

0 commit comments

Comments
 (0)