Skip to content

Commit d4e5c24

Browse files
committed
Auto merge of rust-lang#18292 - roife:fix-issue-17427, r=Veykril
feat: handle self-param outside of methods when renaming close rust-lang#17427
2 parents 23ef13d + 2ee4925 commit d4e5c24

File tree

1 file changed

+41
-12
lines changed
  • src/tools/rust-analyzer/crates/ide/src

1 file changed

+41
-12
lines changed

src/tools/rust-analyzer/crates/ide/src/rename.rs

+41-12
Original file line numberDiff line numberDiff line change
@@ -421,19 +421,28 @@ fn text_edit_from_self_param(self_param: &ast::SelfParam, new_name: &str) -> Opt
421421
None
422422
}
423423

424-
let impl_def = self_param.syntax().ancestors().find_map(ast::Impl::cast)?;
425-
let type_name = target_type_name(&impl_def)?;
426-
427-
let mut replacement_text = String::from(new_name);
428-
replacement_text.push_str(": ");
429-
match (self_param.amp_token(), self_param.mut_token()) {
430-
(Some(_), None) => replacement_text.push('&'),
431-
(Some(_), Some(_)) => replacement_text.push_str("&mut "),
432-
(_, _) => (),
433-
};
434-
replacement_text.push_str(type_name.as_str());
424+
match self_param.syntax().ancestors().find_map(ast::Impl::cast) {
425+
Some(impl_def) => {
426+
let type_name = target_type_name(&impl_def)?;
427+
428+
let mut replacement_text = String::from(new_name);
429+
replacement_text.push_str(": ");
430+
match (self_param.amp_token(), self_param.mut_token()) {
431+
(Some(_), None) => replacement_text.push('&'),
432+
(Some(_), Some(_)) => replacement_text.push_str("&mut "),
433+
(_, _) => (),
434+
};
435+
replacement_text.push_str(type_name.as_str());
435436

436-
Some(TextEdit::replace(self_param.syntax().text_range(), replacement_text))
437+
Some(TextEdit::replace(self_param.syntax().text_range(), replacement_text))
438+
}
439+
None => {
440+
cov_mark::hit!(rename_self_outside_of_methods);
441+
let mut replacement_text = String::from(new_name);
442+
replacement_text.push_str(": _");
443+
Some(TextEdit::replace(self_param.syntax().text_range(), replacement_text))
444+
}
445+
}
437446
}
438447

439448
#[cfg(test)]
@@ -1977,6 +1986,26 @@ impl Foo {
19771986
);
19781987
}
19791988

1989+
#[test]
1990+
fn test_self_outside_of_methods() {
1991+
cov_mark::check!(rename_self_outside_of_methods);
1992+
check(
1993+
"foo",
1994+
r#"
1995+
fn f($0self) -> i32 {
1996+
use self as _;
1997+
self.i
1998+
}
1999+
"#,
2000+
r#"
2001+
fn f(foo: _) -> i32 {
2002+
use self as _;
2003+
foo.i
2004+
}
2005+
"#,
2006+
);
2007+
}
2008+
19802009
#[test]
19812010
fn test_self_in_path_to_parameter() {
19822011
check(

0 commit comments

Comments
 (0)