diff --git a/src/lang/defs.rs b/src/lang/defs.rs index dfb90f40..405e6533 100644 --- a/src/lang/defs.rs +++ b/src/lang/defs.rs @@ -150,12 +150,20 @@ impl SymbolDef { pub fn search_scope(&self, db: &AnalysisDatabase) -> SearchScope { match &self { Self::Variable(var) => { - match db.first_ancestor_of_kind(var.syntax_node(db), SyntaxKind::FunctionWithBody) { - Some(owning_function) => SearchScope::file_span( + if let Some(owning_function) = + iter::successors(Some(var.syntax_node(db)), SyntaxNode::parent).find(|node| { + matches!( + node.kind(db.upcast()), + SyntaxKind::FunctionWithBody | SyntaxKind::TraitItemFunction + ) + }) + { + SearchScope::file_span( owning_function.stable_ptr().file_id(db.upcast()), owning_function.span(db.upcast()), - ), - None => SearchScope::file(var.definition_stable_ptr.file_id(db.upcast())), + ) + } else { + SearchScope::file(var.definition_stable_ptr.file_id(db.upcast())) } } diff --git a/tests/e2e/find_references/vars.rs b/tests/e2e/find_references/vars.rs index bcd0d7a9..8e4d442a 100644 --- a/tests/e2e/find_references/vars.rs +++ b/tests/e2e/find_references/vars.rs @@ -166,3 +166,28 @@ fn param_captured_by_closure() { } ") } + +#[test] +fn var_in_trait_function_default_body() { + test_transform!(find_references, r#" + trait Foo { + fn foo() { + let foobar = 42; + let x = foobar + 1; + } + } + fn bar() { + let foobar = 42; + } + "#, @r" + trait Foo { + fn foo() { + let foobar = 42; + let x = foobar + 1; + } + } + fn bar() { + let foobar = 42; + } + ") +}