Skip to content

Commit 13ccbb2

Browse files
committed
find_usages limited to actual usages again
1 parent ce8121b commit 13ccbb2

File tree

2 files changed

+84
-14
lines changed

2 files changed

+84
-14
lines changed

crates/ra_ide/src/references/rename.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,76 @@ mod tests {
381381
);
382382
}
383383

384+
#[test]
385+
fn test_field_shorthand_correct_struct() {
386+
test_rename(
387+
r#"
388+
struct Foo {
389+
i<|>: i32,
390+
}
391+
392+
struct Bar {
393+
i: i32,
394+
}
395+
396+
impl Bar {
397+
fn new(i: i32) -> Self {
398+
Self { i }
399+
}
400+
}
401+
"#,
402+
"j",
403+
r#"
404+
struct Foo {
405+
j: i32,
406+
}
407+
408+
struct Bar {
409+
i: i32,
410+
}
411+
412+
impl Bar {
413+
fn new(i: i32) -> Self {
414+
Self { i }
415+
}
416+
}
417+
"#,
418+
);
419+
}
420+
421+
#[test]
422+
fn test_shadow_local_for_struct_shorthand() {
423+
test_rename(
424+
r#"
425+
struct Foo {
426+
i: i32,
427+
}
428+
429+
fn baz(i<|>: i32) -> Self {
430+
let x = Foo { i };
431+
{
432+
let i = 0;
433+
Foo { i }
434+
}
435+
}
436+
"#,
437+
"j",
438+
r#"
439+
struct Foo {
440+
i: i32,
441+
}
442+
443+
fn baz(j: i32) -> Self {
444+
let x = Foo { i: j };
445+
{
446+
let i = 0;
447+
Foo { i }
448+
}
449+
}
450+
"#,
451+
);
452+
}
453+
384454
#[test]
385455
fn test_rename_mod() {
386456
let (analysis, position) = analysis_and_position(

crates/ra_ide_db/src/search.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -256,21 +256,21 @@ impl Definition {
256256
access: reference_access(&def, &name_ref),
257257
});
258258
}
259-
Some(NameRefClass::FieldShorthand { local, field: _ }) => {
260-
let kind = match self {
261-
Definition::StructField(_) => {
262-
ReferenceKind::StructFieldShorthandForField
263-
}
264-
Definition::Local(_) => ReferenceKind::StructFieldShorthandForLocal,
265-
_ => continue,
259+
Some(NameRefClass::FieldShorthand { local, field }) => {
260+
match self {
261+
Definition::StructField(_) if &field == self => refs.push(Reference {
262+
file_range: sema.original_range(name_ref.syntax()),
263+
kind: ReferenceKind::StructFieldShorthandForField,
264+
access: reference_access(&field, &name_ref),
265+
}),
266+
Definition::Local(l) if &local == l => refs.push(Reference {
267+
file_range: sema.original_range(name_ref.syntax()),
268+
kind: ReferenceKind::StructFieldShorthandForLocal,
269+
access: reference_access(&Definition::Local(local), &name_ref),
270+
}),
271+
272+
_ => {} // not a usage
266273
};
267-
268-
let file_range = sema.original_range(name_ref.syntax());
269-
refs.push(Reference {
270-
file_range,
271-
kind,
272-
access: reference_access(&Definition::Local(local), &name_ref),
273-
});
274274
}
275275
_ => {} // not a usage
276276
}

0 commit comments

Comments
 (0)