Skip to content

Commit 0fa227a

Browse files
committed
Deduplicate references when some of them are in macro expansions
Commit 6a06f6f (Deduplicate reference search results, 2022-11-07) deduplicates references within each definition. Apparently our descend_into_macros() stanza returns one definition for each time a name is used in a macro. Each of those definitions has the same set of references. We return them all, leading to many redundant references. Work around this by deduplicating definitions as well. Perhaps there is a better fix to not produce duplicate definitions in the first place. I discovered this working with the "bitflags" macro from the crate of the same name. Fixes #16357
1 parent 9d8889c commit 0fa227a

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

crates/ide/src/references.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,13 @@ pub(crate) fn find_all_refs(
120120
}
121121
None => {
122122
let search = make_searcher(false);
123-
Some(find_defs(sema, &syntax, position.offset)?.into_iter().map(search).collect())
123+
Some(
124+
find_defs(sema, &syntax, position.offset)?
125+
.into_iter()
126+
.unique()
127+
.map(search)
128+
.collect(),
129+
)
124130
}
125131
}
126132
}
@@ -2139,4 +2145,28 @@ fn test() {
21392145
"#]],
21402146
);
21412147
}
2148+
2149+
#[test]
2150+
fn multiple_references_inside_macro() {
2151+
check(
2152+
r#"
2153+
macro_rules! use_name {
2154+
($name:ident) => {
2155+
let _ = $name;
2156+
let _ = $name;
2157+
let _ = $name;
2158+
};
2159+
}
2160+
fn test() {
2161+
let name = ();
2162+
use_name!(name$0);
2163+
}
2164+
"#,
2165+
expect![[r#"
2166+
name Local FileId(0) 145..149 145..149
2167+
2168+
FileId(0) 170..174 Read
2169+
"#]],
2170+
);
2171+
}
21422172
}

0 commit comments

Comments
 (0)