Skip to content

Commit 5ea4017

Browse files
Tenny150Husten150
andauthored
fix(fmt): tune rustfmt rules for ink! macros (#852)
* fix(fmt): tune rustfmt rules for ink! macros (#783) - Add rust-toolchain.toml pinning nightly channel with rustfmt and clippy components so every contributor uses the same formatter. - Add rustfmt.toml with format_macro_matchers = true (nightly-only) so #[ink::contract], #[ink::test] and non_reentrant! macro calls are fully formatted. Also sets format_macro_bodies, edition 2021, max_width 100 and a handful of import-ordering options. - Update smoke-ci.yml: install nightly rustfmt in a dedicated step and run 'cargo +nightly fmt --check' instead of stable fmt. Stable toolchain is still used for clippy and tests. Closes #783 * fix(ci): install nightly rustfmt before fmt check to fix CI failure The rust-toolchain.toml file alone does not auto-install rustfmt for nightly when cargo +nightly is invoked in CI after the stable toolchain has been explicitly installed. Add an explicit dtolnay/rust-toolchain@nightly step with the rustfmt component before running cargo +nightly fmt --check. * fix(fmt): apply cargo +nightly fmt with format_macro_matchers --------- Co-authored-by: husten150 <hustenisama@gmail.com>
1 parent be7f6ef commit 5ea4017

31 files changed

Lines changed: 104 additions & 74 deletions

File tree

.github/workflows/smoke-ci.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@ jobs:
2626
grep -Eq '^/contracts/lending/\s+@MettaChain/lending$' .github/CODEOWNERS
2727
grep -Eq '^/contracts/oracle/\s+@MettaChain/oracle$' .github/CODEOWNERS
2828
29-
- name: Install Stable Rust Toolchain
29+
- name: Install Nightly Rust Toolchain (for fmt)
30+
uses: dtolnay/rust-toolchain@nightly
31+
with:
32+
components: rustfmt
33+
34+
- name: Install Stable Rust Toolchain (for clippy & test)
3035
uses: dtolnay/rust-toolchain@stable
3136
with:
32-
components: rustfmt, clippy
37+
components: clippy
3338

3439
- name: Cache Cargo Build Artifacts
3540
uses: actions/cache@v4
@@ -44,8 +49,8 @@ jobs:
4449
restore-keys: |
4550
${{ runner.os }}-cargo-smoke-
4651
47-
- name: Check Code Formatting Style (fmt)
48-
run: cargo fmt --check
52+
- name: Check Code Formatting Style (nightly fmt)
53+
run: cargo +nightly fmt --check
4954

5055
- name: Execute Static Analysis Compiler Lints (clippy)
5156
run: cargo clippy --all-targets --all-features -- -D warnings

contracts/bridge/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ use scale_info::prelude::vec::Vec;
1818

1919
#[ink::contract]
2020
mod bridge {
21-
use super::*;
2221
use propchain_traits::{non_reentrant, ReentrancyError, ReentrancyGuard};
2322

23+
use super::*;
24+
2425
include!("errors.rs");
2526

2627
/// Maximum number of entries kept in [`PropertyBridge::pause_audit_log`].

contracts/compliance_registry/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
dead_code
77
)]
88

9-
use propchain_traits::ComplianceChecker;
10-
use propchain_traits::*;
9+
use propchain_traits::{ComplianceChecker, *};
1110

1211
#[ink::contract]
1312
mod compliance_registry {
14-
use super::*;
1513
use ink::prelude::vec::Vec;
1614
use ink::storage::Mapping;
1715
use propchain_traits::ComplianceOperation;
1816

17+
use super::*;
18+
1919
/// Represents the verification status of a user
2020
#[derive(Debug, PartialEq, Eq, Clone, Copy, scale::Encode, scale::Decode)]
2121
#[cfg_attr(

contracts/crowdfunding/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ use ink::storage::Mapping;
1010

1111
#[ink::contract]
1212
mod propchain_crowdfunding {
13+
use ink::prelude::string::String;
14+
use ink::prelude::vec::Vec;
15+
1316
use super::*;
14-
use ink::prelude::{string::String, vec::Vec};
1517

1618
#[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)]
1719
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
@@ -1428,11 +1430,12 @@ pub use crate::propchain_crowdfunding::{CrowdfundingError, RealEstateCrowdfundin
14281430

14291431
#[cfg(test)]
14301432
mod tests {
1431-
use super::*;
14321433
#[allow(unused_imports)]
14331434
use ink::env::{test, DefaultEnvironment};
14341435
use propchain_crowdfunding::{CampaignStatus, CrowdfundingError, RealEstateCrowdfunding};
14351436

1437+
use super::*;
1438+
14361439
fn setup() -> RealEstateCrowdfunding {
14371440
let accounts = test::default_accounts::<DefaultEnvironment>();
14381441
test::set_caller::<DefaultEnvironment>(accounts.alice);

contracts/dex/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
clippy::needless_borrows_for_generic_args
77
)]
88

9-
use ink::prelude::{string::String, vec::Vec};
9+
use ink::prelude::string::String;
10+
use ink::prelude::vec::Vec;
1011
use ink::storage::Mapping;
1112
use propchain_traits::*;
1213

1314
#[ink::contract]
1415
mod dex {
15-
use super::*;
1616
use propchain_traits::{non_reentrant, ReentrancyError, ReentrancyGuard};
1717

18+
use super::*;
19+
1820
const BIPS_DENOMINATOR: u128 = 10_000;
1921
const REWARD_PRECISION: u128 = 1_000_000_000;
2022

contracts/factory/src/tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::contract_factory::*;
21
use ink::env::test;
32
use ink::primitives::Hash;
43

4+
use crate::contract_factory::*;
5+
56
#[ink::test]
67
fn test_factory_initialization() {
78
let factory = ContractFactory::new();

contracts/fractional/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
mod fractional {
1111
use ink::prelude::vec::Vec;
1212
use ink::storage::Mapping;
13-
use propchain_traits;
14-
use propchain_traits::{non_reentrant, ReentrancyError, ReentrancyGuard};
13+
use propchain_traits::{self, non_reentrant, ReentrancyError, ReentrancyGuard};
1514

1615
#[derive(
1716
Debug,
@@ -1196,9 +1195,10 @@ mod fractional {
11961195

11971196
#[cfg(test)]
11981197
mod tests {
1199-
use super::*;
12001198
use ink::env::test;
12011199

1200+
use super::*;
1201+
12021202
fn alice() -> AccountId {
12031203
test::default_accounts::<ink::env::DefaultEnvironment>().alice
12041204
}
@@ -1894,9 +1894,10 @@ mod fractional {
18941894

18951895
#[cfg(test)]
18961896
mod fractional_admin_rotation_tests {
1897-
use super::*;
18981897
use ink::env::{test, DefaultEnvironment};
18991898

1899+
use super::*;
1900+
19001901
fn setup() -> Fractional {
19011902
let accounts = test::default_accounts::<DefaultEnvironment>();
19021903
test::set_caller::<DefaultEnvironment>(accounts.alice);

contracts/hello-world/src/test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#![cfg(test)]
22
#![allow(dead_code, unused_imports, deprecated)]
33

4-
use super::*;
54
use soroban_sdk::Env;
65

6+
use super::*;
7+
78
#[test]
89
fn test_loan_lifecycle() {
910
let env = Env::default();

contracts/insurance/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ mod fraud_detection;
2525
/// Decentralized Property Insurance Platform
2626
#[ink::contract]
2727
mod propchain_insurance {
28-
use super::*;
29-
use ink::prelude::{string::String, vec::Vec};
28+
use ink::prelude::string::String;
29+
use ink::prelude::vec::Vec;
3030
use propchain_traits::{non_reentrant, ReentrancyError, ReentrancyGuard};
3131

32+
use super::*;
33+
3234
// Error types extracted to errors.rs (Issue #101)
3335
include!("errors.rs");
3436

@@ -84,10 +86,9 @@ mod propchain_insurance {
8486
}
8587

8688
// Risk Assessment Model (Task #254)
87-
use crate::risk_assessment::risk_model;
88-
8989
// Fraud Detection System (Task #258)
9090
use crate::fraud_detection::fraud_detection;
91+
use crate::risk_assessment::risk_model;
9192
// Premium calculation engine
9293
include!("premium_engine.rs");
9394

contracts/insurance/src/tests.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
// ============================================================================
1919
#[cfg(test)]
2020
mod reinsurance_stats_derives {
21+
use ink::env::{test, DefaultEnvironment};
22+
2123
use crate::propchain_insurance::{
2224
CoverageType, PropertyInsurance, ReinsuranceStats, ReinsuranceTreatyType,
2325
};
24-
use ink::env::{test, DefaultEnvironment};
2526

2627
fn setup() -> PropertyInsurance {
2728
let accounts = test::default_accounts::<DefaultEnvironment>();
@@ -1701,9 +1702,10 @@ mod insurance_tests {
17011702

17021703
#[cfg(test)]
17031704
mod circuit_breaker_tests {
1704-
use crate::propchain_insurance::{CoverageType, InsuranceError, PropertyInsurance};
17051705
use ink::env::{test, DefaultEnvironment};
17061706

1707+
use crate::propchain_insurance::{CoverageType, InsuranceError, PropertyInsurance};
1708+
17071709
fn setup_with_pool() -> (PropertyInsurance, u64) {
17081710
let accounts = test::default_accounts::<DefaultEnvironment>();
17091711
test::set_caller::<DefaultEnvironment>(accounts.alice);
@@ -1797,9 +1799,10 @@ mod circuit_breaker_tests {
17971799

17981800
#[cfg(test)]
17991801
mod insurance_admin_rotation_tests {
1800-
use crate::propchain_insurance::{InsuranceError, PropertyInsurance};
18011802
use ink::env::{test, DefaultEnvironment};
18021803

1804+
use crate::propchain_insurance::{InsuranceError, PropertyInsurance};
1805+
18031806
fn setup() -> PropertyInsurance {
18041807
let accounts = test::default_accounts::<DefaultEnvironment>();
18051808
test::set_caller::<DefaultEnvironment>(accounts.alice);

0 commit comments

Comments
 (0)