Skip to content

Commit

Permalink
[easy] Fix Clippy on master (#492)
Browse files Browse the repository at this point in the history
* chore: run clippy

* test: Improve error messaging for parser test assertions

- Replaced bare `assert!(false)` with informative `unreachable!("unexpected parse result")` in `src/parser/string.rs` and `src/parser/base.rs`.

* refactor: Refactor code for readability using Option::then

- Optimized the slicing of the symbol path array in `store.rs` using shorthand syntax.
- Utilized the `.then()` combinator
  • Loading branch information
huitseeker authored Jul 6, 2023
1 parent 57dfee0 commit bb335de
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 93 deletions.
4 changes: 3 additions & 1 deletion clutch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,9 @@ impl ClutchState<F, Coproc<F>> {
chain: bool,
) -> Result<Option<Ptr<F>>> {
let args = store.cdr(&rest)?;
let (commitment, Some(e)) = self.open_aux(store, rest)? else { bail!("failed to open") };
let (commitment, Some(e)) = self.open_aux(store, rest)? else {
bail!("failed to open")
};
let call = store.cons(e, args);
let (arg, _) = store.car_cdr(&args)?;

Expand Down
6 changes: 1 addition & 5 deletions src/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,7 @@ impl<F: LurkField> IO<F> {
_ => return None,
};

if expr.continuation.tag == crate::tag::ContTag::Emit {
Some(expr.value)
} else {
None
}
(expr.continuation.tag == crate::tag::ContTag::Emit).then_some(expr.value)
}

pub fn to_vector(&self, store: &Store<F>) -> Result<Vec<F>, store::Error> {
Expand Down
2 changes: 1 addition & 1 deletion src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ pub mod tests {

#[test]
fn prop_ser_de(x in any::<FWrap<Fr>>()) {
let bytes = to_z_data(&x).unwrap();
let bytes = to_z_data(x).unwrap();
let f2: FWrap<Fr> = from_z_data(&bytes).unwrap();
assert_eq!(x, f2)
}
Expand Down
4 changes: 2 additions & 2 deletions src/lem/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ impl LEMOP {
LEMOP::Open(tgt_secret, tgt_ptr, comm_or_num) => match bindings.get(comm_or_num)? {
Ptr::Leaf(Tag::Num, hash) | Ptr::Leaf(Tag::Comm, hash) => {
let Some((secret, ptr)) = store.comms.get(&FWrap::<F>(*hash)) else {
bail!("No committed data for hash {}", &hash.hex_digits())
};
bail!("No committed data for hash {}", &hash.hex_digits())
};
bindings.insert(tgt_ptr.clone(), *ptr)?;
bindings.insert(tgt_secret.clone(), Ptr::Leaf(Tag::Num, *secret))?;
Ok(())
Expand Down
14 changes: 7 additions & 7 deletions src/lem/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl<F: LurkField> Store<F> {
match self.sym_cache.get(path) {
Some(ptr_cache) => *ptr_cache,
None => {
let tail = &path[1..path.len()];
let tail = &path[1..];
let tail_ptr = self.intern_symbol_path(tail);
let head = &path[0];
let head_ptr = self.intern_string(head);
Expand Down Expand Up @@ -228,8 +228,8 @@ impl<F: LurkField> Store<F> {
Some(z_ptr) => Ok(*z_ptr),
None => {
let Some((a, b)) = self.ptrs2.get_index(*idx) else {
bail!("Index {idx} not found on ptrs2")
};
bail!("Index {idx} not found on ptrs2")
};
let a = self.hash_ptr(a)?;
let b = self.hash_ptr(b)?;
let z_ptr = ZPtr {
Expand All @@ -250,8 +250,8 @@ impl<F: LurkField> Store<F> {
Some(z_ptr) => Ok(*z_ptr),
None => {
let Some((a, b, c)) = self.ptrs3.get_index(*idx) else {
bail!("Index {idx} not found on ptrs3")
};
bail!("Index {idx} not found on ptrs3")
};
let a = self.hash_ptr(a)?;
let b = self.hash_ptr(b)?;
let c = self.hash_ptr(c)?;
Expand All @@ -275,8 +275,8 @@ impl<F: LurkField> Store<F> {
Some(z_ptr) => Ok(*z_ptr),
None => {
let Some((a, b, c, d)) = self.ptrs4.get_index(*idx) else {
bail!("Index {idx} not found on ptrs4")
};
bail!("Index {idx} not found on ptrs4")
};
let a = self.hash_ptr(a)?;
let b = self.hash_ptr(b)?;
let c = self.hash_ptr(c)?;
Expand Down
6 changes: 3 additions & 3 deletions src/parser/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,17 @@ pub mod tests {
println!("input: {:?}", i);
println!("expected: {:?}", expected);
println!("detected: {:?}", x);
assert!(false)
unreachable!("unexpected parse result")
}
(Some(..), Err(e)) => {
println!("{}", e);
assert!(false)
unreachable!("unexpected parse result")
}
(None, Ok((i, x))) => {
println!("input: {:?}", i);
println!("expected parse error");
println!("detected: {:?}", x);
assert!(false)
unreachable!("unexpected parse result")
}
(None, Err(_e)) => (),
}
Expand Down
6 changes: 3 additions & 3 deletions src/parser/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,17 @@ pub mod tests {
println!("input: {:?}", i);
println!("expected: {:?}", expected);
println!("detected: {:?}", x);
assert!(false)
unreachable!("unexpected parse result")
}
(Some(..), Err(e)) => {
println!("{}", e);
assert!(false)
unreachable!("unexpected parse result")
}
(None, Ok((i, x))) => {
println!("input: {:?}", i);
println!("expected parse error");
println!("detected: {:?}", x);
assert!(false)
unreachable!("unexpected parse result")
}
(None, Err(_e)) => (),
}
Expand Down
32 changes: 14 additions & 18 deletions src/proof/groth16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,24 +431,20 @@ mod tests {
check_cs_deltas(&cs, limit, lang_rc.clone());
}

let proof_results = if check_groth16 {
Some(
groth_prover
.outer_prove(
groth_params,
&INNER_PRODUCT_SRS,
expr,
empty_sym_env(s),
s,
limit,
rng,
lang_rc,
)
.unwrap(),
)
} else {
None
};
let proof_results = (check_groth16).then(|| {
groth_prover
.outer_prove(
groth_params,
&INNER_PRODUCT_SRS,
expr,
empty_sym_env(s),
s,
limit,
rng,
lang_rc,
)
.unwrap()
});

if let Some((proof, public_inputs, public_outputs)) = proof_results {
let srs_vk = INNER_PRODUCT_SRS.specialize_vk(proof.proof_count);
Expand Down
61 changes: 10 additions & 51 deletions src/syntax_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,13 @@ macro_rules! char {
macro_rules! symbol {
([$( $x:expr ),*]) => {
{
#[allow(unused_mut)]
let mut temp_vec = Vec::new();
$(
temp_vec.push($x.to_string());
)*
let temp_vec = vec![ $( $x.to_string() ),* ];
$crate::syntax::Syntax::Symbol(Pos::No, $crate::symbol::Symbol { path: temp_vec })
}
};
($f:ty, [$( $x:expr ),*] ) => {
{
#[allow(unused_mut)]
let mut temp_vec = Vec::new();
$(
temp_vec.push($x.to_string());
)*
let temp_vec = vec![ $( $x.to_owned() ),* ];
$crate::syntax::Syntax::<$f>::Symbol(Pos::No, $crate::symbol::Symbol {path: temp_vec})
}
};
Expand All @@ -71,11 +63,7 @@ macro_rules! symbol {
macro_rules! sym {
[$( $x:expr ),*] => {
{
#[allow(unused_mut)]
let mut temp_vec = Vec::new();
$(
temp_vec.push($x.to_string());
)*
let temp_vec = vec![ $( $x.to_string() ),* ];
$crate::symbol::Symbol::Sym(temp_vec)
}
};
Expand All @@ -86,12 +74,7 @@ macro_rules! sym {
macro_rules! lurksym {
[$( $x:expr ),*] => {
{
#[allow(unused_mut)]
let mut temp_vec = Vec::new();
temp_vec.push("lurk".to_string());
$(
temp_vec.push($x.to_string());
)*
let temp_vec = vec![ "lurk".to_owned(), $( $x.to_string() ),* ];
$crate::symbol::Symbol::Sym(temp_vec)
}
};
Expand All @@ -101,21 +84,13 @@ macro_rules! lurksym {
macro_rules! keyword {
([$( $x:expr ),*]) => {
{
#[allow(unused_mut)]
let mut temp_vec = vec!["keyword"];
$(
temp_vec.push($x);
)*
let temp_vec = vec![ "keyword", $( $x ),* ];
$crate::syntax::Syntax::Keyword(Pos::No, $crate::symbol::Symbol::new(&temp_vec))
}
};
($f:ty, [$( $x:expr ),*]) => {
{
#[allow(unused_mut)]
let mut temp_vec = vec!["keyword"];
$(
temp_vec.push($x.to_string());
)*
let temp_vec = vec![ "keyword".to_owned(), $( $x.to_string() ),* ];
$crate::syntax::Syntax::Keyword(Pos::No, $crate::symbol::Symbol {path: temp_vec})
}
};
Expand All @@ -124,41 +99,25 @@ macro_rules! keyword {
macro_rules! list {
([$( $x:expr ),*], $end:expr ) => {
{
#[allow(unused_mut)]
let mut temp_vec = Vec::new();
$(
temp_vec.push($x);
)*
let temp_vec = vec![ $( $x ),* ];
$crate::syntax::Syntax::Improper(Pos::No, temp_vec, Box::new($end))
}
};
([$( $x:expr ),*] ) => {
{
#[allow(unused_mut)]
let mut temp_vec = Vec::new();
$(
temp_vec.push($x);
)*
let temp_vec = vec![ $( $x ),* ];
$crate::syntax::Syntax::List(Pos::No, temp_vec)
}
};
($f:ty, [$( $x:expr ),*], $end:expr ) => {
{
#[allow(unused_mut)]
let mut temp_vec = Vec::new();
$(
temp_vec.push($x);
)*
let temp_vec = vec![ $( $x ),* ];
$crate::syntax::Syntax::<$f>::Improper(Pos::No, temp_vec, Box::new($end))
}
};
($f:ty, [$( $x:expr ),*] ) => {
{
#[allow(unused_mut)]
let mut temp_vec = Vec::new();
$(
temp_vec.push($x);
)*
let temp_vec = vec![ $( $x ),* ];
$crate::syntax::Syntax::<$f>::List(Pos::No, temp_vec)
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/z_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ pub mod tests {
#[test]
fn unit_trimmed_bytes() {
assert_eq!(ZData::to_trimmed_le_bytes(43411), vec![147, 169]);
assert_eq!(43411, ZData::read_size_bytes(&vec![147, 169]).unwrap());
assert_eq!(43411, ZData::read_size_bytes(&[147, 169]).unwrap());
assert_eq!(ZData::to_trimmed_le_bytes(37801), vec![169, 147]);
assert_eq!(37801, ZData::read_size_bytes(&vec![169, 147]).unwrap());
assert_eq!(37801, ZData::read_size_bytes(&[169, 147]).unwrap());
}

#[test]
Expand Down

0 comments on commit bb335de

Please sign in to comment.