diff --git a/Cargo.toml b/Cargo.toml index fb2aab7..43aebad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,6 +55,7 @@ suspicious = { level = "warn", priority = -1 } # These lints are explicitly allowed. missing_errors_doc = "allow" # the Error type is self documenting map_unwrap_or = "allow" # we prefer to `map(a).unwrap_or(b)` as it's clear what the fallback value is +must_use_candidate = "allow" # These lints are allowed, but we want to deny them over time missing_panics_doc = "allow" diff --git a/cli/src/cli.rs b/cli/src/cli.rs index bb2a50b..6c8635d 100644 --- a/cli/src/cli.rs +++ b/cli/src/cli.rs @@ -578,7 +578,7 @@ impl Lint { ); for warn in warnings { - println!(" - {}", warn); + println!(" - {warn}"); } println!(); diff --git a/ocpi-tariffs/src/explain.rs b/ocpi-tariffs/src/explain.rs index 8f4bc43..2eefc98 100644 --- a/ocpi-tariffs/src/explain.rs +++ b/ocpi-tariffs/src/explain.rs @@ -33,10 +33,10 @@ pub fn explain(tariff: &OcpiTariff) -> Explain { TariffDimensionType::Flat => components.flat = Some(component.price.with_scale(2)), TariffDimensionType::Time => components.time = Some(component.price.with_scale(2)), TariffDimensionType::Energy => { - components.energy = Some(component.price.with_scale(2)) + components.energy = Some(component.price.with_scale(2)); } TariffDimensionType::ParkingTime => { - components.parking_time = Some(component.price.with_scale(2)) + components.parking_time = Some(component.price.with_scale(2)); } } } @@ -44,7 +44,7 @@ pub fn explain(tariff: &OcpiTariff) -> Explain { let restrictions = element .restrictions .as_ref() - .map(|restr| explain_restrictions(restr)) + .map(explain_restrictions) .unwrap_or_default(); elements.push(ExplainElement { @@ -76,11 +76,11 @@ pub fn explain_restrictions(restr: &OcpiTariffRestriction) -> Vec { } if let Some((start_time, end_time)) = restr.start_time.zip(restr.end_time) { - explains.push(format!("between {} and {}", start_time, end_time)); + explains.push(format!("between {start_time} and {end_time}")); } else if let Some(start_time) = restr.start_time { - explains.push(format!("after {}", start_time)); + explains.push(format!("after {start_time}")); } else if let Some(end_time) = restr.end_time { - explains.push(format!("before {}", end_time)); + explains.push(format!("before {end_time}")); } if let Some((min_duration, max_duration)) = restr.min_duration.zip(restr.max_duration) { @@ -102,11 +102,11 @@ pub fn explain_restrictions(restr: &OcpiTariffRestriction) -> Vec { } if let Some((start_date, end_date)) = restr.start_date.zip(restr.end_date) { - explains.push(format!("between {} and {}", start_date, end_date)); + explains.push(format!("between {start_date} and {end_date}")); } else if let Some(start_date) = restr.start_date { - explains.push(format!("after {}", start_date)); + explains.push(format!("after {start_date}")); } else if let Some(end_date) = restr.end_date { - explains.push(format!("before {}", end_date)); + explains.push(format!("before {end_date}")); } explains diff --git a/ocpi-tariffs/src/lint.rs b/ocpi-tariffs/src/lint.rs index b64e745..0558dbc 100644 --- a/ocpi-tariffs/src/lint.rs +++ b/ocpi-tariffs/src/lint.rs @@ -124,19 +124,11 @@ pub fn lint(tariff: &OcpiTariff) -> Vec { } // Now for each component type we attempt to lint the restrictions. + lint_restrictions(&energy_elements, TariffDimensionType::Energy, &mut warnings); + lint_restrictions(&flat_elements, TariffDimensionType::Flat, &mut warnings); + lint_restrictions(&time_elements, TariffDimensionType::Energy, &mut warnings); lint_restrictions( - &mut energy_elements, - TariffDimensionType::Energy, - &mut warnings, - ); - lint_restrictions(&mut flat_elements, TariffDimensionType::Flat, &mut warnings); - lint_restrictions( - &mut time_elements, - TariffDimensionType::Energy, - &mut warnings, - ); - lint_restrictions( - &mut parking_time_elements, + &parking_time_elements, TariffDimensionType::ParkingTime, &mut warnings, ); @@ -156,12 +148,7 @@ pub fn lint(tariff: &OcpiTariff) -> Vec { for (el_idx, count) in comp_counts { // All components are redundant, mark the whole element as redundant. if count == 0 { - warnings.retain(|w| match w { - &Warning::ComponentIsRedundant { element_index, .. } if el_idx == element_index => { - false - } - _ => true, - }); + warnings.retain(|w| !matches!(w, &Warning::ComponentIsRedundant { element_index, .. } if el_idx == element_index)); warnings.push(Warning::ElementIsRedundant { element_index: el_idx, @@ -179,7 +166,7 @@ struct UnaryElement { } fn lint_restrictions( - elements: &mut Vec, + elements: &[UnaryElement], ty: TariffDimensionType, warnings: &mut Vec, ) { @@ -200,7 +187,7 @@ fn lint_restrictions( let mut matrix = Matrix::new(bounds); - for element in elements.iter() { + for element in elements { let Some(restr) = &element.restrictions else { matrix.add_pattern(Pattern::new( vec![Range::wildcard(); 4], @@ -262,7 +249,7 @@ fn lint_restrictions( warnings.push(Warning::ComponentIsRedundant { element_index, component_index, - }) + }); } } @@ -273,7 +260,7 @@ fn lint_restrictions( // If the trailing wildcard is useful it means all the elements above are non-exhaustive. if last.is_usefull { - warnings.push(Warning::DimensionNotExhaustive { ty, cases: vec![] }) + warnings.push(Warning::DimensionNotExhaustive { ty, cases: vec![] }); } } @@ -292,7 +279,7 @@ impl Matrix { } fn add_pattern(&mut self, pattern: Pattern) { - self.patterns.push(pattern) + self.patterns.push(pattern); } /// Computes usefulness for the whole matrix and mark the patterns with usefulness. @@ -307,7 +294,9 @@ impl Matrix { if !witnesses.is_empty() { self.patterns[i].is_usefull = true; - witnesses.iter_mut().for_each(|v| v.reverse()); + for witness in &mut witnesses { + witness.reverse(); + } self.patterns[i].witness = witnesses; } @@ -355,13 +344,13 @@ impl Matrix { continue; } - next_consider.push(i) + next_consider.push(i); } for mut witness in self.usefulness_rec(column + 1, pattern, &next_consider) { witness.push(constr); - witnesses.push(witness) + witnesses.push(witness); } } diff --git a/ocpi-tariffs/src/normalize.rs b/ocpi-tariffs/src/normalize.rs index cf4af0d..fef46ad 100644 --- a/ocpi-tariffs/src/normalize.rs +++ b/ocpi-tariffs/src/normalize.rs @@ -20,8 +20,8 @@ pub fn normalize(tariff: &mut OcpiTariff) { } } - remove_components.sort(); - remove_elements.sort(); + remove_components.sort_unstable(); + remove_elements.sort_unstable(); // Remove them in sorted reverse order for the indices to stay intact. for &(el, comp) in remove_components.iter().rev() { diff --git a/ocpi-tariffs/src/types/electricity.rs b/ocpi-tariffs/src/types/electricity.rs index 267ab23..88940ce 100644 --- a/ocpi-tariffs/src/types/electricity.rs +++ b/ocpi-tariffs/src/types/electricity.rs @@ -49,6 +49,7 @@ impl Kwh { } /// Round this number to the specified amount of decimals. + #[must_use] pub fn with_scale(self, scale: u32) -> Self { Self(self.0.with_scale(scale)) } diff --git a/ocpi-tariffs/src/types/money.rs b/ocpi-tariffs/src/types/money.rs index 310ef17..ff9bb82 100644 --- a/ocpi-tariffs/src/types/money.rs +++ b/ocpi-tariffs/src/types/money.rs @@ -36,6 +36,7 @@ impl Price { } /// Round this number to the specified amount of decimals. + #[must_use] pub fn with_scale(self, scale: u32) -> Self { Self { excl_vat: self.excl_vat.with_scale(scale), @@ -81,6 +82,7 @@ impl Money { } /// Round this number to the specified amount of decimals. + #[must_use] pub fn with_scale(self, scale: u32) -> Self { Self(self.0.with_scale(scale)) }