Skip to content

Commit

Permalink
clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
remkop22 committed Nov 22, 2024
1 parent b924714 commit c31020e
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 38 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ impl Lint {
);

for warn in warnings {
println!(" - {}", warn);
println!(" - {warn}");
}

println!();
Expand Down
18 changes: 9 additions & 9 deletions ocpi-tariffs/src/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ 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));
}
}
}

let restrictions = element
.restrictions
.as_ref()
.map(|restr| explain_restrictions(restr))
.map(explain_restrictions)
.unwrap_or_default();

elements.push(ExplainElement {
Expand Down Expand Up @@ -76,11 +76,11 @@ pub fn explain_restrictions(restr: &OcpiTariffRestriction) -> Vec<String> {
}

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) {
Expand All @@ -102,11 +102,11 @@ pub fn explain_restrictions(restr: &OcpiTariffRestriction) -> Vec<String> {
}

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
Expand Down
41 changes: 15 additions & 26 deletions ocpi-tariffs/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,11 @@ pub fn lint(tariff: &OcpiTariff) -> Vec<Warning> {
}

// 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,
);
Expand All @@ -156,12 +148,7 @@ pub fn lint(tariff: &OcpiTariff) -> Vec<Warning> {
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,
Expand All @@ -179,7 +166,7 @@ struct UnaryElement {
}

fn lint_restrictions(
elements: &mut Vec<UnaryElement>,
elements: &[UnaryElement],
ty: TariffDimensionType,
warnings: &mut Vec<Warning>,
) {
Expand All @@ -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],
Expand Down Expand Up @@ -262,7 +249,7 @@ fn lint_restrictions(
warnings.push(Warning::ComponentIsRedundant {
element_index,
component_index,
})
});
}
}

Expand All @@ -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![] });
}
}

Expand All @@ -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.
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions ocpi-tariffs/src/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions ocpi-tariffs/src/types/electricity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
2 changes: 2 additions & 0 deletions ocpi-tariffs/src/types/money.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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))
}
Expand Down

0 comments on commit c31020e

Please sign in to comment.