Repository: oasisprotocol/oasis-sdk
Pull request #33 (2021-03-09 12:08:45)

Deny some rust lints
This PR denies the following lints
* `unsafe_code` - for obvious reasons
*  `unreachable_pub` - to make sure everything `pub` is intentionally exported
* `rust_2018_idoms` - being current is nice


Patch:
diff --git a/runtime-sdk/src/context.rs b/runtime-sdk/src/context.rs
index 638533ee74..8b09d22352 100644
--- a/runtime-sdk/src/context.rs
+++ b/runtime-sdk/src/context.rs
@@ -53,7 +53,7 @@ pub struct DispatchContext<'a> {
 
 impl<'a> DispatchContext<'a> {
     /// Create a new dispatch context from the low-level runtime context.
-    pub(crate) fn from_runtime(ctx: &'a RuntimeContext, mkvs: &'a mut dyn mkvs::MKVS) -> Self {
+    pub(crate) fn from_runtime(ctx: &'a RuntimeContext<'_>, mkvs: &'a mut dyn mkvs::MKVS) -> Self {
         Self {
             mode: if ctx.check_only {
                 Mode::CheckTx
@@ -129,7 +129,7 @@ impl<'a> DispatchContext<'a> {
     /// Executes a function with the transaction-specific context set.
     pub fn with_tx<F, R>(&mut self, tx: transaction::Transaction, f: F) -> R
     where
-        F: FnOnce(TxContext, transaction::Call) -> R,
+        F: FnOnce(TxContext<'_, '_>, transaction::Call) -> R,
     {
         // Create a store wrapped by an overlay store so we can either rollback or commit.
         let store = storage::MKVSStore::new(self.io_ctx.clone(), &mut self.runtime_storage);
diff --git a/runtime-sdk/src/crypto/signature/secp256k1.rs b/runtime-sdk/src/crypto/signature/secp256k1.rs
index b57d233611..a91ae11488 100644
--- a/runtime-sdk/src/crypto/signature/secp256k1.rs
+++ b/runtime-sdk/src/crypto/signature/secp256k1.rs
@@ -77,7 +77,7 @@ impl<'de> serde::Deserialize<'de> for PublicKey {
         impl<'de> serde::de::Visitor<'de> for BytesVisitor {
             type Value = PublicKey;
 
-            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                 formatter.write_str("bytes expected")
             }
 
diff --git a/runtime-sdk/src/dispatcher.rs b/runtime-sdk/src/dispatcher.rs
index 0124135701..e921dcd253 100644
--- a/runtime-sdk/src/dispatcher.rs
+++ b/runtime-sdk/src/dispatcher.rs
@@ -92,7 +92,7 @@ impl<R: Runtime> Dispatcher<R> {
 
     fn dispatch_tx(
         &self,
-        ctx: &mut DispatchContext,
+        ctx: &mut DispatchContext<'_>,
         tx: types::transaction::Transaction,
     ) -> Result<DispatchResult, Error> {
         // Run pre-processing hooks.
@@ -128,7 +128,7 @@ impl<R: Runtime> Dispatcher<R> {
         Ok(result)
     }
 
-    fn check_tx(&self, ctx: &mut DispatchContext, tx: &[u8]) -> Result<CheckTxResult, Error> {
+    fn check_tx(&self, ctx: &mut DispatchContext<'_>, tx: &[u8]) -> Result<CheckTxResult, Error> {
         let tx = match self.decode_tx(&tx) {
             Ok(tx) => tx,
             Err(err) => {
@@ -160,7 +160,11 @@ impl<R: Runtime> Dispatcher<R> {
         }
     }
 
-    fn execute_tx(&self, ctx: &mut DispatchContext, tx: &[u8]) -> Result<ExecuteTxResult, Error> {
+    fn execute_tx(
+        &self,
+        ctx: &mut DispatchContext<'_>,
+        tx: &[u8],
+    ) -> Result<ExecuteTxResult, Error> {
         let tx = match self.decode_tx(&tx) {
             Ok(tx) => tx,
             Err(err) => {
@@ -179,7 +183,7 @@ impl<R: Runtime> Dispatcher<R> {
         })
     }
 
-    fn maybe_init_state(&self, ctx: &mut DispatchContext) {
+    fn maybe_init_state(&self, ctx: &mut DispatchContext<'_>) {
         R::migrate(ctx)
     }
 }
@@ -187,7 +191,7 @@ impl<R: Runtime> Dispatcher<R> {
 impl<R: Runtime> transaction::dispatcher::Dispatcher for Dispatcher<R> {
     fn execute_batch(
         &self,
-        ctx: transaction::Context,
+        ctx: transaction::Context<'_>,
         batch: &TxnBatch,
     ) -> Result<ExecuteBatchResult, RuntimeError> {
         // TODO: Get rid of StorageContext (pass mkvs in ctx).
@@ -218,7 +222,7 @@ impl<R: Runtime> transaction::dispatcher::Dispatcher for Dispatcher<R> {
 
     fn check_batch(
         &self,
-        ctx: transaction::Context,
+        ctx: transaction::Context<'_>,
         batch: &TxnBatch,
     ) -> Result<Vec<CheckTxResult>, RuntimeError> {
         // TODO: Get rid of StorageContext (pass mkvs in ctx).
@@ -244,7 +248,7 @@ impl<R: Runtime> transaction::dispatcher::Dispatcher for Dispatcher<R> {
 
     fn query(
         &self,
-        ctx: transaction::Context,
+        ctx: transaction::Context<'_>,
         method: &str,
         args: cbor::Value,
     ) -> Result<cbor::Value, RuntimeError> {
diff --git a/runtime-sdk/src/lib.rs b/runtime-sdk/src/lib.rs
index 46f17fdf71..d3ff970855 100644
--- a/runtime-sdk/src/lib.rs
+++ b/runtime-sdk/src/lib.rs
@@ -1,5 +1,7 @@
 //! Oasis runtime SDK.
 #![feature(const_fn)]
+#![deny(rust_2018_idioms, unreachable_pub)]
+#![forbid(unsafe_code)]
 
 pub mod context;
 pub mod crypto;
diff --git a/runtime-sdk/src/module.rs b/runtime-sdk/src/module.rs
index 83d19a0d27..d9844b42c1 100644
--- a/runtime-sdk/src/module.rs
+++ b/runtime-sdk/src/module.rs
@@ -19,7 +19,7 @@ pub struct CallableMethodInfo {
     pub name: &'static str,
 
     /// Method handler function.
-    pub handler: fn(&CallableMethodInfo, &mut TxContext, cbor::Value) -> CallResult,
+    pub handler: fn(&CallableMethodInfo, &mut TxContext<'_, '_>, cbor::Value) -> CallResult,
 }
 
 /// Metadata of a query method.
@@ -30,7 +30,7 @@ pub struct QueryMethodInfo {
     /// Method handler function.
     pub handler: fn(
         &QueryMethodInfo,
-        &mut DispatchContext,
+        &mut DispatchContext<'_>,
         cbor::Value,
     ) -> Result<cbor::Value, error::RuntimeError>,
 }
@@ -101,7 +101,7 @@ pub trait AuthHandler {
     ///
     /// Note that any signatures have already been verified.
     fn authenticate_tx(
-        _ctx: &mut DispatchContext,
+        _ctx: &mut DispatchContext<'_>,
         _tx: &Transaction,
     ) -> Result<(), modules::core::Error> {
         // Default implementation doesn't do any checks.
@@ -112,7 +112,7 @@ pub trait AuthHandler {
 #[impl_for_tuples(30)]
 impl AuthHandler for Tuple {
     fn authenticate_tx(
-        ctx: &mut DispatchContext,
+        ctx: &mut DispatchContext<'_>,
         tx: &Transaction,
     ) -> Result<(), modules::core::Error> {
         for_tuples!( #( Tuple::authenticate_tx(ctx, tx)?; )* );
@@ -129,7 +129,7 @@ pub trait MigrationHandler {
     ///
     /// Should return true in case metadata has been changed.
     fn init_or_migrate(
-        ctx: &mut DispatchContext,
+        ctx: &mut DispatchContext<'_>,
         meta: &mut modules::core::types::Metadata,
         genesis: &Self::Genesis,
     ) -> bool;
@@ -141,7 +141,7 @@ impl MigrationHandler for Tuple {
     for_tuples!( type Genesis = ( #( Tuple::Genesis ),* ); );
 
     fn init_or_migrate(
-        ctx: &mut DispatchContext,
+        ctx: &mut DispatchContext<'_>,
         meta: &mut modules::core::types::Metadata,
         genesis: &Self::Genesis,
     ) -> bool {
@@ -156,13 +156,13 @@ impl MigrationHandler for Tuple {
 pub trait BlockHandler {
     /// Perform any common actions at the start of the block (before any transactions have been
     /// executed).
-    fn begin_block(_ctx: &mut DispatchContext) {
+    fn begin_block(_ctx: &mut DispatchContext<'_>) {
         // Default implementation doesn't do anything.
     }
 
     /// Perform any common actions at the end of the block (after all transactions have been
     /// executed).
-    fn end_block(_ctx: &mut DispatchContext) {
+    fn end_block(_ctx: &mut DispatchContext<'_>) {
         // Default implementation doesn't do anything.
     }
 }
diff --git a/runtime-sdk/src/modules/accounts/mod.rs b/runtime-sdk/src/modules/accounts/mod.rs
index 3ed927d93f..d867cbdcb9 100644
--- a/runtime-sdk/src/modules/accounts/mod.rs
+++ b/runtime-sdk/src/modules/accounts/mod.rs
@@ -183,17 +183,25 @@ module!{
 pub trait API {
     /// Transfer an amount from one account to the other.
     fn transfer(
-        ctx: &mut TxContext,
+        ctx: &mut TxContext<'_, '_>,
         from: Address,
         to: Address,
         amount: &token::BaseUnits,
     ) -> Result<(), Error>;
 
     /// Mint new tokens, increasing the total supply.
-    fn mint(ctx: &mut TxContext, to: Address, amount: &token::BaseUnits) -> Result<(), Error>;
+    fn mint(
+        ctx: &mut TxContext<'_, '_>,
+        to: Address,
+        amount: &token::BaseUnits,
+    ) -> Result<(), Error>;
 
     /// Burn existing tokens, decreasing the total supply.
-    fn burn(ctx: &mut TxContext, from: Address, amount: &token::BaseUnits) -> Result<(), Error>;
+    fn burn(
+        ctx: &mut TxContext<'_, '_>,
+        from: Address,
+        amount: &token::BaseUnits,
+    ) -> Result<(), Error>;
 
     /// Fetch an account's current nonce.
     fn get_nonce<S: storage::Store>(state: S, address: Address) -> Result<u64, Error>;
@@ -296,7 +304,7 @@ impl Module {
 
 impl API for Module {
     fn transfer(
-        ctx: &mut TxContext,
+        ctx: &mut TxContext<'_, '_>,
         from: Address,
         to: Address,
         amount: &token::BaseUnits,
@@ -320,7 +328,11 @@ impl API for Module {
         Ok(())
     }
 
-    fn mint(ctx: &mut TxContext, to: Address, amount: &token::BaseUnits) -> Result<(), Error> {
+    fn mint(
+        ctx: &mut TxContext<'_, '_>,
+        to: Address,
+        amount: &token::BaseUnits,
+    ) -> Result<(), Error> {
         // Add to destination account.
         Self::add_amount(ctx.runtime_state(), to, amount)?;
 
@@ -330,7 +342,11 @@ impl API for Module {
         Ok(())
     }
 
-    fn burn(ctx: &mut TxContext, from: Address, amount: &token::BaseUnits) -> Result<(), Error> {
+    fn burn(
+        ctx: &mut TxContext<'_, '_>,
+        from: Address,
+        amount: &token::BaseUnits,
+    ) -> Result<(), Error> {
         // Remove from target account.
         Self::sub_amount(ctx.runtime_state(), from, amount)?;
 
@@ -363,7 +379,7 @@ impl API for Module {
 }
 
 impl Module {
-    fn tx_transfer(ctx: &mut TxContext, body: types::Transfer) -> Result<(), Error> {
+    fn tx_transfer(ctx: &mut TxContext<'_, '_>, body: types::Transfer) -> Result<(), Error> {
         // Reject transfers when they are disabled.
         if Self::params(ctx.runtime_state()).transfers_disabled {
             return Err(Error::Forbidden);
@@ -374,12 +390,12 @@ impl Module {
         Ok(())
     }
 
-    fn query_nonce(ctx: &mut DispatchContext, args: types::NonceQuery) -> Result<u64, Error> {
+    fn query_nonce(ctx: &mut DispatchContext<'_>, args: types::NonceQuery) -> Result<u64, Error> {
         Self::get_nonce(ctx.runtime_state(), args.address)
     }
 
     fn query_balances(
-        ctx: &mut DispatchContext,
+        ctx: &mut DispatchContext<'_>,
         args: types::BalancesQuery,
     ) -> Result<types::AccountBalances, Error> {
         Self::get_balances(ctx.runtime_state(), args.address)
@@ -389,7 +405,7 @@ impl Module {
 impl Module {
     fn _callable_transfer_handler(
         _mi: &CallableMethodInfo,
-        ctx: &mut TxContext,
+        ctx: &mut TxContext<'_, '_>,
         body: cbor::Value,
     ) -> CallResult {
         let result = || -> Result<cbor::Value, Error> {
@@ -404,7 +420,7 @@ impl Module {
 
     fn _query_nonce_handler(
         _mi: &QueryMethodInfo,
-        ctx: &mut DispatchContext,
+        ctx: &mut DispatchContext<'_>,
         args: cbor::Value,
     ) -> Result<cbor::Value, error::RuntimeError> {
         let args = cbor::from_value(args).map_err(|_| Error::InvalidArgument)?;
@@ -413,7 +429,7 @@ impl Module {
 
     fn _query_balances_handler(
         _mi: &QueryMethodInfo,
-        ctx: &mut DispatchContext,
+        ctx: &mut DispatchContext<'_>,
         args: cbor::Value,
     ) -> Result<cbor::Value, error::RuntimeError> {
         let args = cbor::from_value(args).map_err(|_| Error::InvalidArgument)?;
@@ -450,7 +466,7 @@ impl module::MethodRegistrationHandler for Module {
 
 impl Module {
     /// Initialize state from genesis.
-    fn init(ctx: &mut DispatchContext, genesis: &Genesis) {
+    fn init(ctx: &mut DispatchContext<'_>, genesis: &Genesis) {
         // Create accounts.
         let mut store = storage::PrefixStore::new(ctx.runtime_state(), &MODULE_NAME);
         let mut accounts =
@@ -505,7 +521,7 @@ impl Module {
     }
 
     /// Migrate state from a previous version.
-    fn migrate(_ctx: &mut DispatchContext, _from: u32) -> bool {
+    fn migrate(_ctx: &mut DispatchContext<'_>, _from: u32) -> bool {
         // No migrations currently supported.
         false
     }
@@ -515,7 +531,7 @@ impl module::MigrationHandler for Module {
     type Genesis = Genesis;
 
     fn init_or_migrate(
-        ctx: &mut DispatchContext,
+        ctx: &mut DispatchContext<'_>,
         meta: &mut modules::core::types::Metadata,
         genesis: &Self::Genesis,
     ) -> bool {
@@ -554,7 +570,7 @@ const CONTEXT_KEY_FEE_ACCUMULATOR: &str = "accounts.FeeAccumulator";
 
 impl module::AuthHandler for Module {
     fn authenticate_tx(
-        ctx: &mut DispatchContext,
+        ctx: &mut DispatchContext<'_>,
         tx: &Transaction,
     ) -> Result<(), modules::core::Error> {
         // Fetch information about each signer.
@@ -597,7 +613,7 @@ impl module::AuthHandler for Module {
 }
 
 impl module::BlockHandler for Module {
-    fn end_block(ctx: &mut DispatchContext) {
+    fn end_block(ctx: &mut DispatchContext<'_>) {
         // Determine the fees that are available for disbursement from the last block.
         let mut previous_fees = Self::get_balances(ctx.runtime_state(), *ADDRESS_FEE_ACCUMULATOR)
             .expect("get_balances must succeed")
diff --git a/runtime-sdk/src/runtime.rs b/runtime-sdk/src/runtime.rs
index 680831b5ca..99f93c3086 100644
--- a/runtime-sdk/src/runtime.rs
+++ b/runtime-sdk/src/runtime.rs
@@ -25,7 +25,7 @@ pub trait Runtime {
     fn genesis_state() -> <Self::Modules as MigrationHandler>::Genesis;
 
     /// Perform state migrations if required.
-    fn migrate(ctx: &mut DispatchContext) {
+    fn migrate(ctx: &mut DispatchContext<'_>) {
         let store = storage::TypedStore::new(storage::PrefixStore::new(
             ctx.runtime_state(),
             &modules::core::MODULE_NAME,
diff --git a/runtime-sdk/src/storage/overlay.rs b/runtime-sdk/src/storage/overlay.rs
index acee3c0a17..1687b33760 100644
--- a/runtime-sdk/src/storage/overlay.rs
+++ b/runtime-sdk/src/storage/overlay.rs
@@ -70,7 +70,7 @@ impl<S: Store> Store for OverlayStore<S> {
 }
 
 /// An iterator over the `OverlayStore`.
-pub struct OverlayStoreIterator<'store, S: Store> {
+pub(crate) struct OverlayStoreIterator<'store, S: Store> {
     store: &'store OverlayStore<S>,
 
     parent: Box<dyn mkvs::Iterator + 'store>,
diff --git a/runtime-sdk/src/storage/prefix.rs b/runtime-sdk/src/storage/prefix.rs
index 751c557bc0..53b8bd6402 100644
--- a/runtime-sdk/src/storage/prefix.rs
+++ b/runtime-sdk/src/storage/prefix.rs
@@ -39,7 +39,7 @@ impl<'store, S: Store> Store for PrefixStore<'store, S> {
 }
 
 /// An iterator over the `PrefixStore`.
-pub struct PrefixStoreIterator<'store> {
+pub(crate) struct PrefixStoreIterator<'store> {
     inner: Box<dyn mkvs::Iterator + 'store>,
     prefix: &'store [u8],
 }
diff --git a/runtime-sdk/src/storage/typed.rs b/runtime-sdk/src/storage/typed.rs
index 1bb267dd0b..066163961e 100644
--- a/runtime-sdk/src/storage/typed.rs
+++ b/runtime-sdk/src/storage/typed.rs
@@ -34,7 +34,7 @@ impl<S: Store> TypedStore<S> {
         self.parent.remove(key)
     }
 
-    pub fn iter<K: DecodableStoreKey, V: DeserializeOwned>(&self) -> TypedStoreIterator<K, V> {
+    pub fn iter<K: DecodableStoreKey, V: DeserializeOwned>(&self) -> TypedStoreIterator<'_, K, V> {
         TypedStoreIterator::new(self.parent.iter())
     }
 }
diff --git a/runtime-sdk/src/types/address.rs b/runtime-sdk/src/types/address.rs
index a73542f969..0b81037793 100644
--- a/runtime-sdk/src/types/address.rs
+++ b/runtime-sdk/src/types/address.rs
@@ -109,7 +109,7 @@ impl From<&'static str> for Address {
 }
 
 impl fmt::LowerHex for Address {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         for i in &self.0[..] {
             write!(f, "{:02x}", i)?;
         }
@@ -118,14 +118,14 @@ impl fmt::LowerHex for Address {
 }
 
 impl fmt::Debug for Address {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         write!(f, "{}", self.to_bech32())?;
         Ok(())
     }
 }
 
 impl fmt::Display for Address {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         write!(f, "{}", self.to_bech32())?;
         Ok(())
     }
@@ -155,7 +155,7 @@ impl<'de> serde::Deserialize<'de> for Address {
         impl<'de> serde::de::Visitor<'de> for BytesVisitor {
             type Value = Address;
 
-            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
+            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
                 formatter.write_str("bytes or string expected")
             }
 
diff --git a/runtime-sdk/src/types/token.rs b/runtime-sdk/src/types/token.rs
index 2cb96c3391..b5fade5a3e 100644
--- a/runtime-sdk/src/types/token.rs
+++ b/runtime-sdk/src/types/token.rs
@@ -27,7 +27,7 @@ impl Denomination {
 }
 
 impl fmt::Display for Denomination {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         if self.is_native() {
             write!(f, "<native>")?;
         } else {
@@ -87,7 +87,7 @@ impl BaseUnits {
 }
 
 impl fmt::Display for BaseUnits {
-    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
         write!(f, "{} {}", self.0, self.1)?;
         Ok(())
     }


Test patch:
diff --git a/runtime-sdk/src/modules/accounts/test.rs b/runtime-sdk/src/modules/accounts/test.rs
index ed84703b19..14447d1b73 100644
--- a/runtime-sdk/src/modules/accounts/test.rs
+++ b/runtime-sdk/src/modules/accounts/test.rs
@@ -140,7 +140,7 @@ fn test_init_2() {
     );
 }
 
-fn init_accounts(ctx: &mut DispatchContext) {
+fn init_accounts(ctx: &mut DispatchContext<'_>) {
     Accounts::init(
         ctx,
         &Genesis {
diff --git a/runtime-sdk/src/testing/mock.rs b/runtime-sdk/src/testing/mock.rs
index 54bc2e17a8..81eb455dd1 100644
--- a/runtime-sdk/src/testing/mock.rs
+++ b/runtime-sdk/src/testing/mock.rs
@@ -21,7 +21,7 @@ pub struct Mock {
 
 impl Mock {
     /// Create a new mock dispatch context.
-    pub fn create_ctx(&mut self) -> DispatchContext {
+    pub fn create_ctx(&mut self) -> DispatchContext<'_> {
         DispatchContext {
             mode: Mode::ExecuteTx,
             runtime_header: &self.runtime_header,