From 5d84b017b94fa28d986ce843663d7f7f0ac55b5e Mon Sep 17 00:00:00 2001 From: Alain Emilia Anna Zscheile Date: Wed, 24 Jul 2024 00:04:17 +0200 Subject: [PATCH] cargo clippy --- src/expr/linkplan.rs | 17 ++++++++--------- src/expr/mod.rs | 24 ++++++++++++++---------- src/main.rs | 2 +- src/mesh/grid.rs | 5 ++--- 4 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/expr/linkplan.rs b/src/expr/linkplan.rs index a98432c..9a4739b 100644 --- a/src/expr/linkplan.rs +++ b/src/expr/linkplan.rs @@ -50,9 +50,8 @@ fn calculate_votes(mut que: Vec>) -> Vec, VoteEntry .take(que.iter().map(|i| sum(&i[..])).max().map(|i| i.checked_add(1).unwrap()).unwrap_or(0)) .collect::, VoteEntry>>>(); - for i in core::mem::replace(&mut que, Vec::new()) { - let mut l = &mut levels[sum(&i[..])]; - l.insert(i, VoteEntry { + for i in core::mem::take(&mut que) { + levels[sum(&i[..])].insert(i, VoteEntry { votes: 1, best_parent: 0, is_used: true, @@ -62,9 +61,9 @@ fn calculate_votes(mut que: Vec>) -> Vec, VoteEntry // process levels (start from largest) for (lid, i) in levels.iter_mut().enumerate().rev() { // insert queued items into this level - for j in core::mem::replace(&mut que, Vec::new()) { - let mut v = i.entry(j).or_default(); - v.votes = v.votes.checked_add(1).unwrap(); + for j in core::mem::take(&mut que) { + let votes = &mut i.entry(j).or_default().votes; + *votes = votes.checked_add(1).unwrap(); } // handle all items in this level @@ -138,9 +137,9 @@ pub fn fullfill_links(que: Vec) -> Vec, u16)>>> { let mut ret = vec![Vec::new(); max_vid]; for (vid, que) in g { - let mut votes = calculate_votes(que.clone()); - let mut ret = &mut ret[usize::from(vid)]; - ret.resize_with(votes.len(), || Vec::new()); + let votes = calculate_votes(que.clone()); + let ret = &mut ret[usize::from(vid)]; + ret.resize_with(votes.len(), Vec::new); for (i, j) in ret.iter_mut().zip(votes.into_iter()) { for (k, l) in j.into_iter() { diff --git a/src/expr/mod.rs b/src/expr/mod.rs index 7a2ce6d..7397b19 100644 --- a/src/expr/mod.rs +++ b/src/expr/mod.rs @@ -104,7 +104,7 @@ impl System { #[derive(Clone, Debug, PartialEq, thiserror::Error)] pub enum ParseError { #[error("unexpected end of expression")] - EmptyExpr, + UnexpectedEol, #[error("unexpected invalid char '{0}'")] InvalidChar(char), @@ -131,14 +131,14 @@ pub enum ParseError { Float(#[from] std::num::ParseFloatError), } -trait ParseLink: Sized { +pub trait ParseLink: Sized { fn parse_link(slb: &mut StrLexerBase<'_>) -> Result, ParseError>; } impl ParseLink for () { fn parse_link(slb: &mut StrLexerBase<'_>) -> Result, ParseError> { if slb.inp.is_empty() { - Err(ParseError::EmptyExpr) + Err(ParseError::UnexpectedEol) } else if slb.inp.starts_with('.') { slb.consume(1); Ok(Some(())) @@ -151,9 +151,9 @@ impl ParseLink for () { impl ParseLink for Link { fn parse_link(slb: &mut StrLexerBase<'_>) -> Result, ParseError> { if slb.inp.is_empty() { - Err(ParseError::EmptyExpr) + Err(ParseError::UnexpectedEol) } else { - let mut new_slb = (*slb).clone(); + let mut new_slb = *slb; let var = new_slb.consume_select(|i| i.is_ascii_digit()); if var.is_empty() { return Ok(None); @@ -188,7 +188,7 @@ fn parse_expr(slb: &mut StrLexerBase<'_>) -> Result(slb: &mut StrLexerBase<'_>) -> Result(slb: &mut StrLexerBase<'_>) -> Result(slb: &mut StrLexerBase<'_>) -> Result(slb)?); slb.consume_select(|i| i.is_whitespace()); - if slb.inp.chars().next() != Some(',') { + if !slb.inp.starts_with(',') { break; } slb.consume(1); } - if slb.inp.chars().next() != Some(')') { + if slb.inp.is_empty() { + return Err(ParseError::UnexpectedEol); + } + + if !slb.inp.starts_with(')') { let c = slb.inp.chars().next().unwrap(); return Err(ParseError::InvalidChar(c)); } diff --git a/src/main.rs b/src/main.rs index a8e4291..526dc0e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,7 +42,7 @@ fn main() { // parse system let system = { let system = std::fs::read(&cli.system).expect("unable to read system to render"); - let system = std::str::from_utf8(&*system).expect("unable to interpret system description as UTF-8"); + let system = std::str::from_utf8(&system).expect("unable to interpret system description as UTF-8"); expr::parse_system(system).expect("unable to parse system") }; diff --git a/src/mesh/grid.rs b/src/mesh/grid.rs index b08eedd..2eab7c4 100644 --- a/src/mesh/grid.rs +++ b/src/mesh/grid.rs @@ -21,13 +21,12 @@ impl ForEachStencilData<'_, F> { let prefix: usize = self.buf.len().checked_sub(shape.len()).expect("buffer too small"); // recurse over `this` - if let Some(&fi) = shape.get(0) { + if let Some(&fi) = shape.first() { let shnext = &shape[1..]; for i in self.stencil_start .. (fi + 1 - self.stencil_end) { self.buf[prefix] = i; self.fe_st_intern(shnext); } - return; } else { (*self.f)(self.buf); } @@ -68,7 +67,7 @@ impl Shape { return None; } // (v, acc) = (x % s, x / s) - Some(acc.checked_mul(s)?.checked_add(v)?) + acc.checked_mul(s)?.checked_add(v) }) }