Skip to content

Commit 53599d1

Browse files
committed
Fix incorrectly replacing method calls in "Convert to named struct" assist
1 parent 8d4be82 commit 53599d1

File tree

1 file changed

+41
-17
lines changed

1 file changed

+41
-17
lines changed

crates/ide_assists/src/handlers/convert_tuple_struct_to_named_struct.rs

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use hir::{Adt, ModuleDef};
2-
use ide_db::defs::Definition;
2+
use ide_db::defs::{Definition, NameRefClass};
33
use syntax::{
44
ast::{self, AstNode, GenericParamsOwner, VisibilityOwner},
55
match_ast,
@@ -80,11 +80,9 @@ fn edit_struct_references(
8080
strukt: &ast::Struct,
8181
names: &[ast::Name],
8282
) {
83-
let strukt_def = ctx.sema.to_def(strukt).unwrap();
84-
let usages = Definition::ModuleDef(ModuleDef::Adt(Adt::Struct(strukt_def)))
85-
.usages(&ctx.sema)
86-
.include_self_kw_refs(true)
87-
.all();
83+
let strukt = ctx.sema.to_def(strukt).unwrap();
84+
let strukt_def = Definition::ModuleDef(ModuleDef::Adt(Adt::Struct(strukt)));
85+
let usages = strukt_def.usages(&ctx.sema).include_self_kw_refs(true).all();
8886

8987
for (file_id, refs) in usages {
9088
edit.edit_file(file_id);
@@ -109,16 +107,26 @@ fn edit_struct_references(
109107
.to_string(),
110108
);
111109
},
112-
// for tuple struct creations like: Foo(42)
110+
// for tuple struct creations like Foo(42)
113111
ast::CallExpr(call_expr) => {
114-
let path = call_expr.syntax().descendants().find_map(ast::PathExpr::cast).unwrap();
112+
let path = call_expr.syntax().descendants().find_map(ast::PathExpr::cast).unwrap().path().unwrap();
113+
114+
// this also includes method calls like Foo::new(42), we should skip them
115+
if let Some(Some(name_ref)) = path.segment().map(|s| s.name_ref()) {
116+
match NameRefClass::classify(&ctx.sema, &name_ref) {
117+
Some(NameRefClass::Definition(Definition::SelfType(_))) => {},
118+
Some(NameRefClass::Definition(def)) if def == strukt_def => {},
119+
_ => continue,
120+
};
121+
}
122+
115123
let arg_list =
116124
call_expr.syntax().descendants().find_map(ast::ArgList::cast).unwrap();
117125

118126
edit.replace(
119127
call_expr.syntax().text_range(),
120128
ast::make::record_expr(
121-
path.path().unwrap(),
129+
path,
122130
ast::make::record_expr_field_list(arg_list.args().zip(names).map(
123131
|(expr, name)| {
124132
ast::make::record_expr_field(
@@ -191,8 +199,12 @@ struct Inner;
191199
struct A$0(Inner);
192200
193201
impl A {
194-
fn new() -> A {
195-
A(Inner)
202+
fn new(inner: Inner) -> A {
203+
A(inner)
204+
}
205+
206+
fn new_with_default() -> A {
207+
A::new(Inner)
196208
}
197209
198210
fn into_inner(self) -> Inner {
@@ -204,8 +216,12 @@ struct Inner;
204216
struct A { field1: Inner }
205217
206218
impl A {
207-
fn new() -> A {
208-
A { field1: Inner }
219+
fn new(inner: Inner) -> A {
220+
A { field1: inner }
221+
}
222+
223+
fn new_with_default() -> A {
224+
A::new(Inner)
209225
}
210226
211227
fn into_inner(self) -> Inner {
@@ -224,8 +240,12 @@ struct Inner;
224240
struct A$0(Inner);
225241
226242
impl A {
227-
fn new() -> Self {
228-
Self(Inner)
243+
fn new(inner: Inner) -> Self {
244+
Self(inner)
245+
}
246+
247+
fn new_with_default() -> Self {
248+
Self::new(Inner)
229249
}
230250
231251
fn into_inner(self) -> Inner {
@@ -237,8 +257,12 @@ struct Inner;
237257
struct A { field1: Inner }
238258
239259
impl A {
240-
fn new() -> Self {
241-
Self { field1: Inner }
260+
fn new(inner: Inner) -> Self {
261+
Self { field1: inner }
262+
}
263+
264+
fn new_with_default() -> Self {
265+
Self::new(Inner)
242266
}
243267
244268
fn into_inner(self) -> Inner {

0 commit comments

Comments
 (0)