From 0ba9a301b0db81a1c98e34184315c51c18dddf57 Mon Sep 17 00:00:00 2001 From: Joseph Jon Booker Date: Mon, 25 Mar 2024 22:03:26 -0500 Subject: [PATCH] expr: fix panic for "empty substitution" test case --- src/uu/expr/src/syntax_tree.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/uu/expr/src/syntax_tree.rs b/src/uu/expr/src/syntax_tree.rs index 30d50fb26..d333e6c2f 100644 --- a/src/uu/expr/src/syntax_tree.rs +++ b/src/uu/expr/src/syntax_tree.rs @@ -148,7 +148,7 @@ impl StringOp { .map_err(|_| ExprError::InvalidRegexExpression)?; Ok(if re.captures_len() > 0 { re.captures(&left) - .map(|captures| captures.at(1).unwrap()) + .and_then(|captures| captures.at(1)) .unwrap_or("") .to_string() } else { @@ -609,4 +609,14 @@ mod test { Err(ExprError::ExpectedClosingBraceInsteadOf("a".to_string())) ); } + + #[test] + fn empty_substitution() { + // causes a panic in 0.0.25 + let result = AstNode::parse(&["a", ":", r"\(b\)*"]) + .unwrap() + .eval() + .unwrap(); + assert_eq!(result.eval_as_string(), ""); + } }