From 9e8f4d31a40eca605a047ed95a98aac43a3e327f Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Tue, 23 Feb 2021 23:31:07 +0100 Subject: Pickup ConstReference patterns in FindUsages --- crates/ide/src/references.rs | 25 +++++++++++++++++++++++++ crates/ide_db/src/search.rs | 15 +++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs index 55f95ebae..5d0449e56 100644 --- a/crates/ide/src/references.rs +++ b/crates/ide/src/references.rs @@ -1224,4 +1224,29 @@ fn test() {} "#]], ); } + + #[test] + fn test_const_in_pattern() { + check( + r#" +const A$0: i32 = 42; + +fn main() { + match A { + A => (), + _ => (), + } + if let A = A {} +} +"#, + expect![[r#" + A Const FileId(0) 0..18 6..7 + + FileId(0) 42..43 + FileId(0) 54..55 + FileId(0) 97..98 + FileId(0) 101..102 + "#]], + ); + } } diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index ba8bea002..ddcfbd3f3 100644 --- a/crates/ide_db/src/search.rs +++ b/crates/ide_db/src/search.rs @@ -416,10 +416,11 @@ impl<'a> FindUsages<'a> { sink: &mut dyn FnMut(FileId, FileReference) -> bool, ) -> bool { match NameClass::classify(self.sema, name) { - Some(NameClass::PatFieldShorthand { local_def: _, field_ref }) => { - if !matches!(self.def, Definition::Field(_) if &field_ref == self.def) { - return false; - } + Some(NameClass::PatFieldShorthand { local_def: _, field_ref }) + if matches!( + self.def, Definition::Field(_) if &field_ref == self.def + ) => + { let FileRange { file_id, range } = self.sema.original_range(name.syntax()); let reference = FileReference { range, @@ -429,6 +430,12 @@ impl<'a> FindUsages<'a> { }; sink(file_id, reference) } + Some(NameClass::ConstReference(def)) if *self.def == def => { + let FileRange { file_id, range } = self.sema.original_range(name.syntax()); + let reference = + FileReference { range, name: ast::NameLike::Name(name.clone()), access: None }; + sink(file_id, reference) + } _ => false, // not a usage } } -- cgit v1.2.3