Skip to content

Commit

Permalink
Run cargo clippy --fix (#790)
Browse files Browse the repository at this point in the history
* Run cargo clippy --fix

* Remove braces

---------

Co-authored-by: Graydon Hoare <graydon@pobox.com>
  • Loading branch information
sisuresh and graydon authored May 4, 2023
1 parent b5858ea commit 8952557
Show file tree
Hide file tree
Showing 32 changed files with 207 additions and 238 deletions.
4 changes: 2 additions & 2 deletions soroban-env-common/src/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl<T, C: Compare<T>> Compare<Box<T>> for C {
type Error = C::Error;

fn compare(&self, a: &Box<T>, b: &Box<T>) -> Result<Ordering, Self::Error> {
<Self as Compare<T>>::compare(self, &*a, &*b)
<Self as Compare<T>>::compare(self, a, b)
}
}

Expand All @@ -106,7 +106,7 @@ impl<T, C: Compare<T>> Compare<Rc<T>> for C {
type Error = C::Error;

fn compare(&self, a: &Rc<T>, b: &Rc<T>) -> Result<Ordering, Self::Error> {
<Self as Compare<T>>::compare(self, &*a, &*b)
<Self as Compare<T>>::compare(self, a, b)
}
}

Expand Down
2 changes: 1 addition & 1 deletion soroban-env-common/src/env_val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ where
}
ScVal::I256(i) => {
assert!(num::is_small_i256_parts(i));
unsafe { RawVal::from_body_and_tag(i.lo_lo as u64, Tag::I256Small) }
unsafe { RawVal::from_body_and_tag(i.lo_lo, Tag::I256Small) }
}
ScVal::Symbol(bytes) => {
let ss = match std::str::from_utf8(bytes.as_slice()) {
Expand Down
2 changes: 1 addition & 1 deletion soroban-env-common/src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
// nonzero pre-release number can be used to force recompiles on interface
// changes.

pub const ENV_META_V0_SECTION_NAME: &'static str = "contractenvmetav0";
pub const ENV_META_V0_SECTION_NAME: &str = "contractenvmetav0";

soroban_env_macros::generate_env_meta_consts!(
ledger_protocol_version: 20,
Expand Down
10 changes: 5 additions & 5 deletions soroban-env-common/src/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl From<U64Small> for u64 {

impl From<I64Small> for i64 {
fn from(value: I64Small) -> Self {
value.0.get_signed_body() as i64
value.0.get_signed_body()
}
}

Expand All @@ -118,7 +118,7 @@ impl From<U128Small> for u128 {

impl From<I128Small> for i128 {
fn from(value: I128Small) -> Self {
(value.0.get_signed_body() as i64) as i128
value.0.get_signed_body() as i128
}
}

Expand All @@ -130,7 +130,7 @@ impl From<U256Small> for U256 {

impl From<I256Small> for I256 {
fn from(value: I256Small) -> Self {
I256::from(value.0.get_signed_body() as i64)
I256::from(value.0.get_signed_body())
}
}

Expand Down Expand Up @@ -229,7 +229,7 @@ impl TryFrom<U256Small> for ScVal {
impl TryFrom<&U256Small> for ScVal {
type Error = ConversionError;
fn try_from(value: &U256Small) -> Result<Self, Self::Error> {
value.clone().try_into()
(*value).try_into()
}
}

Expand All @@ -250,7 +250,7 @@ impl TryFrom<I256Small> for ScVal {
impl TryFrom<&I256Small> for ScVal {
type Error = ConversionError;
fn try_from(value: &I256Small) -> Result<Self, Self::Error> {
value.clone().try_into()
(*value).try_into()
}
}

Expand Down
8 changes: 4 additions & 4 deletions soroban-env-common/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl ScValObject {
/// Inspect the provided `value` and return `Ok(ScValObject(value))` if it
/// is a value that should be represented as an object, else `Err(value)`.
pub fn classify(value: ScVal) -> Result<ScValObject, ScVal> {
if let Some(_) = ScValObjRef::classify(&value) {
if ScValObjRef::classify(&value).is_some() {
Ok(ScValObject(value))
} else {
Err(value)
Expand All @@ -110,9 +110,9 @@ impl AsRef<ScVal> for ScValObject {
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct ScValObjRef<'a>(&'a ScVal);

impl<'a> Into<&'a ScVal> for ScValObjRef<'a> {
fn into(self) -> &'a ScVal {
self.0
impl<'a> From<ScValObjRef<'a>> for &'a ScVal {
fn from(val: ScValObjRef<'a>) -> Self {
val.0
}
}

Expand Down
10 changes: 5 additions & 5 deletions soroban-env-common/src/raw_val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ impl RawValConvertible for u32 {
}
#[inline(always)]
unsafe fn unchecked_from_val(v: RawVal) -> Self {
v.get_major() as u32
v.get_major()
}
}

Expand Down Expand Up @@ -681,8 +681,8 @@ impl Debug for RawVal {
}

match self.get_tag() {
Tag::U32Val => write!(f, "U32({})", self.get_major() as u32),
Tag::I32Val => write!(f, "I32({})", (self.get_major() as u32) as i32),
Tag::U32Val => write!(f, "U32({})", self.get_major()),
Tag::I32Val => write!(f, "I32({})", self.get_major() as i32),
Tag::False => write!(f, "False"),
Tag::True => write!(f, "True"),
Tag::Void => write!(f, "Void"),
Expand All @@ -695,10 +695,10 @@ impl Debug for RawVal {
Tag::DurationSmall => write!(f, "Duration({})", self.get_body()),
// These can't be bigger than u64/i64 so just cast to them.
Tag::U128Small => write!(f, "U128({})", self.get_body()),
Tag::I128Small => write!(f, "I128({})", self.get_signed_body() as i64),
Tag::I128Small => write!(f, "I128({})", { self.get_signed_body() }),
// These can't be bigger than u64/i64 so just cast to them.
Tag::U256Small => write!(f, "U256({})", self.get_body()),
Tag::I256Small => write!(f, "I256({})", self.get_signed_body() as i64),
Tag::I256Small => write!(f, "I256({})", { self.get_signed_body() }),
Tag::SymbolSmall => {
let ss: SymbolStr =
unsafe { <SymbolSmall as RawValConvertible>::unchecked_from_val(*self) }.into();
Expand Down
8 changes: 4 additions & 4 deletions soroban-env-common/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Ord for Status {
impl<E: Env> Compare<Status> for E {
type Error = E::Error;
fn compare(&self, a: &Status, b: &Status) -> Result<Ordering, Self::Error> {
Ok(a.cmp(&b))
Ok(a.cmp(b))
}
}

Expand Down Expand Up @@ -316,7 +316,7 @@ impl Status {
ScStatus::HostAuthError(code) => code as i32 as u32,
ScStatus::VmError(code) => code as i32 as u32,
ScStatus::UnknownError(code) => code as i32 as u32,
ScStatus::ContractError(code) => code as u32,
ScStatus::ContractError(code) => code,
};
Self::from_type_and_code(sc.discriminant(), code)
}
Expand Down Expand Up @@ -366,7 +366,7 @@ mod tests {
];

let pairs: Vec<_> = xdr_vals
.into_iter()
.iter()
.map(|xdr_val| {
let host_val = Status::try_from(xdr_val.clone()).unwrap();
(xdr_val, host_val)
Expand All @@ -376,7 +376,7 @@ mod tests {
let mut pairs_xdr_sorted = pairs.clone();
let mut pairs_host_sorted = pairs_xdr_sorted.clone();

pairs_xdr_sorted.sort_by(|&(v1, _), &(v2, _)| v1.cmp(&v2));
pairs_xdr_sorted.sort_by(|&(v1, _), &(v2, _)| v1.cmp(v2));

pairs_host_sorted.sort_by(|&(_, v1), &(_, v2)| v1.cmp(&v2));

Expand Down
6 changes: 2 additions & 4 deletions soroban-env-common/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl<E: Env> TryFromVal<E, StringObject> for String {
fn try_from_val(env: &E, v: &StringObject) -> Result<Self, Self::Error> {
let len: u32 = env.string_len(*v).map_err(|_| ConversionError)?.into();
let len = len as usize;
let mut vec = std::vec![0; len as usize];
let mut vec = std::vec![0; len];
env.string_copy_to_slice(*v, RawVal::U32_ZERO, &mut vec)
.map_err(|_| ConversionError)?;
String::from_utf8(vec).map_err(|_| ConversionError)
Expand All @@ -34,9 +34,7 @@ impl<E: Env> TryFromVal<E, &str> for StringObject {
type Error = ConversionError;
#[inline(always)]
fn try_from_val(env: &E, val: &&str) -> Result<StringObject, Self::Error> {
Ok(env
.string_new_from_slice(*val)
.map_err(|_| ConversionError)?)
env.string_new_from_slice(val).map_err(|_| ConversionError)
}
}

Expand Down
4 changes: 2 additions & 2 deletions soroban-env-common/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<E: Env> TryFromVal<E, &[u8]> for Symbol {

fn try_from_val(env: &E, v: &&[u8]) -> Result<Self, Self::Error> {
// We don't know this byte-slice is actually utf-8 ...
let s: &str = unsafe { core::str::from_utf8_unchecked(*v) };
let s: &str = unsafe { core::str::from_utf8_unchecked(v) };
// ... but this next conversion step will check that its
// _bytes_ are in the symbol-char range, which is a subset
// of utf-8, so we're only lying harmlessly.
Expand Down Expand Up @@ -455,7 +455,7 @@ impl TryFrom<SymbolSmall> for ScVal {
impl TryFrom<&SymbolSmall> for ScVal {
type Error = ConversionError;
fn try_from(s: &SymbolSmall) -> Result<Self, Self::Error> {
s.clone().try_into()
(*s).try_into()
}
}

Expand Down
4 changes: 2 additions & 2 deletions soroban-env-common/src/unimplemented_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ impl EnvBase for UnimplementedEnv {
unimplemented!()
}

fn string_new_from_slice<'a>(&self, _s: &'a str) -> Result<crate::StringObject, Self::Error> {
fn string_new_from_slice(&self, _s: &str) -> Result<crate::StringObject, Self::Error> {
unimplemented!()
}

fn symbol_new_from_slice<'a>(&self, _s: &'a str) -> Result<crate::SymbolObject, Self::Error> {
fn symbol_new_from_slice(&self, _s: &str) -> Result<crate::SymbolObject, Self::Error> {
unimplemented!()
}

Expand Down
29 changes: 13 additions & 16 deletions soroban-env-host/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl AuthorizedInvocation {
let sub_invocations_xdr = xdr_invocation.sub_invocations.into_vec();
let sub_invocations = sub_invocations_xdr
.into_iter()
.map(|i| AuthorizedInvocation::from_xdr(i))
.map(AuthorizedInvocation::from_xdr)
.collect::<Result<Vec<_>, _>>()?;
Ok(Self {
contract_id: xdr_invocation.contract_id,
Expand Down Expand Up @@ -433,7 +433,7 @@ impl AuthorizationManager {
// We could also make this included into the authorized stack to
// generalize all the host function invocations.
Frame::HostFunction(_) => return Ok(()),
Frame::Token(id, fn_name, _) => (id.metered_clone(&self.budget)?, fn_name.clone()),
Frame::Token(id, fn_name, _) => (id.metered_clone(&self.budget)?, *fn_name),
#[cfg(any(test, feature = "testutils"))]
Frame::TestContract(tc) => (tc.id.clone(), tc.func.clone()),
};
Expand Down Expand Up @@ -472,7 +472,7 @@ impl AuthorizationManager {
// Should only be called in the recording mode.
pub(crate) fn get_recorded_auth_payloads(&self) -> Result<Vec<RecordedAuthPayload>, HostError> {
match &self.mode {
AuthorizationMode::Enforcing => return Err(ScUnknownErrorCode::General.into()),
AuthorizationMode::Enforcing => Err(ScUnknownErrorCode::General.into()),
AuthorizationMode::Recording(_) => Ok(self
.trackers
.iter()
Expand Down Expand Up @@ -636,7 +636,7 @@ impl AuthorizationTracker {
false
};
let nonce = if !is_invoker {
Some(host.read_and_consume_nonce(&contract_id, &address)?)
Some(host.read_and_consume_nonce(contract_id, &address)?)
} else {
None
};
Expand Down Expand Up @@ -829,15 +829,13 @@ impl AuthorizationTracker {
break;
}
}
} else {
if !self.root_authorized_invocation.is_exhausted
&& &self.root_authorized_invocation.contract_id == contract_id
&& &self.root_authorized_invocation.function_name == function_name
&& &self.root_authorized_invocation.args == args
{
frame_index = Some(0);
self.root_authorized_invocation.is_exhausted = true;
}
} else if !self.root_authorized_invocation.is_exhausted
&& &self.root_authorized_invocation.contract_id == contract_id
&& &self.root_authorized_invocation.function_name == function_name
&& &self.root_authorized_invocation.args == args
{
frame_index = Some(0);
self.root_authorized_invocation.is_exhausted = true;
}
if frame_index.is_some() {
*self.invocation_id_in_call_stack.last_mut().unwrap() = frame_index;
Expand Down Expand Up @@ -875,7 +873,6 @@ impl AuthorizationTracker {
invocation: self.invocation_to_xdr(host.budget_ref())?,
nonce: self
.nonce
.clone()
.ok_or_else(|| host.err_general("unexpected missing nonce"))?,
});

Expand All @@ -892,7 +889,7 @@ impl AuthorizationTracker {
let payload = self.get_signature_payload(host)?;
match address {
ScAddress::Account(acc) => {
check_account_authentication(host, &acc, &payload, &self.signature_args)?;
check_account_authentication(host, acc, &payload, &self.signature_args)?;
}
ScAddress::Contract(acc_contract) => {
check_account_contract_auth(
Expand Down Expand Up @@ -984,7 +981,7 @@ impl Host {
self.with_mut_storage(|storage| storage.get(&nonce_key, self.budget_ref()))?;
match &entry.data {
LedgerEntryData::ContractData(ContractDataEntry { val, .. }) => match val {
ScVal::U64(val) => val.clone(),
ScVal::U64(val) => *val,
_ => {
return Err(self.err_general("unexpected nonce entry type"));
}
Expand Down
8 changes: 6 additions & 2 deletions soroban-env-host/src/budget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,14 +429,18 @@ impl Budget {
*t_iters = t_iters.saturating_add(iterations);
match (t_inputs, input) {
(None, None) => Ok(()),
(Some(t), Some(i)) => Ok(*t = t.saturating_add(i.saturating_mul(iterations))),
(Some(t), Some(i)) => {
*t = t.saturating_add(i.saturating_mul(iterations));
Ok(())
}
// TODO: improve error code "unexpected cost model input"
_ => Err(ScUnknownErrorCode::General.into()),
}
})?;
self.get_tracker_mut(ContractCostType::ChargeBudget, |(t_iters, _)| {
// we already know `ChargeBudget` has undefined input, so here we just add 1 iteration.
Ok(*t_iters = t_iters.saturating_add(1))
*t_iters = t_iters.saturating_add(1);
Ok(())
})?;

// do the actual budget charging
Expand Down
Loading

0 comments on commit 8952557

Please sign in to comment.