From fada5cafd7228834d473de8fd793392955e7e423 Mon Sep 17 00:00:00 2001 From: James Tomlinson Date: Wed, 17 Jul 2024 12:25:20 +0100 Subject: [PATCH] chore: Fix various Clippy warnings. (#218) --- pywr-core/src/metric.rs | 2 +- pywr-core/src/models/multi.rs | 1 + pywr-core/src/network.rs | 8 +-- pywr-core/src/parameters/mod.rs | 55 ++++++++----------- pywr-core/src/solvers/highs/mod.rs | 13 ++--- pywr-core/src/solvers/highs/settings.rs | 10 +--- pywr-core/src/state.rs | 2 +- pywr-core/src/test_utils.rs | 2 +- .../src/nodes/water_treatment_works.rs | 15 ++--- pywr-schema/src/parameters/mod.rs | 2 +- 10 files changed, 42 insertions(+), 68 deletions(-) diff --git a/pywr-core/src/metric.rs b/pywr-core/src/metric.rs index 523b6cbc..8f1df5bb 100644 --- a/pywr-core/src/metric.rs +++ b/pywr-core/src/metric.rs @@ -42,7 +42,7 @@ impl SimpleMetricF64 { SimpleMetricF64::ParameterValue(idx) => Ok(values.get_simple_parameter_f64(*idx)?), SimpleMetricF64::IndexParameterValue(idx) => Ok(values.get_simple_parameter_usize(*idx)? as f64), SimpleMetricF64::MultiParameterValue((idx, key)) => Ok(values.get_simple_multi_parameter_f64(*idx, key)?), - SimpleMetricF64::Constant(m) => m.get_value(&values.get_constant_values()), + SimpleMetricF64::Constant(m) => m.get_value(values.get_constant_values()), } } } diff --git a/pywr-core/src/models/multi.rs b/pywr-core/src/models/multi.rs index 316c572b..a9568338 100644 --- a/pywr-core/src/models/multi.rs +++ b/pywr-core/src/models/multi.rs @@ -313,6 +313,7 @@ impl MultiNetworkModel { /// Calculate inter-model parameters for the given scenario index. /// /// +#[allow(clippy::too_many_arguments)] // This function is not too unreadable with 8 arguments. fn compute_inter_network_transfers( timestep: &Timestep, scenario_index: &ScenarioIndex, diff --git a/pywr-core/src/network.rs b/pywr-core/src/network.rs index f0f55ca7..c9627270 100644 --- a/pywr-core/src/network.rs +++ b/pywr-core/src/network.rs @@ -1331,9 +1331,7 @@ impl Network { &mut self, parameter: Box>, ) -> Result, PywrError> { - let parameter_index = self.parameters.add_simple_f64(parameter)?; - - Ok(parameter_index.into()) + self.parameters.add_simple_f64(parameter) } /// Add a [`parameters::ConstParameter`] to the network @@ -1341,9 +1339,7 @@ impl Network { &mut self, parameter: Box>, ) -> Result, PywrError> { - let parameter_index = self.parameters.add_const_f64(parameter)?; - - Ok(parameter_index.into()) + self.parameters.add_const_f64(parameter) } /// Add a `parameters::IndexParameter` to the network diff --git a/pywr-core/src/parameters/mod.rs b/pywr-core/src/parameters/mod.rs index 9e67e914..7dd318d6 100644 --- a/pywr-core/src/parameters/mod.rs +++ b/pywr-core/src/parameters/mod.rs @@ -884,7 +884,7 @@ impl ParameterCollection { } match parameter.try_into_simple() { - Some(simple) => self.add_simple_f64(simple).map(|idx| idx.into()), + Some(simple) => self.add_simple_f64(simple), None => { let index = GeneralParameterIndex::new(self.general_f64.len()); self.general_f64.push(parameter); @@ -902,7 +902,7 @@ impl ParameterCollection { } match parameter.try_into_const() { - Some(constant) => self.add_const_f64(constant).map(|idx| idx.into()), + Some(constant) => self.add_const_f64(constant), None => { let index = SimpleParameterIndex::new(self.simple_f64.len()); @@ -951,25 +951,22 @@ impl ParameterCollection { .general_f64 .iter() .position(|p| p.meta().name == name) - .map(|idx| GeneralParameterIndex::new(idx)) + .map(GeneralParameterIndex::new) { Some(idx.into()) } else if let Some(idx) = self .simple_f64 .iter() .position(|p| p.meta().name == name) - .map(|idx| SimpleParameterIndex::new(idx)) - { - Some(idx.into()) - } else if let Some(idx) = self - .constant_f64 - .iter() - .position(|p| p.meta().name == name) - .map(|idx| ConstParameterIndex::new(idx)) + .map(SimpleParameterIndex::new) { Some(idx.into()) } else { - None + self.constant_f64 + .iter() + .position(|p| p.meta().name == name) + .map(ConstParameterIndex::new) + .map(|idx| idx.into()) } } @@ -1047,25 +1044,22 @@ impl ParameterCollection { .general_usize .iter() .position(|p| p.meta().name == name) - .map(|idx| GeneralParameterIndex::new(idx)) + .map(GeneralParameterIndex::new) { Some(idx.into()) } else if let Some(idx) = self .simple_usize .iter() .position(|p| p.meta().name == name) - .map(|idx| SimpleParameterIndex::new(idx)) - { - Some(idx.into()) - } else if let Some(idx) = self - .constant_usize - .iter() - .position(|p| p.meta().name == name) - .map(|idx| ConstParameterIndex::new(idx)) + .map(SimpleParameterIndex::new) { Some(idx.into()) } else { - None + self.constant_usize + .iter() + .position(|p| p.meta().name == name) + .map(ConstParameterIndex::new) + .map(|idx| idx.into()) } } @@ -1145,25 +1139,22 @@ impl ParameterCollection { .general_multi .iter() .position(|p| p.meta().name == name) - .map(|idx| GeneralParameterIndex::new(idx)) + .map(GeneralParameterIndex::new) { Some(idx.into()) } else if let Some(idx) = self .simple_multi .iter() .position(|p| p.meta().name == name) - .map(|idx| SimpleParameterIndex::new(idx)) - { - Some(idx.into()) - } else if let Some(idx) = self - .constant_multi - .iter() - .position(|p| p.meta().name == name) - .map(|idx| ConstParameterIndex::new(idx)) + .map(SimpleParameterIndex::new) { Some(idx.into()) } else { - None + self.constant_multi + .iter() + .position(|p| p.meta().name == name) + .map(ConstParameterIndex::new) + .map(|idx| idx.into()) } } diff --git a/pywr-core/src/solvers/highs/mod.rs b/pywr-core/src/solvers/highs/mod.rs index d52a9b9b..73fab05b 100644 --- a/pywr-core/src/solvers/highs/mod.rs +++ b/pywr-core/src/solvers/highs/mod.rs @@ -75,7 +75,6 @@ impl Highs { pub fn add_rows( &mut self, - nrows: HighsInt, row_lower: &[f64], row_upper: &[f64], nnz: HighsInt, @@ -86,7 +85,7 @@ impl Highs { unsafe { let ret = Highs_addRows( self.ptr, - nrows, + row_upper.len() as HighsInt, row_lower.as_ptr(), row_upper.as_ptr(), nnz, @@ -170,12 +169,11 @@ impl Solver for HighsSolver { &[] } - fn setup(network: &Network, settings: &Self::Settings) -> Result, PywrError> { + fn setup(network: &Network, _settings: &Self::Settings) -> Result, PywrError> { let builder: SolverBuilder = SolverBuilder::default(); let built = builder.create(network)?; let num_cols = built.num_cols(); - let num_rows = built.num_rows(); let num_nz = built.num_non_zero(); let mut highs_lp = Highs::default(); @@ -183,7 +181,6 @@ impl Solver for HighsSolver { highs_lp.add_cols(built.col_lower(), built.col_upper(), built.col_obj_coef(), num_cols); highs_lp.add_rows( - num_rows, built.row_lower(), built.row_upper(), num_nz, @@ -265,7 +262,7 @@ mod tests { let columns: Vec = vec![0, 1]; let elements: Vec = vec![1.0, 1.0]; - lp.add_rows(1, &row_lower, &row_upper, 2, &row_starts, &columns, &elements); + lp.add_rows(&row_lower, &row_upper, 2, &row_starts, &columns, &elements); } #[test] @@ -286,7 +283,7 @@ mod tests { lp.add_cols(&col_lower, &col_upper, &col_obj_coef, ncols); - lp.add_rows(nrows, &row_lower, &row_upper, nnz, &row_starts, &columns, &elements); + lp.add_rows(&row_lower, &row_upper, nnz, &row_starts, &columns, &elements); lp.run(); assert!(approx_eq!(f64, lp.objective_value(), -20.0)); @@ -314,7 +311,7 @@ mod tests { lp.add_cols(&col_lower, &col_upper, &col_obj_coef, ncols); - lp.add_rows(nrows, &row_lower, &row_upper, nnz, &row_starts, &columns, &elements); + lp.add_rows(&row_lower, &row_upper, nnz, &row_starts, &columns, &elements); lp.run(); assert!(approx_eq!(f64, lp.objective_value(), -40.0)); diff --git a/pywr-core/src/solvers/highs/settings.rs b/pywr-core/src/solvers/highs/settings.rs index 6f3e01aa..98af46cb 100644 --- a/pywr-core/src/solvers/highs/settings.rs +++ b/pywr-core/src/solvers/highs/settings.rs @@ -48,20 +48,12 @@ impl HighsSolverSettings { /// let settings = builder.build(); /// /// ``` +#[derive(Default)] pub struct HighsSolverSettingsBuilder { parallel: bool, threads: usize, } -impl Default for HighsSolverSettingsBuilder { - fn default() -> Self { - Self { - parallel: false, - threads: 0, - } - } -} - impl HighsSolverSettingsBuilder { pub fn parallel(&mut self) -> &mut Self { self.parallel = true; diff --git a/pywr-core/src/state.rs b/pywr-core/src/state.rs index f3b2eff9..1effbbeb 100644 --- a/pywr-core/src/state.rs +++ b/pywr-core/src/state.rs @@ -397,7 +397,7 @@ impl<'a> ParameterValuesRef<'a> { } fn get_multi_value(&self, idx: usize, key: &str) -> Option<&f64> { - self.multi_values.get(idx).map(|s| s.get_value(key)).flatten() + self.multi_values.get(idx).and_then(|s| s.get_value(key)) } } diff --git a/pywr-core/src/test_utils.rs b/pywr-core/src/test_utils.rs index 0a9f9666..752c1a4a 100644 --- a/pywr-core/src/test_utils.rs +++ b/pywr-core/src/test_utils.rs @@ -213,7 +213,7 @@ where ); model .run::(&Default::default()) - .expect(&format!("Failed to solve with: {}", S::name())); + .unwrap_or_else(|_| panic!("Failed to solve with: {}", S::name())); } else { assert!( !has_features, diff --git a/pywr-schema/src/nodes/water_treatment_works.rs b/pywr-schema/src/nodes/water_treatment_works.rs index c65b42a5..3caebc0b 100644 --- a/pywr-schema/src/nodes/water_treatment_works.rs +++ b/pywr-schema/src/nodes/water_treatment_works.rs @@ -182,16 +182,13 @@ impl WaterTreatmentWorks { // The aggregated node does not support zero loss factors so filter them here. let lf = match loss_factor.load(network, args)? { MetricF64::Simple(s) => match s { - SimpleMetricF64::Constant(c) => match c { - ConstantMetricF64::Constant(f) => { - if f.is_zero() { - None - } else { - Some(f.into()) - } + SimpleMetricF64::Constant(ConstantMetricF64::Constant(f)) => { + if f.is_zero() { + None + } else { + Some(f.into()) } - _ => None, - }, + } _ => None, }, m => Some(m), diff --git a/pywr-schema/src/parameters/mod.rs b/pywr-schema/src/parameters/mod.rs index 1f50a98a..5e48bd1b 100644 --- a/pywr-schema/src/parameters/mod.rs +++ b/pywr-schema/src/parameters/mod.rs @@ -772,7 +772,7 @@ impl ParameterIndexValue { match self { Self::Reference(name) => { // This should be an existing parameter - Ok(network.get_index_parameter_index_by_name(name)?.into()) + Ok(network.get_index_parameter_index_by_name(name)?) } Self::Inline(parameter) => { // Inline parameter needs to be added