Skip to content

Commit

Permalink
fix: deny unwrap to prevent panics
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon committed Aug 19, 2022
1 parent f3b6eb2 commit 654d562
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/lib.rs
@@ -1,4 +1,5 @@
#![feature(box_patterns)]
#![deny(clippy::unwrap_used)]

mod config;
mod infer;
Expand Down Expand Up @@ -72,7 +73,13 @@ impl<'a> VisitMut for TransformVisitor<'a> {
expr: JSXExpr::Expr(box Expr::Lit(Lit::Str(string))),
..
})) => {
let (_, d) = Directive::parse(&*string.value).unwrap();
let d = match Directive::parse(&*string.value) {
Ok((_, d)) => d,
Err(e) => {
println!("fail : could not parse `{}` {}", string.value, e);
return;
},
};
if self.tw_attr.replace(literal_from_directive(d, &self.config.theme)).is_some() {
println!("warn : encountered multiple tw attributes");
}
Expand Down Expand Up @@ -189,7 +196,13 @@ impl<'a> VisitMut for TransformVisitor<'a> {
}
};

let (_, d) = Directive::parse(text.as_ref()).unwrap();
let d = match Directive::parse(text) {
Ok((_, d)) => d,
Err(e) => {
println!("fail : could not parse `{}` - {}", text, e);
return;
}
};
if self
.tw_tpl
.replace(literal_from_directive(d, &self.config.theme))
Expand Down
6 changes: 4 additions & 2 deletions src/parse/nom.rs
Expand Up @@ -46,7 +46,9 @@ impl<'a> Expression<'a> {
let mods = many0(terminated(
verify(
take_while(|c| is_alphabetic(c as u8) || c == '-'),
|s: &str| s.len() > 0 && is_alphabetic(s.chars().next().unwrap() as u8),
|s: &str| {
!s.is_empty() && is_alphabetic(s.chars().next().expect("not empty") as u8)
},
),
char(':'),
));
Expand Down Expand Up @@ -80,7 +82,7 @@ impl<'a> Subject<'a> {
pub fn parse(s: &'a str) -> IResult<&'a str, Self, nom::error::Error<&'a str>> {
let literal = verify(
take_while(|c| is_alphanumeric(c as u8) || ['-', '[', ']'].contains(&c)),
|c: &str| !c.is_empty() && is_alphabetic(c.chars().next().unwrap() as u8),
|c: &str| !c.is_empty() && is_alphabetic(c.chars().next().expect("not empty") as u8),
)
.map(Subject::Literal);
let group = delimited(char('('), Directive::parse_inner, char(')')).map(Subject::Group);
Expand Down

0 comments on commit 654d562

Please sign in to comment.