diff --git a/crates/els/completion.rs b/crates/els/completion.rs index 185a0ee15..47c465110 100644 --- a/crates/els/completion.rs +++ b/crates/els/completion.rs @@ -442,13 +442,7 @@ impl CompletionCache { } pub fn get(&self, namespace: &str) -> Option>> { - if self.cache.borrow().get(namespace).is_some() { - Some(RwLockReadGuard::map(self.cache.borrow(), |cache| { - cache.get(namespace).unwrap() - })) - } else { - None - } + RwLockReadGuard::try_map(self.cache.borrow(), |cache| cache.get(namespace)).ok() } pub fn insert(&self, namespace: String, items: Vec) { diff --git a/crates/els/server.rs b/crates/els/server.rs index 1f6dc123d..0429b6df7 100644 --- a/crates/els/server.rs +++ b/crates/els/server.rs @@ -1151,10 +1151,7 @@ impl Server { pub fn get_hir(&self, uri: &NormalizedUrl) -> Option> { let path = uri.to_file_path().ok()?; let ent = self.shared.get_module(&path)?; - ent.hir.as_ref()?; - Some(MappedRwLockReadGuard::map(ent, |ent| { - ent.hir.as_ref().unwrap() - })) + MappedRwLockReadGuard::try_map(ent, |ent| ent.hir.as_ref()).ok() } pub fn steal_entry(&self, uri: &NormalizedUrl) -> Option { @@ -1170,10 +1167,7 @@ impl Server { pub fn get_ast(&self, uri: &NormalizedUrl) -> Option> { let path = uri.to_file_path().ok()?; let ent = self.shared.get_module(&path)?; - ent.ast.as_ref()?; - Some(MappedRwLockReadGuard::map(ent, |ent| { - ent.ast.as_ref().unwrap() - })) + MappedRwLockReadGuard::try_map(ent, |ent| ent.ast.as_ref()).ok() } pub fn get_warns(&self, uri: &NormalizedUrl) -> Option> { diff --git a/crates/erg_compiler/module/cache.rs b/crates/erg_compiler/module/cache.rs index 14bce069c..76fa220ff 100644 --- a/crates/erg_compiler/module/cache.rs +++ b/crates/erg_compiler/module/cache.rs @@ -260,13 +260,7 @@ impl SharedModuleCache { where NormalizedPathBuf: Borrow, { - if self.0.borrow().get(path).is_some() { - Some(RwLockReadGuard::map(self.0.borrow(), |cache| { - cache.get(path).unwrap() - })) - } else { - None - } + RwLockReadGuard::try_map(self.0.borrow(), |cache| cache.get(path)).ok() } pub fn get_mut( @@ -276,13 +270,7 @@ impl SharedModuleCache { where NormalizedPathBuf: Borrow, { - if self.0.borrow().get(path).is_some() { - Some(RwLockWriteGuard::map(self.0.borrow_mut(), |cache| { - cache.get_mut(path).unwrap() - })) - } else { - None - } + RwLockWriteGuard::try_map(self.0.borrow_mut(), |cache| cache.get_mut(path)).ok() } pub fn ref_ctx( @@ -292,13 +280,10 @@ impl SharedModuleCache { where NormalizedPathBuf: Borrow, { - if self.0.borrow().get(path).is_some() { - Some(RwLockReadGuard::map(self.0.borrow(), |cache| { - &cache.get(path).unwrap().module - })) - } else { - None - } + RwLockReadGuard::try_map(self.0.borrow(), |cache| { + cache.get(path).map(|ent| &ent.module) + }) + .ok() } /// FIXME: see the comment in this function diff --git a/crates/erg_compiler/module/graph.rs b/crates/erg_compiler/module/graph.rs index 214314f5a..65412e739 100644 --- a/crates/erg_compiler/module/graph.rs +++ b/crates/erg_compiler/module/graph.rs @@ -225,13 +225,7 @@ impl SharedModuleGraph { &self, path: &NormalizedPathBuf, ) -> Option>> { - if self.0.borrow().get_node(path).is_some() { - Some(RwLockReadGuard::map(self.0.borrow(), |graph| { - graph.get_node(path).unwrap() - })) - } else { - None - } + RwLockReadGuard::try_map(self.0.borrow(), |graph| graph.get_node(path)).ok() } pub fn depends_on(&self, path: &NormalizedPathBuf, target: &NormalizedPathBuf) -> bool { diff --git a/crates/erg_compiler/module/impls.rs b/crates/erg_compiler/module/impls.rs index 37c32a616..09b940b43 100644 --- a/crates/erg_compiler/module/impls.rs +++ b/crates/erg_compiler/module/impls.rs @@ -119,13 +119,7 @@ impl SharedTraitImpls { where Str: Borrow, { - if self.0.borrow().get(path).is_some() { - Some(RwLockReadGuard::map(self.0.borrow(), |tis| { - tis.get(path).unwrap() - })) - } else { - None - } + RwLockReadGuard::try_map(self.0.borrow(), |tis| tis.get(path)).ok() } pub fn get_mut( @@ -135,13 +129,7 @@ impl SharedTraitImpls { where Str: Borrow, { - if self.0.borrow().get(path).is_some() { - Some(RwLockWriteGuard::map(self.0.borrow_mut(), |tis| { - tis.get_mut(path).unwrap() - })) - } else { - None - } + RwLockWriteGuard::try_map(self.0.borrow_mut(), |tis| tis.get_mut(path)).ok() } pub fn register(&self, name: Str, impls: Set) { diff --git a/crates/erg_compiler/module/index.rs b/crates/erg_compiler/module/index.rs index 4e52ff5e5..115155e94 100644 --- a/crates/erg_compiler/module/index.rs +++ b/crates/erg_compiler/module/index.rs @@ -171,13 +171,7 @@ impl SharedModuleIndex { &self, referee: &AbsLocation, ) -> Option> { - if self.0.borrow().get_refs(referee).is_some() { - Some(RwLockReadGuard::map(self.0.borrow(), |index| { - index.get_refs(referee).unwrap() - })) - } else { - None - } + RwLockReadGuard::try_map(self.0.borrow(), |index| index.get_refs(referee)).ok() } pub fn members(&self) -> Members { diff --git a/crates/erg_compiler/ty/free.rs b/crates/erg_compiler/ty/free.rs index f93b3ff34..ecf7b110f 100644 --- a/crates/erg_compiler/ty/free.rs +++ b/crates/erg_compiler/ty/free.rs @@ -1124,41 +1124,29 @@ impl Free { #[track_caller] pub fn get_linked_ref(&self) -> Option> { - if !self.is_linked() { - None - } else { - let mapped = Ref::map(self.borrow(), |f| match f { - FreeKind::Linked(t) | FreeKind::UndoableLinked { t, .. } => t, - FreeKind::Unbound { .. } | FreeKind::NamedUnbound { .. } => unreachable!(), - }); - Some(mapped) - } + Ref::filter_map(self.borrow(), |f| match f { + FreeKind::Linked(t) | FreeKind::UndoableLinked { t, .. } => Some(t), + FreeKind::Unbound { .. } | FreeKind::NamedUnbound { .. } => None, + }) + .ok() } #[track_caller] pub fn get_linked_refmut(&self) -> Option> { - if !self.is_linked() { - None - } else { - let mapped = RefMut::map(self.borrow_mut(), |f| match f { - FreeKind::Linked(t) | FreeKind::UndoableLinked { t, .. } => t, - FreeKind::Unbound { .. } | FreeKind::NamedUnbound { .. } => unreachable!(), - }); - Some(mapped) - } + RefMut::filter_map(self.borrow_mut(), |f| match f { + FreeKind::Linked(t) | FreeKind::UndoableLinked { t, .. } => Some(t), + FreeKind::Unbound { .. } | FreeKind::NamedUnbound { .. } => None, + }) + .ok() } #[track_caller] pub fn get_previous(&self) -> Option>>> { - if !self.is_undoable_linked() { - None - } else { - let mapped = Ref::map(self.borrow(), |f| match f { - FreeKind::UndoableLinked { previous, .. } => previous, - _ => unreachable!(), - }); - Some(mapped) - } + Ref::filter_map(self.borrow(), |f| match f { + FreeKind::UndoableLinked { previous, .. } => Some(previous), + _ => None, + }) + .ok() } pub fn detach(&self) -> Self { diff --git a/crates/erg_compiler/ty/mod.rs b/crates/erg_compiler/ty/mod.rs index a474ab7aa..eb85caa0b 100644 --- a/crates/erg_compiler/ty/mod.rs +++ b/crates/erg_compiler/ty/mod.rs @@ -3990,10 +3990,8 @@ impl Type { pub fn tyvar_mut_return_t(&mut self) -> Option> { match self { - Self::FreeVar(fv) if fv.get_linked()?.return_t().is_some() => { - Some(RefMut::map(fv.borrow_mut(), |fk| { - fk.linked_mut().unwrap().mut_return_t().unwrap() - })) + Self::FreeVar(fv) => { + RefMut::filter_map(fv.borrow_mut(), |fv| fv.linked_mut()?.mut_return_t()).ok() } _ => None, }