Skip to content

Commit

Permalink
Clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
SethDusek committed Apr 27, 2024
1 parent 48496ab commit 4595115
Show file tree
Hide file tree
Showing 24 changed files with 56 additions and 81 deletions.
16 changes: 4 additions & 12 deletions bindings/ergo-lib-c-core/src/json.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! JSON serialization according to EIP-12 (using strings for BoxValue and TokenAmount)

use std::convert::TryInto;

use derive_more::FromStr;
use ergo_lib::chain::transaction::unsigned::UnsignedTransaction;
use ergo_lib::chain::transaction::DataInput;
Expand Down Expand Up @@ -39,11 +37,8 @@ impl From<Transaction> for TransactionJsonEip12 {
fn from(t: Transaction) -> Self {
TransactionJsonEip12 {
tx_id: t.id(),
inputs: t.inputs.try_into().unwrap(),
data_inputs: t
.data_inputs
.map(|di| di.try_into().unwrap())
.unwrap_or_else(Vec::new),
inputs: t.inputs.into(),
data_inputs: t.data_inputs.map(|di| di.into()).unwrap_or_default(),
outputs: t.outputs.into_iter().map(|b| b.into()).collect(),
}
}
Expand All @@ -67,11 +62,8 @@ pub(crate) struct UnsignedTransactionJsonEip12 {
impl From<UnsignedTransaction> for UnsignedTransactionJsonEip12 {
fn from(t: UnsignedTransaction) -> Self {
UnsignedTransactionJsonEip12 {
inputs: t.inputs.try_into().unwrap(),
data_inputs: t
.data_inputs
.map(|di| di.try_into().unwrap())
.unwrap_or_else(Vec::new),
inputs: t.inputs.into(),
data_inputs: t.data_inputs.map(|di| di.into()).unwrap_or_default(),
outputs: t.output_candidates.into_iter().map(|b| b.into()).collect(),
}
}
Expand Down
11 changes: 3 additions & 8 deletions bindings/ergo-lib-wasm/src/json.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! JSON serialization according to EIP-12 (using strings for BoxValue and TokenAmount)

use std::convert::TryInto;

use derive_more::FromStr;
use ergo_lib::chain::transaction::unsigned::UnsignedTransaction;
use ergo_lib::chain::transaction::DataInput;
Expand Down Expand Up @@ -43,7 +41,7 @@ impl From<Transaction> for TransactionJsonEip12 {
data_inputs: t
.data_inputs
.map(|di| di.as_vec().clone())
.unwrap_or_else(Vec::new),
.unwrap_or_default(),
outputs: t.outputs.into_iter().map(|b| b.into()).collect(),
}
}
Expand All @@ -69,11 +67,8 @@ impl From<UnsignedTransaction> for UnsignedTransactionJsonEip12 {
// Following unwraps are fine since we're converting from BoundedVec to Vec.
#[allow(clippy::unwrap_used)]
UnsignedTransactionJsonEip12 {
inputs: t.inputs.try_into().unwrap(),
data_inputs: t
.data_inputs
.map(|di| di.try_into().unwrap())
.unwrap_or_else(Vec::new),
inputs: t.inputs.into(),
data_inputs: t.data_inputs.map(|di| di.into()).unwrap_or_default(),
outputs: t.output_candidates.into_iter().map(|b| b.into()).collect(),
}
}
Expand Down
4 changes: 1 addition & 3 deletions bindings/ergo-lib-wasm/src/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Verifier

use std::convert::TryFrom;

use ergo_lib::ergotree_ir::sigma_protocol::sigma_boolean::SigmaBoolean;
use wasm_bindgen::{prelude::*, JsValue};

Expand All @@ -15,7 +13,7 @@ pub fn verify_signature(
signature: &[u8],
) -> Result<bool, JsValue> {
if let Address(ergo_lib::ergotree_ir::chain::address::Address::P2Pk(d)) = address.clone() {
let sb = SigmaBoolean::try_from(d).map_err(|e| JsValue::from_str(&format!("{:?}", e)))?;
let sb = SigmaBoolean::from(d);
ergo_lib::ergotree_interpreter::sigma_protocol::verifier::verify_signature(
sb, message, signature,
)
Expand Down
2 changes: 1 addition & 1 deletion ergo-lib/src/chain/ergo_box/box_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ mod tests {
let token_name = "USD".to_string();
let token_desc = "Nothing backed USD token".to_string();
let token_num_dec = 2;
vec![R4, R5, R6].iter().for_each(|r_id| {
[R4, R5, R6].iter().for_each(|r_id| {
let mut box_builder =
ErgoBoxCandidateBuilder::new(out_box_value, force_any_val::<ErgoTree>(), 0);
box_builder.mint_token(
Expand Down
6 changes: 3 additions & 3 deletions ergo-lib/src/wallet/box_selector/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ mod tests {
fn test_select_change_value_is_too_small(inputs in
vec(any_with::<ErgoBoxAssetsData>(
(BoxValue::MIN_RAW * 1000 .. BoxValue::MIN_RAW * 10000).into()), 2..10)) {
let first_input_box = inputs.get(0).unwrap().clone();
let first_input_box = inputs.first().unwrap().clone();
let s = SimpleBoxSelector::new();
let target_balance = BoxValue::try_from(first_input_box.value().as_u64() - 1).unwrap();
let selection = s.select(inputs, target_balance, vec![].as_slice()).unwrap();
Expand All @@ -376,7 +376,7 @@ mod tests {
(BoxValue::MIN_RAW * 1000 .. BoxValue::MIN_RAW * 10000).into()), 2..10),
target_balance in
any_with::<BoxValue>((BoxValue::MIN_RAW * 100 .. BoxValue::MIN_RAW * 1500).into())) {
let first_input_box = inputs.get(0).unwrap().clone();
let first_input_box = inputs.first().unwrap().clone();
prop_assume!(first_input_box.tokens.is_some());
let first_input_box_token = first_input_box.tokens.as_ref().unwrap().first();
let first_input_box_token_amount = u64::from(first_input_box_token.amount);
Expand Down Expand Up @@ -535,7 +535,7 @@ mod tests {
let s = SimpleBoxSelector::new();
let all_input_tokens = sum_tokens_from_boxes(inputs.as_slice()).unwrap();
prop_assume!(!all_input_tokens.is_empty());
let target_token_id = all_input_tokens.keys().collect::<Vec<&TokenId>>().get(0).cloned().unwrap();
let target_token_id = all_input_tokens.keys().collect::<Vec<&TokenId>>().first().cloned().unwrap();
let input_token_amount = u64::from(*all_input_tokens.get(target_token_id).unwrap()) / 2;
let target_token_amount = TokenAmount::MAX_RAW;
prop_assume!(input_token_amount < target_token_amount);
Expand Down
2 changes: 1 addition & 1 deletion ergo-lib/src/wallet/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ mod tests {
// boxes_to_spend are in the different order to test inputs <-> boxes_to_spend association in the
// prover (it should not depend on both of them to be in the same order)
boxes_to_spend.shuffle(&mut thread_rng());
let ergo_tree = ErgoTree::try_from(Expr::Const(secrets.get(0).unwrap().public_image().into())).unwrap();
let ergo_tree = ErgoTree::try_from(Expr::Const(secrets.first().unwrap().public_image().into())).unwrap();
let candidate = ErgoBoxCandidateBuilder::new(BoxValue::SAFE_USER_MIN, ergo_tree, 0)
.build().unwrap();
let output_candidates = vec![candidate];
Expand Down
2 changes: 1 addition & 1 deletion ergo-merkle-tree/src/batchmerkleproof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl BatchMerkleProof {
let e: Vec<BatchMerkleProofIndex> = a_new
.iter()
.copied()
.zip(e_new.into_iter())
.zip(e_new)
.map(|(index, hash)| BatchMerkleProofIndex { index, hash })
.collect();
e_new = validate(&a_new, &e, &m_new)?;
Expand Down
4 changes: 2 additions & 2 deletions ergo-merkle-tree/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl From<LevelNode> for LevelNodeJson {
Self(
node.hash
.map(|hash| base16::encode_lower(&hash))
.unwrap_or_else(String::new),
.unwrap_or_default(),
node.side,
)
}
Expand Down Expand Up @@ -136,7 +136,7 @@ impl From<LevelNode> for BatchLevelNodeJson {
digest: node
.hash
.map(|hash| base16::encode_lower(&hash))
.unwrap_or_else(String::new),
.unwrap_or_default(),
side: node.side,
}
}
Expand Down
4 changes: 2 additions & 2 deletions ergo-merkle-tree/src/merkletree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl MerkleTree {
/// Returns the root hash for MerkleTree. If the tree is empty, then returns [0; 32]
pub fn root_hash(&self) -> Digest32 {
self.nodes
.get(0)
.first()
.and_then(MerkleNode::get_hash)
.cloned()
.unwrap_or_else(Digest32::zero)
Expand All @@ -219,7 +219,7 @@ impl MerkleTree {
/// See: <https://github.com/ergoplatform/ergo/issues/1077>
pub fn root_hash_special(&self) -> Digest32 {
self.nodes
.get(0)
.first()
.and_then(MerkleNode::get_hash)
.cloned()
.unwrap_or_else(|| {
Expand Down
2 changes: 1 addition & 1 deletion ergo-nipopow/src/nipopow_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl PoPowHeader {
.map(|(k, v)| -> Vec<u8> {
std::iter::once(2u8)
.chain(k.iter().copied())
.chain(v.into_iter())
.chain(v)
.collect()
})
.map(ergo_merkle_tree::MerkleNode::from_bytes)
Expand Down
5 changes: 2 additions & 3 deletions ergo-rest/src/api/peer_discovery_internals/non_chrome.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ use url::Url;
//
//#[cfg(target_arch = "wasm32")]
//macro_rules! console_log {
//// Note that this is using the `log` function imported above during
//// `bare_bones`
/// Note that this is using the `log` function imported above during
/// `bare_bones`
//($($t:tt)*) => (log(&format_args!($($t)*).to_string()))
//}

Expand Down Expand Up @@ -237,7 +237,6 @@ async fn peer_discovery_impl<
drop(tx_url);
let coll: Vec<_> = visited_active_peers
.difference(&seeds_set)
.into_iter()
.cloned()
.collect();

Expand Down
2 changes: 1 addition & 1 deletion ergo-rest/src/wasm_timer/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ impl Default for TimerHandle {
unsafe {
let handle = TimerHandle::from_usize(fallback);
let ret = handle.clone();
drop(handle.into_usize());
handle.into_usize();
return ret;
}
}
Expand Down
3 changes: 1 addition & 2 deletions ergo-rest/src/wasm_timer/timer/global/desktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ fn run(timer: Timer, done: Arc<AtomicBool>) {

pin_mut!(timer);
while !done.load(Ordering::SeqCst) {
drop(timer.as_mut().poll(&mut cx));

let _ = timer.as_mut().poll(&mut cx);
timer.advance();
match timer.next_event() {
// Ok, block for the specified time
Expand Down
2 changes: 1 addition & 1 deletion ergotree-interpreter/src/eval/coll_by_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ mod tests {
let ctx = Rc::new(force_any_val::<Context>());
assert_eq!(
eval_out::<Arc<ErgoBox>>(&expr, ctx.clone()).box_id(),
ctx.outputs.get(0).unwrap().box_id()
ctx.outputs.first().unwrap().box_id()
);
}

Expand Down
4 changes: 1 addition & 3 deletions ergotree-interpreter/src/eval/coll_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,7 @@ mod tests {
vec![],
|d| d
.as_vec()
.iter()
.cloned()
.filter(| b| 1 <= b.value.as_i64())
.iter().filter(|& b| 1 <= b.value.as_i64()).cloned()
.collect()
);
assert_eq!(
Expand Down
30 changes: 14 additions & 16 deletions ergotree-interpreter/src/eval/savltree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub(crate) static IS_REMOVE_ALLOWED_EVAL_FN: EvalFn = |_env, _ctx, obj, _args| {
pub(crate) static UPDATE_OPERATIONS_EVAL_FN: EvalFn = |_env, _ctx, obj, args| {
let mut avl_tree_data = obj.try_extract_into::<AvlTreeData>()?;
let new_operations = {
let v = args.get(0).cloned().ok_or_else(|| {
let v = args.first().cloned().ok_or_else(|| {
EvalError::AvlTree("eval is missing first arg (new_operations)".to_string())
})?;
v.try_extract_into::<i8>()? as u8
Expand All @@ -76,7 +76,7 @@ pub(crate) static UPDATE_OPERATIONS_EVAL_FN: EvalFn = |_env, _ctx, obj, args| {
pub(crate) static UPDATE_DIGEST_EVAL_FN: EvalFn = |_env, _ctx, obj, args| {
let mut avl_tree_data = obj.try_extract_into::<AvlTreeData>()?;
let new_digest = {
let v = args.get(0).cloned().ok_or_else(|| {
let v = args.first().cloned().ok_or_else(|| {
EvalError::AvlTree("eval is missing first arg (new_digest)".to_string())
})?;
let bytes_vec = v.try_extract_into::<Vec<u8>>()?;
Expand All @@ -91,7 +91,7 @@ pub(crate) static GET_EVAL_FN: EvalFn =
let avl_tree_data = obj.try_extract_into::<AvlTreeData>()?;

let key = {
let v = args.get(0).cloned().ok_or_else(|| {
let v = args.first().cloned().ok_or_else(|| {
EvalError::AvlTree("eval is missing first arg (entries)".to_string())
})?;
v.try_extract_into::<Vec<u8>>()?
Expand Down Expand Up @@ -139,7 +139,7 @@ pub(crate) static GET_MANY_EVAL_FN: EvalFn =
let avl_tree_data = obj.try_extract_into::<AvlTreeData>()?;

let keys = {
let v = args.get(0).cloned().ok_or_else(|| {
let v = args.first().cloned().ok_or_else(|| {
EvalError::AvlTree("eval is missing first arg (entries)".to_string())
})?;
v.try_extract_into::<Vec<Vec<u8>>>()?
Expand Down Expand Up @@ -201,7 +201,7 @@ pub(crate) static INSERT_EVAL_FN: EvalFn =
}

let entries = {
let v = args.get(0).cloned().ok_or_else(|| {
let v = args.first().cloned().ok_or_else(|| {
EvalError::AvlTree("eval is missing first arg (entries)".to_string())
})?;
v.try_extract_into::<Vec<(Vec<u8>, Vec<u8>)>>()?
Expand Down Expand Up @@ -264,7 +264,7 @@ pub(crate) static REMOVE_EVAL_FN: EvalFn =
}

let keys = {
let v = args.get(0).cloned().ok_or_else(|| {
let v = args.first().cloned().ok_or_else(|| {
EvalError::AvlTree("eval is missing first arg (keys)".to_string())
})?;
v.try_extract_into::<Vec<Vec<u8>>>()?
Expand Down Expand Up @@ -319,7 +319,7 @@ pub(crate) static CONTAINS_EVAL_FN: EvalFn = |_env, _ctx, obj, args| {
let avl_tree_data = obj.try_extract_into::<AvlTreeData>()?;
let key = {
let v = args
.get(0)
.first()
.cloned()
.ok_or_else(|| EvalError::AvlTree("eval is missing first arg (key)".to_string()))?;
Bytes::from(v.try_extract_into::<Vec<u8>>()?)
Expand Down Expand Up @@ -371,7 +371,7 @@ pub(crate) static UPDATE_EVAL_FN: EvalFn =
}

let entries = {
let v = args.get(0).cloned().ok_or_else(|| {
let v = args.first().cloned().ok_or_else(|| {
EvalError::AvlTree("eval is missing first arg (entries)".to_string())
})?;
v.try_extract_into::<Vec<(Vec<u8>, Vec<u8>)>>()?
Expand Down Expand Up @@ -433,8 +433,6 @@ fn map_eval_err<T: std::fmt::Debug>(e: T) -> EvalError {
#[cfg(test)]
#[cfg(feature = "arbitrary")]
mod tests {
use std::convert::TryFrom;

use ergo_avltree_rust::batch_avl_prover::BatchAVLProver;
use ergotree_ir::{
mir::{
Expand Down Expand Up @@ -560,9 +558,9 @@ mod tests {
.into(),
);

let search_key_1 = Literal::try_from(vec![1u8]).unwrap();
let search_key_2 = Literal::try_from(vec![2u8]).unwrap();
let search_key_3 = Literal::try_from(vec![3u8]).unwrap();
let search_key_1 = Literal::from(vec![1u8]);
let search_key_2 = Literal::from(vec![2u8]);
let search_key_3 = Literal::from(vec![3u8]);

let keys = Constant {
tpe: SType::SColl(Box::new(SType::SColl(Box::new(SType::SByte)))),
Expand Down Expand Up @@ -941,7 +939,7 @@ mod tests {
.into(),
);

let key1 = Literal::try_from(vec![1u8]).unwrap();
let key1 = Literal::from(vec![1u8]);
let keys = Constant {
tpe: SType::SColl(Box::new(SType::SColl(Box::new(SType::SByte)))),
v: Literal::Coll(CollKind::WrappedColl {
Expand Down Expand Up @@ -1070,8 +1068,8 @@ mod tests {

fn mk_pair(x: u8, y: u64) -> [Literal; 2] {
[
Literal::try_from(vec![x]).unwrap(),
Literal::try_from(y.to_be_bytes().to_vec()).unwrap(),
Literal::from(vec![x]),
Literal::from(y.to_be_bytes().to_vec()),
]
}
}
2 changes: 1 addition & 1 deletion ergotree-interpreter/src/eval/sbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(crate) static VALUE_EVAL_FN: EvalFn = |_env, _ctx, obj, _args| {

pub(crate) static GET_REG_EVAL_FN: EvalFn = |_env, _ctx, obj, args| {
let reg_id = args
.get(0)
.first()
.cloned()
.ok_or_else(|| EvalError::NotFound("register index is missing".to_string()))?
.try_extract_into::<i8>()?;
Expand Down
Loading

0 comments on commit 4595115

Please sign in to comment.