mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-25 04:23:25 +00:00
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:
parent
6cde0b1aa0
commit
bf9b4578bb
8 changed files with 22 additions and 23 deletions
|
@ -257,7 +257,7 @@ pub use prelude::*;
|
||||||
.find(|dep| &dep.name.to_string() == std_crate)?
|
.find(|dep| &dep.name.to_string() == std_crate)?
|
||||||
.krate;
|
.krate;
|
||||||
|
|
||||||
let mut module = std_crate.root_module(db)?;
|
let mut module = std_crate.root_module(db);
|
||||||
for segment in path {
|
for segment in path {
|
||||||
module = module.children(db).find_map(|child| {
|
module = module.children(db).find_map(|child| {
|
||||||
let name = child.name(db)?;
|
let name = child.name(db)?;
|
||||||
|
|
|
@ -83,9 +83,9 @@ impl Crate {
|
||||||
.collect()
|
.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;
|
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 {
|
pub fn root_file(self, db: &dyn HirDatabase) -> FileId {
|
||||||
|
|
|
@ -40,7 +40,7 @@ pub(crate) fn goto_definition(
|
||||||
reference_definition(&sema, &name_ref).to_vec()
|
reference_definition(&sema, &name_ref).to_vec()
|
||||||
},
|
},
|
||||||
ast::Name(name) => {
|
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)?;
|
let nav = def.try_to_nav(sema.db)?;
|
||||||
vec![nav]
|
vec![nav]
|
||||||
},
|
},
|
||||||
|
@ -82,7 +82,8 @@ pub(crate) fn reference_definition(
|
||||||
name_ref: &ast::NameRef,
|
name_ref: &ast::NameRef,
|
||||||
) -> ReferenceResult {
|
) -> ReferenceResult {
|
||||||
let name_kind = classify_name_ref(sema, name_ref);
|
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) {
|
return match def.try_to_nav(sema.db) {
|
||||||
Some(nav) => ReferenceResult::Exact(nav),
|
Some(nav) => ReferenceResult::Exact(nav),
|
||||||
None => ReferenceResult::Approximate(Vec::new()),
|
None => ReferenceResult::Approximate(Vec::new()),
|
||||||
|
|
|
@ -85,8 +85,8 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
|
||||||
let node = token.parent();
|
let node = token.parent();
|
||||||
let definition = match_ast! {
|
let definition = match_ast! {
|
||||||
match node {
|
match node {
|
||||||
ast::NameRef(name_ref) => classify_name_ref(&sema, &name_ref).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).and_then(|d| d.definition(sema.db)),
|
ast::Name(name) => classify_name(&sema, &name).map(|d| d.definition(sema.db)),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -130,13 +130,13 @@ fn find_name(
|
||||||
opt_name: Option<ast::Name>,
|
opt_name: Option<ast::Name>,
|
||||||
) -> Option<RangeInfo<Definition>> {
|
) -> Option<RangeInfo<Definition>> {
|
||||||
if let Some(name) = opt_name {
|
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();
|
let range = name.syntax().text_range();
|
||||||
return Some(RangeInfo::new(range, def));
|
return Some(RangeInfo::new(range, def));
|
||||||
}
|
}
|
||||||
let name_ref =
|
let name_ref =
|
||||||
sema.find_node_at_offset_with_descend::<ast::NameRef>(&syntax, position.offset)?;
|
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();
|
let range = name_ref.syntax().text_range();
|
||||||
Some(RangeInfo::new(range, def))
|
Some(RangeInfo::new(range, def))
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,19 +93,19 @@ pub enum NameClass {
|
||||||
impl NameClass {
|
impl NameClass {
|
||||||
pub fn into_definition(self, db: &dyn HirDatabase) -> Option<Definition> {
|
pub fn into_definition(self, db: &dyn HirDatabase) -> Option<Definition> {
|
||||||
Some(match self {
|
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::Definition(it) => it,
|
||||||
NameClass::ConstReference(_) => return None,
|
NameClass::ConstReference(_) => return None,
|
||||||
NameClass::FieldShorthand { local, field: _ } => Definition::Local(local),
|
NameClass::FieldShorthand { local, field: _ } => Definition::Local(local),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn definition(self, db: &dyn HirDatabase) -> Option<Definition> {
|
pub fn definition(self, db: &dyn HirDatabase) -> Definition {
|
||||||
Some(match self {
|
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) | NameClass::ConstReference(it) => it,
|
NameClass::Definition(it) | NameClass::ConstReference(it) => it,
|
||||||
NameClass::FieldShorthand { local: _, field } => field,
|
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))?;
|
.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 {
|
} else {
|
||||||
let extern_crate = it.syntax().parent().and_then(ast::ExternCrate::cast)?;
|
let extern_crate = it.syntax().parent().and_then(ast::ExternCrate::cast)?;
|
||||||
let resolved = sema.resolve_extern_crate(&extern_crate)?;
|
let resolved = sema.resolve_extern_crate(&extern_crate)?;
|
||||||
|
@ -234,14 +234,12 @@ pub enum NameRefClass {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NameRefClass {
|
impl NameRefClass {
|
||||||
pub fn definition(self, db: &dyn HirDatabase) -> Option<Definition> {
|
pub fn definition(self, db: &dyn HirDatabase) -> Definition {
|
||||||
Some(match self {
|
match self {
|
||||||
NameRefClass::ExternCrate(krate) => {
|
NameRefClass::ExternCrate(krate) => Definition::ModuleDef(krate.root_module(db).into()),
|
||||||
Definition::ModuleDef(krate.root_module(db)?.into())
|
|
||||||
}
|
|
||||||
NameRefClass::Definition(def) => def,
|
NameRefClass::Definition(def) => def,
|
||||||
NameRefClass::FieldShorthand { local, field: _ } => Definition::Local(local),
|
NameRefClass::FieldShorthand { local, field: _ } => Definition::Local(local),
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -72,7 +72,7 @@ impl AnalysisStatsCmd {
|
||||||
shuffle(&mut rng, &mut krates);
|
shuffle(&mut rng, &mut krates);
|
||||||
}
|
}
|
||||||
for krate in 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 = module.definition_source(db).file_id;
|
||||||
let file_id = file_id.original_file(db);
|
let file_id = file_id.original_file(db);
|
||||||
let source_root = db.file_source_root(file_id);
|
let source_root = db.file_source_root(file_id);
|
||||||
|
|
|
@ -28,7 +28,7 @@ pub fn diagnostics(
|
||||||
let mut work = Vec::new();
|
let mut work = Vec::new();
|
||||||
let krates = Crate::all(db);
|
let krates = Crate::all(db);
|
||||||
for krate in 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 = module.definition_source(db).file_id;
|
||||||
let file_id = file_id.original_file(db);
|
let file_id = file_id.original_file(db);
|
||||||
let source_root = db.file_source_root(file_id);
|
let source_root = db.file_source_root(file_id);
|
||||||
|
|
Loading…
Reference in a new issue