From 49f24d7b6e2deb144d4214226d267e3f3c867fc8 Mon Sep 17 00:00:00 2001 From: JuanLeon Lahoz Date: Sun, 8 Jan 2023 16:39:05 +0100 Subject: [PATCH] lint: more pedantic linting --- src/format/mod.rs | 2 +- src/plot/histogram.rs | 16 ++++++++-------- src/plot/matchbar.rs | 4 ++-- src/plot/splittimehist.rs | 2 +- src/plot/timehist.rs | 2 +- src/plot/xy.rs | 7 +++---- 6 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/format/mod.rs b/src/format/mod.rs index 570c1c9..085b10f 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -24,7 +24,7 @@ impl F64Formatter { Self { decimals, divisor: 0, - suffix: "".to_owned(), + suffix: String::new(), } } diff --git a/src/plot/histogram.rs b/src/plot/histogram.rs index 84b41a4..47bb14c 100644 --- a/src/plot/histogram.rs +++ b/src/plot/histogram.rs @@ -68,7 +68,7 @@ impl Histogram { let stats = Stats::new(vec, options.precision); let size = options.intervals.min(vec.len()); let step = (stats.max - stats.min) / size as f64; - let mut histogram = Self::new_with_stats(step, stats, options); + let mut histogram = Self::new_with_stats(step, stats, &options); histogram.load(vec); histogram } @@ -79,7 +79,7 @@ impl Histogram { /// Parameters are similar to those on the `new` method, but a parameter /// named `stats` is needed to decide how future data (to be injected with /// the load method) will be accommodated. - pub fn new_with_stats(step: f64, stats: Stats, options: HistogramOptions) -> Self { + pub fn new_with_stats(step: f64, stats: Stats, options: &HistogramOptions) -> Self { let mut vec = Vec::::with_capacity(options.intervals); let mut lower = stats.min; for _ in 0..options.intervals { @@ -88,7 +88,7 @@ impl Histogram { } Self { vec, - max: stats.min + (step * options.intervals as f64), + max: step.mul_add(options.intervals as f64, stats.min), step, top: 0, last: options.intervals - 1, @@ -148,7 +148,7 @@ impl HistWriter { let horizontal_scale = HorizontalScale::new(hist.top / self.get_max_bar_len(width_range + width_count)); writeln!(f, "{horizontal_scale}")?; - for x in hist.vec.iter() { + for x in &hist.vec { self.write_bucket(f, x, &horizontal_scale, width_range, width_count)?; } Ok(()) @@ -205,7 +205,7 @@ mod tests { intervals: 8, ..Default::default() }; - let mut hist = Histogram::new_with_stats(2.5, stats, options); + let mut hist = Histogram::new_with_stats(2.5, stats, &options); hist.load(&[ -1.0, -1.1, 2.0, 2.0, 2.1, -0.9, 11.0, 11.2, 1.9, 1.99, 1.98, 1.97, 1.96, ]); @@ -225,7 +225,7 @@ mod tests { intervals: 6, ..Default::default() }; - let mut hist = Histogram::new_with_stats(1.0, Stats::new(&[-2.0, 4.0], None), options); + let mut hist = Histogram::new_with_stats(1.0, Stats::new(&[-2.0, 4.0], None), &options); hist.load(&[-1.0, 2.0, -1.0, 2.0, 10.0, 10.0, 10.0, -10.0]); assert_eq!(hist.top, 2); } @@ -238,7 +238,7 @@ mod tests { precision: Some(3), log_scale: false, }; - let mut hist = Histogram::new_with_stats(2.5, stats, options); + let mut hist = Histogram::new_with_stats(2.5, stats, &options); hist.load(&[ -1.0, -1.1, 2.0, 2.0, 2.1, -0.9, 11.0, 11.2, 1.9, 1.99, 1.98, 1.97, 1.96, ]); @@ -256,7 +256,7 @@ mod tests { precision: Some(3), log_scale: false, }; - let mut hist = Histogram::new_with_stats(2.5, Stats::new(&[-2.0, 14.0], None), options); + let mut hist = Histogram::new_with_stats(2.5, Stats::new(&[-2.0, 14.0], None), &options); hist.load(&[ -1.0, -1.1, 2.0, 2.0, 2.1, -0.9, 11.0, 11.2, 1.9, 1.99, 1.98, 1.97, 1.96, ]); diff --git a/src/plot/matchbar.rs b/src/plot/matchbar.rs index fc1213e..dec8ef8 100644 --- a/src/plot/matchbar.rs +++ b/src/plot/matchbar.rs @@ -41,7 +41,7 @@ impl MatchBar { pub fn new(vec: Vec) -> Self { let mut top_lenght: usize = 0; let mut top_values: usize = 0; - for row in vec.iter() { + for row in &vec { top_lenght = top_lenght.max(row.label.len()); top_values = top_values.max(row.count); } @@ -67,7 +67,7 @@ impl fmt::Display for MatchBar { )), )?; writeln!(f, "{horizontal_scale}")?; - for row in self.vec.iter() { + for row in &self.vec { writeln!( f, "[{label}] [{count}] {bar}", diff --git a/src/plot/splittimehist.rs b/src/plot/splittimehist.rs index a3d59b6..eeafba3 100644 --- a/src/plot/splittimehist.rs +++ b/src/plot/splittimehist.rs @@ -162,7 +162,7 @@ impl fmt::Display for SplitTimeHistogram { } writeln!(f, "{horizontal_scale}")?; let ts_fmt = date_fmt_string(self.step.num_seconds()); - for row in self.vec.iter() { + for row in &self.vec { self.fmt_row(f, row, horizontal_scale.get_scale(), &widths, ts_fmt)?; } Ok(()) diff --git a/src/plot/timehist.rs b/src/plot/timehist.rs index a60594d..ff38af6 100644 --- a/src/plot/timehist.rs +++ b/src/plot/timehist.rs @@ -110,7 +110,7 @@ impl fmt::Display for TimeHistogram { )?; writeln!(f, "{horizontal_scale}")?; let ts_fmt = date_fmt_string(self.step.num_seconds()); - for row in self.vec.iter() { + for row in &self.vec { writeln!( f, "[{label}] [{count}] {bar}", diff --git a/src/plot/xy.rs b/src/plot/xy.rs index bfb91bc..c10ff4a 100644 --- a/src/plot/xy.rs +++ b/src/plot/xy.rs @@ -69,7 +69,7 @@ impl XyPlot { } let step = (self.stats.max - self.stats.min) / self.height as f64; for y in 0..self.height { - self.y_axis.push(self.stats.min + step * y as f64); + self.y_axis.push(step.mul_add(y as f64, self.stats.min)); } } } @@ -77,7 +77,6 @@ impl XyPlot { impl fmt::Display for XyPlot { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.stats)?; - let _step = (self.stats.max - self.stats.min) / self.height as f64; let f64fmt = match self.precision { None => F64Formatter::new_with_range(self.stats.min..self.stats.max), Some(n) => F64Formatter::new(n), @@ -88,7 +87,7 @@ impl fmt::Display for XyPlot { .map(|v| f64fmt.format(*v).len()) .max() .unwrap(); - let mut newvec = self.y_axis.to_vec(); + let mut newvec = self.y_axis.clone(); newvec.reverse(); print_line(f, &self.x_axis, newvec[0]..f64::INFINITY, y_width, &f64fmt)?; for y in newvec.windows(2) { @@ -110,7 +109,7 @@ fn print_line( // because of unicode char ● having more bytes than ascii chars. for (x, value) in x_axis.iter().enumerate().rev() { if range.contains(value) { - row.replace_range(x..x + 1, "●".as_ref()); + row.replace_range(x..=x, "●".as_ref()); } } writeln!(