Skip to content

Commit

Permalink
Refactoring for lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Arp-1 committed Dec 3, 2023
1 parent 21c041f commit 9ecd6a2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/uu/expr/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
.map(|v| v.into_iter().map(|s| s.as_ref()).collect::<Vec<_>>())
.unwrap_or_default();

let res = AstNode::parse(&token_strings)?.eval()?.to_string();
let res: String = AstNode::parse(&token_strings)?.eval()?.into();
println!("{res}");
if !is_truthy(&NumOrStr::from(res)) {
return Err(1.into());
Expand Down
53 changes: 31 additions & 22 deletions src/uu/expr/src/syntax_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl RelationOp {
fn eval(&self, a: &AstNode, b: &AstNode) -> ExprResult<NumOrStr> {
let a = a.eval()?;
let b = b.eval()?;
let b = if let (Ok(a), Ok(b)) = (&a.coerce_bigint(), &b.coerce_bigint()) {
let b = if let (Ok(a), Ok(b)) = (&a.to_bigint(), &b.to_bigint()) {
match self {
Self::Lt => a < b,
Self::Leq => a <= b,
Expand Down Expand Up @@ -89,8 +89,8 @@ impl RelationOp {

impl NumericOp {
fn eval(&self, left: &AstNode, right: &AstNode) -> ExprResult<NumOrStr> {
let a = left.eval()?.to_bigint()?;
let b = right.eval()?.to_bigint()?;
let a = <NumOrStr as Into<ExprResult<BigInt>>>::into(left.eval()?)?;
let b = <NumOrStr as Into<ExprResult<BigInt>>>::into(right.eval()?)?;
Ok(NumOrStr::Num(match self {
Self::Add => a + b,
Self::Sub => a - b,
Expand Down Expand Up @@ -135,28 +135,28 @@ impl StringOp {
Ok(left)
}
Self::Match => {
let left = left.eval()?;
let right = right.eval()?;
let re_string = format!("^{}", right.to_string());
let left: String = left.eval()?.into();
let right: String = right.eval()?.into();
let re_string = format!("^{}", right);
let re = Regex::with_options(
&re_string,
RegexOptions::REGEX_OPTION_NONE,
Syntax::grep(),
)
.map_err(|_| ExprError::InvalidRegexExpression)?;
Ok(NumOrStr::from(if re.captures_len() > 0 {
re.captures(&left.to_string())
re.captures(&left)
.map(|captures| captures.at(1).unwrap())
.unwrap_or("")
.to_string()
} else {
re.find(&left.to_string())
re.find(&left)
.map_or("0".to_string(), |(start, end)| (end - start).to_string())
}))
}
Self::Index => {
let left = left.eval()?.to_string();
let right = right.eval()?.to_string();
let left: String = left.eval()?.into();
let right: String = right.eval()?.into();
for (current_idx, ch_h) in left.chars().enumerate() {
for ch_n in right.to_string().chars() {
if ch_n == ch_h {
Expand Down Expand Up @@ -218,31 +218,37 @@ impl From<String> for NumOrStr {
}
}

impl NumOrStr {
pub fn to_usize(self) -> Option<usize> {
match self.to_bigint() {
impl Into<Option<usize>> for NumOrStr {

Check failure on line 221 in src/uu/expr/src/syntax_tree.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-22.04, unix)

ERROR: `cargo clippy`: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true (file:'src/uu/expr/src/syntax_tree.rs', line:221)
fn into(self) -> Option<usize> {
match self.into() {
Ok(num) => num.to_usize(),
Err(_) => None,

Check warning on line 225 in src/uu/expr/src/syntax_tree.rs

View check run for this annotation

Codecov / codecov/patch

src/uu/expr/src/syntax_tree.rs#L225

Added line #L225 was not covered by tests
}
}
}

pub fn to_string(self) -> String {
impl Into<String> for NumOrStr {

Check failure on line 230 in src/uu/expr/src/syntax_tree.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-22.04, unix)

ERROR: `cargo clippy`: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true (file:'src/uu/expr/src/syntax_tree.rs', line:230)
fn into(self) -> String {
match self {
Self::Num(num) => num.to_string(),
Self::Str(str) => str.to_string(),
}
}
}

pub fn to_bigint(self) -> ExprResult<BigInt> {
impl Into<ExprResult<BigInt>> for NumOrStr {

Check failure on line 239 in src/uu/expr/src/syntax_tree.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-22.04, unix)

ERROR: `cargo clippy`: an implementation of `From` is preferred since it gives you `Into<_>` for free where the reverse isn't true (file:'src/uu/expr/src/syntax_tree.rs', line:239)
fn into(self) -> ExprResult<BigInt> {
match self {
Self::Num(num) => Ok(num),
Self::Str(str) => str
.parse::<BigInt>()
.map_err(|_| ExprError::NonIntegerArgument),
}
}
}

pub fn coerce_bigint(&self) -> Result<BigInt, ParseBigIntError> {
impl NumOrStr {
pub fn to_bigint(&self) -> Result<BigInt, ParseBigIntError> {
match self {
Self::Num(num) => Ok(num.clone()),
Self::Str(str) => str.parse::<BigInt>(),
Expand Down Expand Up @@ -288,7 +294,7 @@ impl AstNode {
pos,
length,
} => {
let string = string.eval()?.to_string();
let string: String = string.eval()?.into();

// The GNU docs say:
//
Expand All @@ -297,8 +303,9 @@ impl AstNode {
//
// So we coerce errors into 0 to make that the only case we
// have to care about.
let pos: usize = pos.eval()?.to_usize().unwrap_or(0);
let length: usize = length.eval()?.to_usize().unwrap_or(0);
let pos: usize = <NumOrStr as Into<Option<usize>>>::into(pos.eval()?).unwrap_or(0);
let length: usize =
<NumOrStr as Into<Option<usize>>>::into(length.eval()?).unwrap_or(0);

let (Some(pos), Some(_)) = (pos.checked_sub(1), length.checked_sub(1)) else {
return Ok(NumOrStr::from(String::new()));
Expand All @@ -308,9 +315,11 @@ impl AstNode {
string.chars().skip(pos).take(length).collect::<String>(),
))
}
Self::Length { string } => {
Ok(NumOrStr::from(string.eval()?.to_string().chars().count()))
}
Self::Length { string } => Ok(NumOrStr::from(
<NumOrStr as Into<String>>::into(string.eval()?)
.chars()
.count(),
)),
}
}
}
Expand Down

0 comments on commit 9ecd6a2

Please sign in to comment.