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.
This commit is contained in:
Paul Daniel Faria 2020-08-09 18:52:19 -04:00
parent 6cde0b1aa0
commit bf9b4578bb
8 changed files with 22 additions and 23 deletions

View file

@ -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)?;

View file

@ -83,9 +83,9 @@ impl Crate {
.collect()
}
pub fn root_module(self, db: &dyn HirDatabase) -> Option<Module> {
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 {

View file

@ -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()),

View file

@ -85,8 +85,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
let node = token.parent();
let definition = match_ast! {
match node {
ast::NameRef(name_ref) => 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,
}
};

View file

@ -130,13 +130,13 @@ fn find_name(
opt_name: Option<ast::Name>,
) -> Option<RangeInfo<Definition>> {
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::<ast::NameRef>(&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))
}

View file

@ -93,19 +93,19 @@ pub enum NameClass {
impl NameClass {
pub fn into_definition(self, db: &dyn HirDatabase) -> Option<Definition> {
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<Definition> {
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<RootDatabase>, 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<Definition> {
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),
})
}
}
}

View file

@ -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);

View file

@ -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);