Skip to content

Commit e1cef62

Browse files
authored
Merge pull request #1050 from EnergySystemsModellingLab/clippy
Also lint test code with `clippy` and enable additional lints
2 parents 8da2c55 + 2d5d080 commit e1cef62

50 files changed

Lines changed: 380 additions & 431 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.cargo/config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[alias]
22
# Automatically fix clippy warnings (where possible)
3-
clipfix = "clippy --fix --allow-dirty --allow-staged"
3+
clipfix = "clippy --all-targets --fix --allow-dirty --allow-staged"

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ repos:
5151
rev: v1.0
5252
hooks:
5353
- id: clippy
54+
args: ["--all-targets", "--", "-D", "warnings"]
5455
- repo: https://github.com/astral-sh/ruff-pre-commit
5556
rev: v0.11.6
5657
hooks:

Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ built = {version = "0.8.0", features = ["chrono", "git2"]}
5050
# Disallow lints from "all" and "pedantic" groups by default
5151
all = {level = "deny", priority = -1}
5252
pedantic = {level = "deny", priority = -1}
53+
# Extra lints to disallow
54+
redundant_test_prefix = "deny"
55+
assertions_on_result_states = "deny"
56+
get_unwrap = "deny"
57+
if_then_some_else_none = "deny"
58+
renamed_function_params = "deny"
59+
string_slice = "deny"
60+
dbg_macro = "deny"
61+
infinite_loop = "deny"
62+
integer_division = "deny"
63+
needless_raw_strings = "deny"
64+
redundant_type_annotations = "deny"
65+
return_and_then = "deny"
66+
suspicious_xor_used_as_pow = "deny"
5367
# Whitelist some lints from "pedantic" group
5468
similar_names = "allow"
5569
must_use_candidate = "allow"

src/asset.rs

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,14 +1305,24 @@ mod tests {
13051305
ActivityPerCapacity, Capacity, Dimensionless, FlowPerActivity, MoneyPerActivity,
13061306
MoneyPerCapacity, MoneyPerCapacityPerYear, MoneyPerFlow,
13071307
};
1308+
use float_cmp::assert_approx_eq;
13081309
use indexmap::indexmap;
13091310
use itertools::{Itertools, assert_equal};
13101311
use rstest::{fixture, rstest};
13111312
use std::iter;
13121313
use std::rc::Rc;
13131314

1315+
/// Number of expected children for divisible asset
1316+
#[allow(clippy::cast_possible_truncation)]
1317+
#[allow(clippy::cast_sign_loss)]
1318+
fn expected_children_for_divisible(asset: &Asset) -> usize {
1319+
(asset.capacity / asset.process.unit_size.expect("Asset is not divisible"))
1320+
.value()
1321+
.ceil() as usize
1322+
}
1323+
13141324
#[rstest]
1315-
fn test_get_input_cost_from_prices(
1325+
fn get_input_cost_from_prices_works(
13161326
region_id: RegionID,
13171327
svd_commodity: Commodity,
13181328
mut process: Process,
@@ -1341,15 +1351,15 @@ mod tests {
13411351
// Call function
13421352
let cost = asset.get_input_cost_from_prices(&input_prices, &time_slice);
13431353
// Should be -coeff * price = -(-2.0) * 3.0 = 6.0
1344-
assert_eq!(cost.0, 6.0);
1354+
assert_approx_eq!(MoneyPerActivity, cost, MoneyPerActivity(6.0));
13451355
}
13461356

13471357
#[rstest]
13481358
#[case(Capacity(0.01))]
13491359
#[case(Capacity(0.5))]
13501360
#[case(Capacity(1.0))]
13511361
#[case(Capacity(100.0))]
1352-
fn test_asset_new_valid(process: Process, #[case] capacity: Capacity) {
1362+
fn asset_new_valid(process: Process, #[case] capacity: Capacity) {
13531363
let agent_id = AgentID("agent1".into());
13541364
let region_id = RegionID("GBR".into());
13551365
let asset = Asset::new_future(agent_id, process.into(), region_id, capacity, 2015).unwrap();
@@ -1363,7 +1373,7 @@ mod tests {
13631373
#[case(Capacity(f64::NAN))]
13641374
#[case(Capacity(f64::INFINITY))]
13651375
#[case(Capacity(f64::NEG_INFINITY))]
1366-
fn test_asset_new_invalid_capacity(process: Process, #[case] capacity: Capacity) {
1376+
fn asset_new_invalid_capacity(process: Process, #[case] capacity: Capacity) {
13671377
let agent_id = AgentID("agent1".into());
13681378
let region_id = RegionID("GBR".into());
13691379
assert_error!(
@@ -1373,7 +1383,7 @@ mod tests {
13731383
}
13741384

13751385
#[rstest]
1376-
fn test_asset_new_invalid_commission_year(process: Process) {
1386+
fn asset_new_invalid_commission_year(process: Process) {
13771387
let agent_id = AgentID("agent1".into());
13781388
let region_id = RegionID("GBR".into());
13791389
assert_error!(
@@ -1383,7 +1393,7 @@ mod tests {
13831393
}
13841394

13851395
#[rstest]
1386-
fn test_asset_new_invalid_region(process: Process) {
1396+
fn asset_new_invalid_region(process: Process) {
13871397
let agent_id = AgentID("agent1".into());
13881398
let region_id = RegionID("FRA".into());
13891399
assert_error!(
@@ -1466,7 +1476,7 @@ mod tests {
14661476
}
14671477

14681478
#[rstest]
1469-
fn test_asset_get_activity_per_capacity_limits(
1479+
fn asset_get_activity_per_capacity_limits(
14701480
asset_with_activity_limits: Asset,
14711481
time_slice: TimeSliceID,
14721482
) {
@@ -1478,18 +1488,15 @@ mod tests {
14781488
}
14791489

14801490
#[rstest]
1481-
fn test_divide_asset(asset_divisible: Asset) {
1491+
fn divide_asset_works(asset_divisible: Asset) {
14821492
assert!(
14831493
asset_divisible.is_divisible(),
14841494
"Divisbile asset cannot be divided!"
14851495
);
14861496

14871497
// Check number of children
14881498
let children = asset_divisible.divide_asset();
1489-
let expected_children = (asset_divisible.capacity
1490-
/ asset_divisible.process.unit_size.unwrap())
1491-
.value()
1492-
.ceil() as usize;
1499+
let expected_children = expected_children_for_divisible(&asset_divisible);
14931500
assert_eq!(
14941501
children.len(),
14951502
expected_children,
@@ -1502,14 +1509,14 @@ mod tests {
15021509
assert!(
15031510
child.capacity <= max_child_capacity,
15041511
"Child capacity is too large!"
1505-
)
1512+
);
15061513
}
15071514
let children_capacity: Capacity = children.iter().map(|a| a.capacity).sum();
15081515
assert_eq!(asset_divisible.capacity, children_capacity);
15091516
}
15101517

15111518
#[rstest]
1512-
fn test_asset_pool_new(asset_pool: AssetPool) {
1519+
fn asset_pool_new(asset_pool: AssetPool) {
15131520
// Should be in order of commission year
15141521
assert!(asset_pool.active.is_empty());
15151522
assert!(asset_pool.future.len() == 2);
@@ -1518,33 +1525,30 @@ mod tests {
15181525
}
15191526

15201527
#[rstest]
1521-
fn test_asset_pool_commission_new1(mut asset_pool: AssetPool) {
1528+
fn asset_pool_commission_new1(mut asset_pool: AssetPool) {
15221529
// Asset to be commissioned in this year
15231530
asset_pool.commission_new(2010);
15241531
assert_equal(asset_pool.iter_active(), iter::once(&asset_pool.active[0]));
15251532
}
15261533

15271534
#[rstest]
1528-
fn test_asset_pool_commission_new2(mut asset_pool: AssetPool) {
1535+
fn asset_pool_commission_new2(mut asset_pool: AssetPool) {
15291536
// Commission year has passed
15301537
asset_pool.commission_new(2011);
15311538
assert_equal(asset_pool.iter_active(), iter::once(&asset_pool.active[0]));
15321539
}
15331540

15341541
#[rstest]
1535-
fn test_asset_pool_commission_new3(mut asset_pool: AssetPool) {
1542+
fn asset_pool_commission_new3(mut asset_pool: AssetPool) {
15361543
// Nothing to commission for this year
15371544
asset_pool.commission_new(2000);
15381545
assert!(asset_pool.iter_active().next().is_none()); // no active assets
15391546
}
15401547

15411548
#[rstest]
1542-
fn test_asset_pool_commission_new_divisible(asset_divisible: Asset) {
1549+
fn asset_pool_commission_new_divisible(asset_divisible: Asset) {
15431550
let commision_year = asset_divisible.commission_year;
1544-
let expected_children = (asset_divisible.capacity
1545-
/ asset_divisible.process.unit_size.unwrap())
1546-
.value()
1547-
.ceil() as usize;
1551+
let expected_children = expected_children_for_divisible(&asset_divisible);
15481552
let mut asset_pool = AssetPool::new(vec![asset_divisible.clone()]);
15491553
assert!(asset_pool.active.is_empty());
15501554
asset_pool.commission_new(commision_year);
@@ -1558,7 +1562,7 @@ mod tests {
15581562
}
15591563

15601564
#[rstest]
1561-
fn test_asset_pool_commission_already_decommissioned(asset: Asset) {
1565+
fn asset_pool_commission_already_decommissioned(asset: Asset) {
15621566
let year = asset.max_decommission_year();
15631567
let mut asset_pool = AssetPool::new(vec![asset]);
15641568
assert!(asset_pool.active.is_empty());
@@ -1567,7 +1571,7 @@ mod tests {
15671571
}
15681572

15691573
#[rstest]
1570-
fn test_asset_pool_decommission_old(mut asset_pool: AssetPool) {
1574+
fn asset_pool_decommission_old(mut asset_pool: AssetPool) {
15711575
asset_pool.commission_new(2020);
15721576
assert!(asset_pool.future.is_empty());
15731577
assert_eq!(asset_pool.active.len(), 2);
@@ -1593,14 +1597,14 @@ mod tests {
15931597
}
15941598

15951599
#[rstest]
1596-
fn test_asset_pool_get(mut asset_pool: AssetPool) {
1600+
fn asset_pool_get(mut asset_pool: AssetPool) {
15971601
asset_pool.commission_new(2020);
15981602
assert_eq!(asset_pool.get(AssetID(0)), Some(&asset_pool.active[0]));
15991603
assert_eq!(asset_pool.get(AssetID(1)), Some(&asset_pool.active[1]));
16001604
}
16011605

16021606
#[rstest]
1603-
fn test_asset_pool_extend_empty(mut asset_pool: AssetPool) {
1607+
fn asset_pool_extend_empty(mut asset_pool: AssetPool) {
16041608
// Start with commissioned assets
16051609
asset_pool.commission_new(2020);
16061610
let original_count = asset_pool.active.len();
@@ -1612,7 +1616,7 @@ mod tests {
16121616
}
16131617

16141618
#[rstest]
1615-
fn test_asset_pool_extend_existing_assets(mut asset_pool: AssetPool) {
1619+
fn asset_pool_extend_existing_assets(mut asset_pool: AssetPool) {
16161620
// Start with some commissioned assets
16171621
asset_pool.commission_new(2020);
16181622
assert_eq!(asset_pool.active.len(), 2);
@@ -1627,7 +1631,7 @@ mod tests {
16271631
}
16281632

16291633
#[rstest]
1630-
fn test_asset_pool_extend_new_assets(mut asset_pool: AssetPool, process: Process) {
1634+
fn asset_pool_extend_new_assets(mut asset_pool: AssetPool, process: Process) {
16311635
// Start with some commissioned assets
16321636
asset_pool.commission_new(2020);
16331637
let original_count = asset_pool.active.len();
@@ -1672,10 +1676,7 @@ mod tests {
16721676
}
16731677

16741678
#[rstest]
1675-
fn test_asset_pool_extend_new_divisible_assets(
1676-
mut asset_pool: AssetPool,
1677-
mut process: Process,
1678-
) {
1679+
fn asset_pool_extend_new_divisible_assets(mut asset_pool: AssetPool, mut process: Process) {
16791680
// Start with some commissioned assets
16801681
asset_pool.commission_new(2020);
16811682
let original_count = asset_pool.active.len();
@@ -1694,16 +1695,13 @@ mod tests {
16941695
.unwrap()
16951696
.into(),
16961697
];
1697-
let expected_children = (new_assets[0].capacity / new_assets[0].process.unit_size.unwrap())
1698-
.value()
1699-
.ceil() as usize;
1700-
1698+
let expected_children = expected_children_for_divisible(&new_assets[0]);
17011699
asset_pool.extend(new_assets);
17021700
assert_eq!(asset_pool.active.len(), original_count + expected_children);
17031701
}
17041702

17051703
#[rstest]
1706-
fn test_asset_pool_extend_mixed_assets(mut asset_pool: AssetPool, process: Process) {
1704+
fn asset_pool_extend_mixed_assets(mut asset_pool: AssetPool, process: Process) {
17071705
// Start with some commissioned assets
17081706
asset_pool.commission_new(2020);
17091707

@@ -1736,7 +1734,7 @@ mod tests {
17361734
}
17371735

17381736
#[rstest]
1739-
fn test_asset_pool_extend_maintains_sort_order(mut asset_pool: AssetPool, process: Process) {
1737+
fn asset_pool_extend_maintains_sort_order(mut asset_pool: AssetPool, process: Process) {
17401738
// Start with some commissioned assets
17411739
asset_pool.commission_new(2020);
17421740

@@ -1774,7 +1772,7 @@ mod tests {
17741772
}
17751773

17761774
#[rstest]
1777-
fn test_asset_pool_extend_no_duplicates_expected(mut asset_pool: AssetPool) {
1775+
fn asset_pool_extend_no_duplicates_expected(mut asset_pool: AssetPool) {
17781776
// Start with some commissioned assets
17791777
asset_pool.commission_new(2020);
17801778
let original_count = asset_pool.active.len();
@@ -1792,7 +1790,7 @@ mod tests {
17921790
}
17931791

17941792
#[rstest]
1795-
fn test_asset_pool_extend_increments_next_id(mut asset_pool: AssetPool, process: Process) {
1793+
fn asset_pool_extend_increments_next_id(mut asset_pool: AssetPool, process: Process) {
17961794
// Start with some commissioned assets
17971795
asset_pool.commission_new(2020);
17981796
assert_eq!(asset_pool.next_id, 2); // Should be 2 after commissioning 2 assets
@@ -1829,7 +1827,7 @@ mod tests {
18291827
}
18301828

18311829
#[rstest]
1832-
fn test_asset_pool_mothball_unretained(mut asset_pool: AssetPool) {
1830+
fn asset_pool_mothball_unretained(mut asset_pool: AssetPool) {
18331831
// Commission some assets
18341832
asset_pool.commission_new(2020);
18351833
assert_eq!(asset_pool.active.len(), 2);
@@ -1848,7 +1846,7 @@ mod tests {
18481846
}
18491847

18501848
#[rstest]
1851-
fn test_asset_pool_decommission_unused(mut asset_pool: AssetPool) {
1849+
fn asset_pool_decommission_unused(mut asset_pool: AssetPool) {
18521850
// Commission some assets
18531851
asset_pool.commission_new(2020);
18541852
assert_eq!(asset_pool.active.len(), 2);
@@ -1875,7 +1873,7 @@ mod tests {
18751873
}
18761874

18771875
#[rstest]
1878-
fn test_asset_pool_decommission_if_not_active_none_active(mut asset_pool: AssetPool) {
1876+
fn asset_pool_decommission_if_not_active_none_active(mut asset_pool: AssetPool) {
18791877
// Commission some assets
18801878
asset_pool.commission_new(2020);
18811879
let all_assets = asset_pool.active.clone();
@@ -1896,7 +1894,7 @@ mod tests {
18961894

18971895
#[rstest]
18981896
#[should_panic(expected = "Cannot mothball asset that has not been commissioned")]
1899-
fn test_asset_pool_decommission_if_not_active_non_commissioned_asset(
1897+
fn asset_pool_decommission_if_not_active_non_commissioned_asset(
19001898
mut asset_pool: AssetPool,
19011899
process: Process,
19021900
) {
@@ -1916,7 +1914,7 @@ mod tests {
19161914
}
19171915

19181916
#[rstest]
1919-
fn test_asset_commission(process: Process) {
1917+
fn asset_commission(process: Process) {
19201918
// Test successful commissioning of Future asset
19211919
let process_rc = Rc::new(process);
19221920
let mut asset1 = Asset::new_future(
@@ -1948,7 +1946,7 @@ mod tests {
19481946
#[rstest]
19491947
#[case::commission_during_process_lifetime(2024, 2024)]
19501948
#[case::decommission_after_process_lifetime_ends(2026, 2025)]
1951-
fn test_asset_decommission(
1949+
fn asset_decommission(
19521950
#[case] requested_decommission_year: u32,
19531951
#[case] expected_decommission_year: u32,
19541952
process: Process,
@@ -1978,7 +1976,7 @@ mod tests {
19781976
#[case::decommission_before_predefined_max_year(2024, 2024, Some(2025))]
19791977
#[case::decommission_during_process_lifetime_end_no_max_year(2024, 2024, None)]
19801978
#[case::decommission_after_process_lifetime_end_no_max_year(2026, 2025, None)]
1981-
fn test_asset_decommission_with_max_decommission_year_predefined(
1979+
fn asset_decommission_with_max_decommission_year_predefined(
19821980
#[case] requested_decommission_year: u32,
19831981
#[case] expected_decommission_year: u32,
19841982
#[case] max_decommission_year: Option<u32>,
@@ -2007,15 +2005,15 @@ mod tests {
20072005

20082006
#[rstest]
20092007
#[should_panic(expected = "Assets with state Candidate cannot be commissioned")]
2010-
fn test_commission_wrong_states(process: Process) {
2008+
fn commission_wrong_states(process: Process) {
20112009
let mut asset =
20122010
Asset::new_candidate(process.into(), "GBR".into(), Capacity(1.0), 2020).unwrap();
20132011
asset.commission(AssetID(1), None, "");
20142012
}
20152013

20162014
#[rstest]
20172015
#[should_panic(expected = "Cannot decommission an asset that hasn't been commissioned")]
2018-
fn test_decommission_wrong_state(process: Process) {
2016+
fn decommission_wrong_state(process: Process) {
20192017
let mut asset =
20202018
Asset::new_candidate(process.into(), "GBR".into(), Capacity(1.0), 2020).unwrap();
20212019
asset.decommission(2025, "");

0 commit comments

Comments
 (0)