Use anyhow context, bail, and ensure instead of panicking (#63044)

This commit is contained in:
Anton Sviridov 2024-06-03 15:46:32 +01:00 committed by GitHub
parent 6d142c833f
commit 2230a8025c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,4 +1,4 @@
use anyhow::{Context, Result};
use anyhow::{bail, ensure, Context, Result};
use bitvec::prelude::*;
use protobuf::Enum;
use scip::types::{symbol_information, Descriptor, Document, Occurrence, SymbolInformation};
@ -174,7 +174,7 @@ pub fn parse_tree<'a>(
for capture in m.captures {
let capture_name = capture_names
.get(capture.index as usize)
.expect("capture indexes should always work");
.context("capture indexes should always work")?;
if capture_name.starts_with("descriptor") {
descriptors.push((
@ -188,12 +188,12 @@ pub fn parse_tree<'a>(
}
if capture_name.starts_with("scope") {
assert!(scope.is_none(), "declare only one scope per match");
ensure!(scope.is_none(), "declare only one scope per match");
scope = Some(capture);
}
if capture_name.starts_with("enclosing") {
assert!(enclosing_node.is_none(), "declare only one scope per match");
ensure!(enclosing_node.is_none(), "declare only one scope per match");
enclosing_node = Some(capture.node);
}
@ -202,7 +202,7 @@ pub fn parse_tree<'a>(
}
if capture_name.starts_with("kind") {
assert!(kind.is_none(), "declare only one kind per match");
ensure!(kind.is_none(), "declare only one kind per match");
kind = Some(capture_name)
}
}
@ -277,7 +277,7 @@ pub fn parse_tree<'a>(
}
None => {
if local_range.is_none() {
panic!("there must always be at least one descriptor (except for @local)");
bail!("there must always be at least one descriptor (except for @local)");
}
}
}