From bf9b4578bbe038501ef7c337e22b448de477f61c Mon Sep 17 00:00:00 2001 From: Paul Daniel Faria Date: Sun, 9 Aug 2020 18:52:19 -0400 Subject: [PATCH] Remove Option<...> from result of Crate::root_module There doesn't seem to be any need for it, and removing it simplies several paths of code that depend on it. --- crates/ra_assists/src/utils.rs | 2 +- crates/ra_hir/src/code_model.rs | 4 ++-- crates/ra_ide/src/goto_definition.rs | 5 +++-- crates/ra_ide/src/hover.rs | 4 ++-- crates/ra_ide/src/references.rs | 4 ++-- crates/ra_ide_db/src/defs.rs | 22 +++++++++---------- .../rust-analyzer/src/cli/analysis_stats.rs | 2 +- crates/rust-analyzer/src/cli/diagnostics.rs | 2 +- 8 files changed, 22 insertions(+), 23 deletions(-) diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index 54d5678d14..0de6fdf3fb 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs @@ -257,7 +257,7 @@ pub use prelude::*; .find(|dep| &dep.name.to_string() == std_crate)? .krate; - let mut module = std_crate.root_module(db)?; + let mut module = std_crate.root_module(db); for segment in path { module = module.children(db).find_map(|child| { let name = child.name(db)?; diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 27cdabea03..44456e49e2 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -83,9 +83,9 @@ impl Crate { .collect() } - pub fn root_module(self, db: &dyn HirDatabase) -> Option { + pub fn root_module(self, db: &dyn HirDatabase) -> Module { let module_id = db.crate_def_map(self.id).root; - Some(Module::new(self, module_id)) + Module::new(self, module_id) } pub fn root_file(self, db: &dyn HirDatabase) -> FileId { diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index b44b6fe22f..45389fd23f 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs @@ -40,7 +40,7 @@ pub(crate) fn goto_definition( reference_definition(&sema, &name_ref).to_vec() }, ast::Name(name) => { - let def = classify_name(&sema, &name)?.definition(sema.db)?; + let def = classify_name(&sema, &name)?.definition(sema.db); let nav = def.try_to_nav(sema.db)?; vec![nav] }, @@ -82,7 +82,8 @@ pub(crate) fn reference_definition( name_ref: &ast::NameRef, ) -> ReferenceResult { let name_kind = classify_name_ref(sema, name_ref); - if let Some(def) = name_kind.and_then(|def| def.definition(sema.db)) { + if let Some(def) = name_kind { + let def = def.definition(sema.db); return match def.try_to_nav(sema.db) { Some(nav) => ReferenceResult::Exact(nav), None => ReferenceResult::Approximate(Vec::new()), diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index a632ea6a20..f6e493817b 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -85,8 +85,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option classify_name_ref(&sema, &name_ref).and_then(|d| d.definition(sema.db)), - ast::Name(name) => classify_name(&sema, &name).and_then(|d| d.definition(sema.db)), + ast::NameRef(name_ref) => classify_name_ref(&sema, &name_ref).map(|d| d.definition(sema.db)), + ast::Name(name) => classify_name(&sema, &name).map(|d| d.definition(sema.db)), _ => None, } }; diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs index 9dd228b9c3..453985de3e 100644 --- a/crates/ra_ide/src/references.rs +++ b/crates/ra_ide/src/references.rs @@ -130,13 +130,13 @@ fn find_name( opt_name: Option, ) -> Option> { if let Some(name) = opt_name { - let def = classify_name(sema, &name)?.definition(sema.db)?; + let def = classify_name(sema, &name)?.definition(sema.db); let range = name.syntax().text_range(); return Some(RangeInfo::new(range, def)); } let name_ref = sema.find_node_at_offset_with_descend::(&syntax, position.offset)?; - let def = classify_name_ref(sema, &name_ref)?.definition(sema.db)?; + let def = classify_name_ref(sema, &name_ref)?.definition(sema.db); let range = name_ref.syntax().text_range(); Some(RangeInfo::new(range, def)) } diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs index e2a4f2983c..9bb95277d0 100644 --- a/crates/ra_ide_db/src/defs.rs +++ b/crates/ra_ide_db/src/defs.rs @@ -93,19 +93,19 @@ pub enum NameClass { impl NameClass { pub fn into_definition(self, db: &dyn HirDatabase) -> Option { Some(match self { - NameClass::ExternCrate(krate) => Definition::ModuleDef(krate.root_module(db)?.into()), + NameClass::ExternCrate(krate) => Definition::ModuleDef(krate.root_module(db).into()), NameClass::Definition(it) => it, NameClass::ConstReference(_) => return None, NameClass::FieldShorthand { local, field: _ } => Definition::Local(local), }) } - pub fn definition(self, db: &dyn HirDatabase) -> Option { - Some(match self { - NameClass::ExternCrate(krate) => Definition::ModuleDef(krate.root_module(db)?.into()), + pub fn definition(self, db: &dyn HirDatabase) -> Definition { + match self { + NameClass::ExternCrate(krate) => Definition::ModuleDef(krate.root_module(db).into()), NameClass::Definition(it) | NameClass::ConstReference(it) => it, NameClass::FieldShorthand { local: _, field } => field, - }) + } } } @@ -148,7 +148,7 @@ pub fn classify_name(sema: &Semantics, name: &ast::Name) -> Option }) .and_then(|name_ref| classify_name_ref(sema, &name_ref))?; - Some(NameClass::Definition(name_ref_class.definition(sema.db)?)) + Some(NameClass::Definition(name_ref_class.definition(sema.db))) } else { let extern_crate = it.syntax().parent().and_then(ast::ExternCrate::cast)?; let resolved = sema.resolve_extern_crate(&extern_crate)?; @@ -234,14 +234,12 @@ pub enum NameRefClass { } impl NameRefClass { - pub fn definition(self, db: &dyn HirDatabase) -> Option { - Some(match self { - NameRefClass::ExternCrate(krate) => { - Definition::ModuleDef(krate.root_module(db)?.into()) - } + pub fn definition(self, db: &dyn HirDatabase) -> Definition { + match self { + NameRefClass::ExternCrate(krate) => Definition::ModuleDef(krate.root_module(db).into()), NameRefClass::Definition(def) => def, NameRefClass::FieldShorthand { local, field: _ } => Definition::Local(local), - }) + } } } diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index 721d41a58f..0d386841e5 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -72,7 +72,7 @@ impl AnalysisStatsCmd { shuffle(&mut rng, &mut krates); } for krate in krates { - let module = krate.root_module(db).expect("crate without root module"); + let module = krate.root_module(db); let file_id = module.definition_source(db).file_id; let file_id = file_id.original_file(db); let source_root = db.file_source_root(file_id); diff --git a/crates/rust-analyzer/src/cli/diagnostics.rs b/crates/rust-analyzer/src/cli/diagnostics.rs index 4ac8c8772e..f17fc5dfe9 100644 --- a/crates/rust-analyzer/src/cli/diagnostics.rs +++ b/crates/rust-analyzer/src/cli/diagnostics.rs @@ -28,7 +28,7 @@ pub fn diagnostics( let mut work = Vec::new(); let krates = Crate::all(db); for krate in krates { - let module = krate.root_module(db).expect("crate without root module"); + let module = krate.root_module(db); let file_id = module.definition_source(db).file_id; let file_id = file_id.original_file(db); let source_root = db.file_source_root(file_id);