Skip to content

Commit

Permalink
chore: Fix various Clippy warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
jetuk committed Jul 16, 2024
1 parent f954435 commit d4b8419
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 68 deletions.
2 changes: 1 addition & 1 deletion pywr-core/src/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions pywr-core/src/models/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 2 additions & 6 deletions pywr-core/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1331,19 +1331,15 @@ impl Network {
&mut self,
parameter: Box<dyn parameters::SimpleParameter<f64>>,
) -> Result<ParameterIndex<f64>, 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
pub fn add_const_parameter(
&mut self,
parameter: Box<dyn parameters::ConstParameter<f64>>,
) -> Result<ParameterIndex<f64>, 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
Expand Down
55 changes: 23 additions & 32 deletions pywr-core/src/parameters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());

Expand Down Expand Up @@ -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())
}
}

Expand Down Expand Up @@ -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())
}
}

Expand Down Expand Up @@ -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())
}
}

Expand Down
13 changes: 5 additions & 8 deletions pywr-core/src/solvers/highs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ impl Highs {

pub fn add_rows(
&mut self,
nrows: HighsInt,
row_lower: &[f64],
row_upper: &[f64],
nnz: HighsInt,
Expand All @@ -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,
Expand Down Expand Up @@ -170,20 +169,18 @@ impl Solver for HighsSolver {
&[]
}

fn setup(network: &Network, settings: &Self::Settings) -> Result<Box<Self>, PywrError> {
fn setup(network: &Network, _settings: &Self::Settings) -> Result<Box<Self>, PywrError> {
let builder: SolverBuilder<HighsInt> = 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();

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,
Expand Down Expand Up @@ -265,7 +262,7 @@ mod tests {
let columns: Vec<HighsInt> = vec![0, 1];
let elements: Vec<f64> = 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]
Expand All @@ -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));
Expand Down Expand Up @@ -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));
Expand Down
10 changes: 1 addition & 9 deletions pywr-core/src/solvers/highs/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion pywr-core/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
2 changes: 1 addition & 1 deletion pywr-core/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ where
);
model
.run::<S>(&Default::default())
.expect(&format!("Failed to solve with: {}", S::name()));
.unwrap_or_else(|_| panic!("Failed to solve with: {}", S::name()));
} else {
assert!(
!has_features,
Expand Down
15 changes: 6 additions & 9 deletions pywr-schema/src/nodes/water_treatment_works.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion pywr-schema/src/parameters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d4b8419

Please sign in to comment.