Remove unwraps (#1153)

* Remove a batch of unwraps

* finish another batch
This commit is contained in:
Jonathan Turner
2020-01-04 10:11:21 +13:00
committed by GitHub
parent 339a2de0eb
commit 5919c6c433
27 changed files with 449 additions and 220 deletions

View File

@ -42,7 +42,11 @@ impl CompareOperator {
impl From<&str> for CompareOperator {
fn from(input: &str) -> CompareOperator {
CompareOperator::from_str(input).unwrap()
if let Ok(output) = CompareOperator::from_str(input) {
output
} else {
unreachable!("Internal error: CompareOperator from failed")
}
}
}
@ -90,7 +94,11 @@ impl EvaluationOperator {
impl From<&str> for EvaluationOperator {
fn from(input: &str) -> EvaluationOperator {
EvaluationOperator::from_str(input).unwrap()
if let Ok(output) = EvaluationOperator::from_str(input) {
output
} else {
unreachable!("Internal error: EvaluationOperator 'from' failed")
}
}
}

View File

@ -148,13 +148,21 @@ macro_rules! primitive_decimal {
$(
impl From<$ty> for Number {
fn from(decimal: $ty) -> Number {
Number::Decimal(BigDecimal::$from(decimal).unwrap())
if let Some(num) = BigDecimal::$from(decimal) {
Number::Decimal(num)
} else {
unreachable!("Internal error: BigDecimal 'from' failed")
}
}
}
impl From<&$ty> for Number {
fn from(decimal: &$ty) -> Number {
Number::Decimal(BigDecimal::$from(*decimal).unwrap())
if let Some(num) = BigDecimal::$from(*decimal) {
Number::Decimal(num)
} else {
unreachable!("Internal error: BigDecimal 'from' failed")
}
}
}
)*
@ -909,11 +917,13 @@ pub fn module(input: NomSpan) -> IResult<NomSpan, TokenNode> {
}
fn parse_int<T>(frag: &str, neg: Option<T>) -> i64 {
let int = FromStr::from_str(frag).unwrap();
match neg {
None => int,
Some(_) => -int,
if let Ok(int) = FromStr::from_str(frag) {
match neg {
None => int,
Some(_) => -int,
}
} else {
unreachable!("Internal error: parse_int failed");
}
}

View File

@ -323,10 +323,13 @@ impl TokenTreeBuilder {
let mut input = input.into_iter();
let head = input.next().unwrap();
let tail = input.collect();
if let Some(head) = input.next() {
let tail = input.collect();
CallNode::new(Box::new(head), tail).spanned(span.into())
CallNode::new(Box::new(head), tail).spanned(span.into())
} else {
unreachable!("Internal error: spanned_call failed")
}
}
fn consume_delimiter(

View File

@ -88,9 +88,19 @@ impl RawNumber {
pub(crate) fn to_number(self, source: &Text) -> Number {
match self {
RawNumber::Int(tag) => Number::Int(BigInt::from_str(tag.slice(source)).unwrap()),
RawNumber::Int(tag) => {
if let Ok(int) = BigInt::from_str(tag.slice(source)) {
Number::Int(int)
} else {
unreachable!("Internal error: to_number failed")
}
}
RawNumber::Decimal(tag) => {
Number::Decimal(BigDecimal::from_str(tag.slice(source)).unwrap())
if let Ok(decimal) = BigDecimal::from_str(tag.slice(source)) {
Number::Decimal(decimal)
} else {
unreachable!("Internal error: to_number failed")
}
}
}
}