Skip to content

Commit 6b65781

Browse files
Merge pull request #21053 from dfireBird/fix_panic_20965
fix: extract function panics on more than one usage of variable in macro
2 parents 640af3b + 9fe16a6 commit 6b65781

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

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

Lines changed: 61 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.unique().collect()));
20642064
}
20652065

20662066
let res = tm.make_syntax_mut(syntax);
@@ -6233,4 +6233,64 @@ 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+
}
6274+
6275+
#[test]
6276+
fn parameter_is_added_used_in_eq_expression_in_macro() {
6277+
check_assist(
6278+
extract_function,
6279+
r#"
6280+
//- minicore: fmt
6281+
fn foo() {
6282+
let v = 123;
6283+
$0print!("{v:?}{}", v == 123);$0
6284+
}"#,
6285+
r#"
6286+
fn foo() {
6287+
let v = 123;
6288+
fun_name(v);
6289+
}
6290+
6291+
fn $0fun_name(v: i32) {
6292+
print!("{v:?}{}", v == 123);
6293+
}"#,
6294+
);
6295+
}
62366296
}

crates/parser/src/grammar/expressions/atom.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ fn builtin_expr(p: &mut Parser<'_>) -> Option<CompletedMarker> {
283283
if p.eat(T![,]) {
284284
while !p.at(EOF) && !p.at(T![')']) {
285285
let m = p.start();
286-
if p.at(IDENT) && p.nth_at(1, T![=]) {
286+
if p.at(IDENT) && p.nth_at(1, T![=]) && !p.nth_at(2, T![=]) {
287287
name(p);
288288
p.bump(T![=]);
289289
}

0 commit comments

Comments
 (0)