Skip to content

Commit

Permalink
Remove suspicious unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Aug 2, 2023
1 parent bcff166 commit 6e2c3f6
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 13 deletions.
9 changes: 3 additions & 6 deletions crates/hir-def/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ pub struct ExternCrateDeclData {
pub name: Name,
pub alias: Option<ImportAlias>,
pub visibility: RawVisibility,
pub crate_id: CrateId,
pub crate_id: Option<CrateId>,
}

impl ExternCrateDeclData {
Expand All @@ -482,15 +482,12 @@ impl ExternCrateDeclData {

let name = extern_crate.name.clone();
let crate_id = if name == hir_expand::name![self] {
loc.container.krate()
Some(loc.container.krate())
} else {
db.crate_def_map(loc.container.krate())
.extern_prelude()
.find(|&(prelude_name, ..)| *prelude_name == name)
// FIXME: Suspicious unwrap
.unwrap()
.1
.krate()
.map(|(_, root)| root.krate())
};

Arc::new(Self {
Expand Down
2 changes: 1 addition & 1 deletion crates/hir/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl HasAttrs for ExternCrateDecl {
db.attrs_with_owner(def)
}
fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
let crate_docs = self.resolved_crate(db).root_module().attrs(db).docs().map(String::from);
let crate_docs = self.resolved_crate(db)?.root_module().attrs(db).docs().map(String::from);
let def = AttrDefId::ExternCrateId(self.into());
let decl_docs = db.attrs(def).docs().map(String::from);
match (decl_docs, crate_docs) {
Expand Down
4 changes: 2 additions & 2 deletions crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2138,8 +2138,8 @@ impl ExternCrateDecl {
self.id.module(db.upcast()).into()
}

pub fn resolved_crate(self, db: &dyn HirDatabase) -> Crate {
db.extern_crate_decl_data(self.id).crate_id.into()
pub fn resolved_crate(self, db: &dyn HirDatabase) -> Option<Crate> {
db.extern_crate_decl_data(self.id).crate_id.map(Into::into)
}

pub fn name(self, db: &dyn HirDatabase) -> Name {
Expand Down
2 changes: 1 addition & 1 deletion crates/ide-db/src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ impl NameRefClass {
},
ast::ExternCrate(extern_crate_ast) => {
let extern_crate = sema.to_def(&extern_crate_ast)?;
let krate = extern_crate.resolved_crate(sema.db);
let krate = extern_crate.resolved_crate(sema.db)?;
Some(if extern_crate_ast.rename().is_some() {
NameRefClass::Definition(Definition::Module(krate.root_module()))
} else {
Expand Down
7 changes: 4 additions & 3 deletions crates/ide/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ pub(crate) fn goto_definition(
.into_iter()
.flat_map(|def| {
if let Definition::ExternCrateDecl(crate_def) = def {
return vec![crate_def
return crate_def
.resolved_crate(db)
.root_module()
.to_nav(sema.db)];
.map(|it| it.root_module().to_nav(sema.db))
.into_iter()
.collect();
}
try_filter_trait_item_definition(sema, &def)
.unwrap_or_else(|| def_to_nav(sema.db, def))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@
</style>
<pre><code><span class="keyword">extern</span> <span class="keyword">crate</span> <span class="module crate_root default_library library">std</span><span class="semicolon">;</span>
<span class="keyword">extern</span> <span class="keyword">crate</span> <span class="module crate_root default_library library">alloc</span> <span class="keyword">as</span> <span class="module crate_root declaration">abc</span><span class="semicolon">;</span>
<span class="keyword">extern</span> <span class="keyword">crate</span> <span class="unresolved_reference">unresolved</span> <span class="keyword">as</span> <span class="module crate_root declaration">definitely_unresolved</span><span class="semicolon">;</span>
</code></pre>
1 change: 1 addition & 0 deletions crates/ide/src/syntax_highlighting/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,7 @@ fn test_extern_crate() {
//- /main.rs crate:main deps:std,alloc
extern crate std;
extern crate alloc as abc;
extern crate unresolved as definitely_unresolved;
//- /std/lib.rs crate:std
pub struct S;
//- /alloc/lib.rs crate:alloc
Expand Down

0 comments on commit 6e2c3f6

Please sign in to comment.