mirror of
https://github.com/erg-lang/erg.git
synced 2025-10-03 05:54:33 +00:00
chore: eliminate unwrap
s
This commit is contained in:
parent
f5a21cac8a
commit
d9e4dbe716
16 changed files with 65 additions and 26 deletions
|
@ -208,7 +208,10 @@ macro_rules! impl_sendable {
|
|||
) -> Result<(), mpsc::SendError<WorkerMessage<$Params>>> {
|
||||
self.channels
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.ok_or_else(|| {
|
||||
erg_common::lsp_log!("channels are closed");
|
||||
mpsc::SendError(WorkerMessage::Kill)
|
||||
})?
|
||||
.$receiver
|
||||
.send($crate::channels::WorkerMessage::Request(id, params))
|
||||
}
|
||||
|
|
|
@ -416,12 +416,12 @@ impl CompletionCache {
|
|||
}
|
||||
|
||||
pub fn get(&self, namespace: &str) -> Option<MappedRwLockReadGuard<Vec<CompletionItem>>> {
|
||||
if self.cache.borrow().get(namespace).is_none() {
|
||||
None
|
||||
} else {
|
||||
if self.cache.borrow().get(namespace).is_some() {
|
||||
Some(RwLockReadGuard::map(self.cache.borrow(), |cache| {
|
||||
cache.get(namespace).unwrap()
|
||||
}))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -73,7 +73,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
|||
)));
|
||||
}
|
||||
}
|
||||
} else if let Expr::Accessor(acc) = def.body.block.last().unwrap() {
|
||||
} else if let Some(Expr::Accessor(acc)) = def.body.block.last() {
|
||||
let vi = acc.var_info();
|
||||
match (&vi.def_loc.module, util::loc_to_range(vi.def_loc.loc)) {
|
||||
(Some(path), Some(range)) => {
|
||||
|
|
|
@ -160,7 +160,10 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
|||
}
|
||||
};
|
||||
let ast = self.build_ast(&uri).ok();
|
||||
let ctx = checker.pop_context().unwrap();
|
||||
let Some(ctx) = checker.pop_context() else {
|
||||
_log!(self, "context not found");
|
||||
return Ok(());
|
||||
};
|
||||
if mode == "declare" {
|
||||
self.shared
|
||||
.py_mod_cache
|
||||
|
|
|
@ -318,7 +318,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
|||
) -> ELSResult<InlayHint> {
|
||||
self.send_log(format!("inlay hint resolve request: {hint:?}"))?;
|
||||
if let Some(data) = &hint.data {
|
||||
let Ok(uri) = data.as_str().unwrap().parse::<NormalizedUrl>() else {
|
||||
let Some(uri) = data.as_str().and_then(|s| s.parse::<NormalizedUrl>().ok()) else {
|
||||
return Ok(hint);
|
||||
};
|
||||
if let Some(module) = self.get_mod_ctx(&uri) {
|
||||
|
|
|
@ -851,7 +851,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
|||
HEALTH_CHECKER_ID => {
|
||||
self.channels
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
.ok_or("channels not found")?
|
||||
.health_check
|
||||
.send(WorkerMessage::Request(0, ()))?;
|
||||
}
|
||||
|
@ -982,7 +982,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
|
|||
let mut ctxs = vec![];
|
||||
if let Ok(dir) = uri
|
||||
.to_file_path()
|
||||
.and_then(|p| p.parent().unwrap().read_dir().map_err(|_| ()))
|
||||
.and_then(|p| p.parent().ok_or(())?.read_dir().map_err(|_| ()))
|
||||
{
|
||||
for neighbor in dir {
|
||||
let Ok(neighbor) = neighbor else {
|
||||
|
|
|
@ -51,7 +51,7 @@ where
|
|||
let most_similar_name =
|
||||
candidates.min_by_key(|v| levenshtein(v.borrow(), name, limit).unwrap_or(usize::MAX))?;
|
||||
let dist = levenshtein(most_similar_name.borrow(), name, limit);
|
||||
if dist.is_none() || dist.unwrap() >= limit {
|
||||
if dist.map_or(true, |d| d >= limit) {
|
||||
None
|
||||
} else {
|
||||
Some(most_similar_name)
|
||||
|
@ -69,7 +69,7 @@ where
|
|||
let most_similar_name_and_some = candidates
|
||||
.min_by_key(|(_, v)| levenshtein(v.borrow(), name, limit).unwrap_or(usize::MAX))?;
|
||||
let dist = levenshtein(most_similar_name_and_some.1.borrow(), name, limit);
|
||||
if dist.is_none() || dist.unwrap() >= limit {
|
||||
if dist.map_or(true, |d| d >= limit) {
|
||||
None
|
||||
} else {
|
||||
Some(most_similar_name_and_some)
|
||||
|
|
|
@ -52,7 +52,7 @@ pub fn open_read(filename: &str) -> std::io::Result<String> {
|
|||
|
||||
pub fn read_file(mut f: std::fs::File) -> std::io::Result<String> {
|
||||
let mut s = "".to_string();
|
||||
std::io::Read::read_to_string(&mut f, &mut s).unwrap();
|
||||
std::io::Read::read_to_string(&mut f, &mut s)?;
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
|
@ -183,7 +183,7 @@ pub fn unique_in_place<T: Eq + std::hash::Hash + Clone>(v: &mut Vec<T>) {
|
|||
|
||||
/// at least, this is necessary for Windows and macOS
|
||||
pub fn normalize_path(path: PathBuf) -> PathBuf {
|
||||
let verbatim_replaced = path.to_str().unwrap().replace("\\\\?\\", "");
|
||||
let verbatim_replaced = path.to_string_lossy().replace("\\\\?\\", "");
|
||||
let lower = if !CASE_SENSITIVE {
|
||||
verbatim_replaced.to_lowercase()
|
||||
} else {
|
||||
|
|
|
@ -233,6 +233,8 @@ impl Str {
|
|||
/// assert_eq!(s.split_with(&[".", "::"]), vec!["a", "b", "c"]);
|
||||
/// let s = Str::rc("ああ.いい::うう");
|
||||
/// assert_eq!(s.split_with(&[".", "::"]), vec!["ああ", "いい", "うう"]);
|
||||
/// let s = Str::rc("abc");
|
||||
/// assert_eq!(s.split_with(&[".", "::"]), vec!["abc"]);
|
||||
/// ```
|
||||
pub fn split_with(&self, seps: &[&str]) -> Vec<&str> {
|
||||
let mut result = vec![];
|
||||
|
|
|
@ -521,7 +521,9 @@ impl StyledStrings {
|
|||
/// ```
|
||||
pub fn push_str(&mut self, s: &str) {
|
||||
if self.color_is(Color::Gray) {
|
||||
self.texts.last_mut().unwrap().text.push_str(s);
|
||||
if let Some(ss) = self.texts.last_mut() {
|
||||
ss.text.push_str(s)
|
||||
}
|
||||
} else {
|
||||
self.texts.push(StyledString::new(s, None, None));
|
||||
}
|
||||
|
|
|
@ -336,7 +336,14 @@ impl<A: ASTBuildable> GenericASTLowerer<A> {
|
|||
fn fake_lower_signature(&self, sig: ast::Signature) -> LowerResult<hir::Signature> {
|
||||
match sig {
|
||||
ast::Signature::Var(var) => {
|
||||
let ident = var.ident().unwrap().clone();
|
||||
let Some(ident) = var.ident().cloned() else {
|
||||
return Err(LowerErrors::from(LowerError::declare_error(
|
||||
self.cfg().input.clone(),
|
||||
line!() as usize,
|
||||
var.loc(),
|
||||
self.module.context.caused_by(),
|
||||
)));
|
||||
};
|
||||
let ident = hir::Identifier::bare(ident);
|
||||
let t_spec = if let Some(ts) = var.t_spec {
|
||||
let expr = self.fake_lower_expr(*ts.t_spec_as_expr.clone())?;
|
||||
|
|
|
@ -1191,9 +1191,13 @@ impl_stream!(RecordAttrs, Def);
|
|||
|
||||
impl Locational for RecordAttrs {
|
||||
fn loc(&self) -> Location {
|
||||
if self.is_empty() {
|
||||
Location::Unknown
|
||||
} else {
|
||||
Location::concat(self.0.first().unwrap(), self.0.last().unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Def>> for RecordAttrs {
|
||||
fn from(attrs: Vec<Def>) -> Self {
|
||||
|
@ -2319,8 +2323,8 @@ impl Def {
|
|||
}
|
||||
|
||||
pub fn def_kind(&self) -> DefKind {
|
||||
match self.body.block.first().unwrap() {
|
||||
Expr::Call(call) => match call.obj.show_acc().as_ref().map(|n| &n[..]) {
|
||||
match self.body.block.first() {
|
||||
Some(Expr::Call(call)) => match call.obj.show_acc().as_ref().map(|n| &n[..]) {
|
||||
Some("Class") => DefKind::Class,
|
||||
Some("Inherit") => DefKind::Inherit,
|
||||
Some("Trait") => DefKind::Trait,
|
||||
|
@ -2349,7 +2353,7 @@ impl Def {
|
|||
}
|
||||
|
||||
pub fn get_base(&self) -> Option<&Record> {
|
||||
match self.body.block.first().unwrap() {
|
||||
match self.body.block.first()? {
|
||||
Expr::Call(call) => match call.obj.show_acc().as_ref().map(|n| &n[..]) {
|
||||
Some("Class") | Some("Trait") => {
|
||||
if let Some(Expr::Record(rec)) = call.args.get_left_or_key("Base") {
|
||||
|
|
|
@ -188,7 +188,13 @@ impl ModuleCache {
|
|||
}
|
||||
|
||||
pub fn get_similar_name(&self, name: &str) -> Option<Str> {
|
||||
get_similar_name(self.cache.iter().map(|(v, _)| v.to_str().unwrap()), name).map(Str::rc)
|
||||
get_similar_name(
|
||||
self.cache
|
||||
.iter()
|
||||
.map(|(v, _)| v.to_str().unwrap_or_default()),
|
||||
name,
|
||||
)
|
||||
.map(Str::rc)
|
||||
}
|
||||
|
||||
pub fn rename_path(&mut self, old: &NormalizedPathBuf, new: NormalizedPathBuf) {
|
||||
|
|
|
@ -105,7 +105,7 @@ pub fn py_module(path: TyParam) -> Type {
|
|||
}
|
||||
|
||||
pub fn module_from_path<P: Into<PathBuf>>(path: P) -> Type {
|
||||
let s = ValueObj::Str(Str::rc(path.into().to_str().unwrap()));
|
||||
let s = ValueObj::Str(Str::rc(&path.into().to_string_lossy()));
|
||||
module(TyParam::Value(s))
|
||||
}
|
||||
|
||||
|
|
|
@ -2770,9 +2770,9 @@ impl Type {
|
|||
Self::Refinement(refine) => refine.t.namespace(),
|
||||
Self::Mono(name) | Self::Poly { name, .. } => {
|
||||
let namespaces = name.split_with(&[".", "::"]);
|
||||
if namespaces.len() > 1 {
|
||||
if let Some(last) = namespaces.last() {
|
||||
Str::rc(
|
||||
name.trim_end_matches(namespaces.last().unwrap())
|
||||
name.trim_end_matches(last)
|
||||
.trim_end_matches('.')
|
||||
.trim_end_matches("::"),
|
||||
)
|
||||
|
|
|
@ -1205,9 +1205,13 @@ impl NestedDisplay for ClassAttrs {
|
|||
|
||||
impl Locational for ClassAttrs {
|
||||
fn loc(&self) -> Location {
|
||||
if self.is_empty() {
|
||||
Location::Unknown
|
||||
} else {
|
||||
Location::concat(self.0.first().unwrap(), self.0.last().unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<ClassAttr>> for ClassAttrs {
|
||||
fn from(attrs: Vec<ClassAttr>) -> Self {
|
||||
|
@ -1231,9 +1235,13 @@ impl NestedDisplay for RecordAttrs {
|
|||
|
||||
impl Locational for RecordAttrs {
|
||||
fn loc(&self) -> Location {
|
||||
if self.is_empty() {
|
||||
Location::Unknown
|
||||
} else {
|
||||
Location::concat(self.0.first().unwrap(), self.0.last().unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Vec<Def>> for RecordAttrs {
|
||||
fn from(attrs: Vec<Def>) -> Self {
|
||||
|
@ -6125,9 +6133,13 @@ impl_display_from_nested!(Module);
|
|||
|
||||
impl Locational for Module {
|
||||
fn loc(&self) -> Location {
|
||||
if self.is_empty() {
|
||||
Location::Unknown
|
||||
} else {
|
||||
Location::concat(self.0.first().unwrap(), self.0.last().unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream<Expr> for Module {
|
||||
fn payload(self) -> Vec<Expr> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue