Skip to content

Commit

Permalink
Propagate Err from number parsing
Browse files Browse the repository at this point in the history
Previously, an unwrap was used.
Propagating the error will preserve spans and improve the error message.

This fixes #289
  • Loading branch information
TedDriggs committed May 13, 2024
1 parent ba363b8 commit 195eccf
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions core/src/from_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ macro_rules! from_meta_num {
fn from_value(value: &Lit) -> Result<Self> {
(match *value {
Lit::Str(ref s) => Self::from_string(&s.value()),
Lit::Int(ref s) => Ok(s.base10_parse::<$ty>().unwrap()),
Lit::Int(ref s) => s.base10_parse::<$ty>().map_err(Error::from),
_ => Err(Error::unexpected_lit_type(value)),
})
.map_err(|e| e.with_span(value))
Expand Down Expand Up @@ -278,7 +278,7 @@ macro_rules! from_meta_float {
fn from_value(value: &Lit) -> Result<Self> {
(match *value {
Lit::Str(ref s) => Self::from_string(&s.value()),
Lit::Float(ref s) => Ok(s.base10_parse::<$ty>().unwrap()),
Lit::Float(ref s) => s.base10_parse::<$ty>().map_err(Error::from),
_ => Err(Error::unexpected_lit_type(value)),
})
.map_err(|e| e.with_span(value))
Expand Down Expand Up @@ -909,6 +909,11 @@ mod tests {
assert_eq!(fm::<f64>(quote!(ignore = 1.4e10)), 1.4e10f64);
}

#[test]
fn too_large_int_produces_error() {
assert!(fm::<Result<u8>>(quote!(ignore = 2000)).is_err());
}

#[test]
fn meta_succeeds() {
use syn::Meta;
Expand Down

0 comments on commit 195eccf

Please sign in to comment.