Skip to content

Commit 5b62f4b

Browse files
svanharmelenlucab
andauthored
Fix LSP and Clippy warnings and errors (#540)
Signed-off-by: Sander van Harmelen <[email protected]> Co-authored-by: Luca Bruno <[email protected]>
1 parent 52d76fc commit 5b62f4b

File tree

6 files changed

+51
-49
lines changed

6 files changed

+51
-49
lines changed

src/atomic64.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ impl AtomicU64 {
241241

242242
#[cfg(test)]
243243
mod test {
244+
use std::f64;
244245
use std::f64::consts::PI;
245-
use std::f64::{self, EPSILON};
246246

247247
use super::*;
248248

@@ -251,7 +251,7 @@ mod test {
251251
let table: Vec<f64> = vec![0.0, 1.0, PI, f64::MIN, f64::MAX];
252252

253253
for f in table {
254-
assert!((f - AtomicF64::new(f).get()).abs() < EPSILON);
254+
assert!((f - AtomicF64::new(f).get()).abs() < f64::EPSILON);
255255
}
256256
}
257257

src/counter.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ impl<P: Atomic> Clone for GenericLocalCounterVec<P> {
323323
#[cfg(test)]
324324
mod tests {
325325
use std::collections::HashMap;
326-
use std::f64::EPSILON;
326+
use std::f64;
327327

328328
use super::*;
329329
use crate::metrics::{Collector, Opts};
@@ -343,7 +343,7 @@ mod tests {
343343
assert_eq!(mfs.len(), 1);
344344

345345
let mf = mfs.pop().unwrap();
346-
let m = mf.get_metric().get(0).unwrap();
346+
let m = mf.get_metric().first().unwrap();
347347
assert_eq!(m.get_label().len(), 2);
348348
assert_eq!(m.get_counter().get_value() as u64, 43);
349349

@@ -363,12 +363,12 @@ mod tests {
363363
assert_eq!(mfs.len(), 1);
364364

365365
let mf = mfs.pop().unwrap();
366-
let m = mf.get_metric().get(0).unwrap();
366+
let m = mf.get_metric().first().unwrap();
367367
assert_eq!(m.get_label().len(), 0);
368368
assert_eq!(m.get_counter().get_value() as u64, 12);
369369

370370
counter.reset();
371-
assert_eq!(counter.get() as u64, 0);
371+
assert_eq!(counter.get(), 0);
372372
}
373373

374374
#[test]
@@ -414,9 +414,9 @@ mod tests {
414414

415415
local_counter.reset();
416416
counter.reset();
417-
assert_eq!(counter.get() as u64, 0);
417+
assert_eq!(counter.get(), 0);
418418
local_counter.flush();
419-
assert_eq!(counter.get() as u64, 0);
419+
assert_eq!(counter.get(), 0);
420420
}
421421

422422
#[test]
@@ -502,48 +502,48 @@ mod tests {
502502
assert!(local_vec_1.remove_label_values(&["v1", "v2"]).is_err());
503503

504504
local_vec_1.with_label_values(&["v1", "v2"]).inc_by(23.0);
505-
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 23.0) <= EPSILON);
506-
assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
505+
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 23.0) <= f64::EPSILON);
506+
assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);
507507

508508
local_vec_1.flush();
509-
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
510-
assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= EPSILON);
509+
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);
510+
assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= f64::EPSILON);
511511

512512
local_vec_1.flush();
513-
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
514-
assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= EPSILON);
513+
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);
514+
assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= f64::EPSILON);
515515

516516
local_vec_1.with_label_values(&["v1", "v2"]).inc_by(11.0);
517-
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 11.0) <= EPSILON);
518-
assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= EPSILON);
517+
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 11.0) <= f64::EPSILON);
518+
assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= f64::EPSILON);
519519

520520
local_vec_1.flush();
521-
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
522-
assert!((vec.with_label_values(&["v1", "v2"]).get() - 34.0) <= EPSILON);
521+
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);
522+
assert!((vec.with_label_values(&["v1", "v2"]).get() - 34.0) <= f64::EPSILON);
523523

524524
// When calling `remove_label_values`, it is "flushed" immediately.
525525
assert!(local_vec_1.remove_label_values(&["v1", "v2"]).is_ok());
526-
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
527-
assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
526+
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);
527+
assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);
528528

529529
local_vec_1.with_label_values(&["v1", "v2"]).inc();
530530
assert!(local_vec_1.remove_label_values(&["v1"]).is_err());
531531
assert!(local_vec_1.remove_label_values(&["v1", "v3"]).is_err());
532532

533533
local_vec_1.with_label_values(&["v1", "v2"]).inc_by(13.0);
534-
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 14.0) <= EPSILON);
535-
assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
534+
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 14.0) <= f64::EPSILON);
535+
assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);
536536

537537
local_vec_2.with_label_values(&["v1", "v2"]).inc_by(7.0);
538-
assert!((local_vec_2.with_label_values(&["v1", "v2"]).get() - 7.0) <= EPSILON);
538+
assert!((local_vec_2.with_label_values(&["v1", "v2"]).get() - 7.0) <= f64::EPSILON);
539539

540540
local_vec_1.flush();
541541
local_vec_2.flush();
542-
assert!((vec.with_label_values(&["v1", "v2"]).get() - 21.0) <= EPSILON);
542+
assert!((vec.with_label_values(&["v1", "v2"]).get() - 21.0) <= f64::EPSILON);
543543

544544
local_vec_1.flush();
545545
local_vec_2.flush();
546-
assert!((vec.with_label_values(&["v1", "v2"]).get() - 21.0) <= EPSILON);
546+
assert!((vec.with_label_values(&["v1", "v2"]).get() - 21.0) <= f64::EPSILON);
547547
}
548548

549549
#[test]

src/desc.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,7 @@ mod tests {
253253
vec![name.into()],
254254
HashMap::new(),
255255
)
256-
.err()
257-
.expect(format!("expected error for {}", name).as_ref());
256+
.expect_err(format!("expected error for {}", name).as_ref());
258257
match res {
259258
Error::Msg(msg) => assert_eq!(msg, format!("'{}' is not a valid label name", name)),
260259
other => panic!("{}", other),
@@ -268,8 +267,7 @@ mod tests {
268267
let mut labels = HashMap::new();
269268
labels.insert(name.into(), "value".into());
270269
let res = Desc::new("name".into(), "help".into(), vec![], labels)
271-
.err()
272-
.expect(format!("expected error for {}", name).as_ref());
270+
.expect_err(format!("expected error for {}", name).as_ref());
273271
match res {
274272
Error::Msg(msg) => assert_eq!(msg, format!("'{}' is not a valid label name", name)),
275273
other => panic!("{}", other),
@@ -281,8 +279,7 @@ mod tests {
281279
fn test_invalid_metric_name() {
282280
for &name in &["-dash", "9gag", "has space"] {
283281
let res = Desc::new(name.into(), "help".into(), vec![], HashMap::new())
284-
.err()
285-
.expect(format!("expected error for {}", name).as_ref());
282+
.expect_err(format!("expected error for {}", name).as_ref());
286283
match res {
287284
Error::Msg(msg) => {
288285
assert_eq!(msg, format!("'{}' is not a valid metric name", name))

src/gauge.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ mod tests {
188188
assert_eq!(mfs.len(), 1);
189189

190190
let mf = mfs.pop().unwrap();
191-
let m = mf.get_metric().get(0).unwrap();
191+
let m = mf.get_metric().first().unwrap();
192192
assert_eq!(m.get_label().len(), 2);
193193
assert_eq!(m.get_gauge().get_value() as u64, 42);
194194
}

src/histogram.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,11 @@ impl ShardAndCount {
284284
/// A histogram supports two main execution paths:
285285
///
286286
/// 1. `observe` which increases the overall observation counter, updates the
287-
/// observation sum and increases a single bucket counter.
287+
/// observation sum and increases a single bucket counter.
288288
///
289289
/// 2. `proto` (aka. collecting the metric, from now on referred to as the
290-
/// collect operation) which snapshots the state of the histogram and exposes it
291-
/// as a Protobuf struct.
290+
/// collect operation) which snapshots the state of the histogram and exposes
291+
/// it as a Protobuf struct.
292292
///
293293
/// If an observe and a collect operation interleave, the latter could be
294294
/// exposing a snapshot of the histogram that does not uphold all histogram
@@ -1207,7 +1207,7 @@ impl Clone for LocalHistogramVec {
12071207

12081208
#[cfg(test)]
12091209
mod tests {
1210-
use std::f64::{EPSILON, INFINITY};
1210+
use std::f64;
12111211
use std::thread;
12121212
use std::time::Duration;
12131213

@@ -1237,7 +1237,7 @@ mod tests {
12371237
assert_eq!(mfs.len(), 1);
12381238

12391239
let mf = mfs.pop().unwrap();
1240-
let m = mf.get_metric().get(0).unwrap();
1240+
let m = mf.get_metric().first().unwrap();
12411241
assert_eq!(m.get_label().len(), 2);
12421242
let proto_histogram = m.get_histogram();
12431243
assert_eq!(proto_histogram.get_sample_count(), 3);
@@ -1251,11 +1251,11 @@ mod tests {
12511251
assert_eq!(mfs.len(), 1);
12521252

12531253
let mf = mfs.pop().unwrap();
1254-
let m = mf.get_metric().get(0).unwrap();
1254+
let m = mf.get_metric().first().unwrap();
12551255
assert_eq!(m.get_label().len(), 0);
12561256
let proto_histogram = m.get_histogram();
12571257
assert_eq!(proto_histogram.get_sample_count(), 0);
1258-
assert!((proto_histogram.get_sample_sum() - 0.0) < EPSILON);
1258+
assert!((proto_histogram.get_sample_sum() - 0.0) < f64::EPSILON);
12591259
assert_eq!(proto_histogram.get_bucket().len(), buckets.len())
12601260
}
12611261

@@ -1287,7 +1287,7 @@ mod tests {
12871287
let m = mf.get_metric().get(0).unwrap();
12881288
let proto_histogram = m.get_histogram();
12891289
assert_eq!(proto_histogram.get_sample_count(), 3);
1290-
assert!((proto_histogram.get_sample_sum() - 0.0) > EPSILON);
1290+
assert!((proto_histogram.get_sample_sum() - 0.0) > f64::EPSILON);
12911291
}
12921292

12931293
#[test]
@@ -1311,7 +1311,11 @@ mod tests {
13111311
(vec![], true, DEFAULT_BUCKETS.len()),
13121312
(vec![-2.0, -1.0, -0.5, 0.0, 0.5, 1.0, 2.0], true, 7),
13131313
(vec![-2.0, -1.0, -0.5, 10.0, 0.5, 1.0, 2.0], false, 7),
1314-
(vec![-2.0, -1.0, -0.5, 0.0, 0.5, 1.0, INFINITY], true, 6),
1314+
(
1315+
vec![-2.0, -1.0, -0.5, 0.0, 0.5, 1.0, f64::INFINITY],
1316+
true,
1317+
6,
1318+
),
13151319
];
13161320

13171321
for (buckets, is_ok, length) in table {
@@ -1366,7 +1370,7 @@ mod tests {
13661370
for (millis, seconds) in tbls {
13671371
let d = Duration::from_millis(millis);
13681372
let v = duration_to_seconds(d);
1369-
assert!((v - seconds).abs() < EPSILON);
1373+
assert!((v - seconds).abs() < f64::EPSILON);
13701374
}
13711375
}
13721376

@@ -1405,7 +1409,7 @@ mod tests {
14051409

14061410
let proto_histogram = m.get_histogram();
14071411
assert_eq!(proto_histogram.get_sample_count(), 1);
1408-
assert!((proto_histogram.get_sample_sum() - 1.0) < EPSILON);
1412+
assert!((proto_histogram.get_sample_sum() - 1.0) < f64::EPSILON);
14091413
assert_eq!(proto_histogram.get_bucket().len(), buckets.len())
14101414
}
14111415

@@ -1421,7 +1425,7 @@ mod tests {
14211425
let m = histogram.metric();
14221426
let proto_histogram = m.get_histogram();
14231427
assert_eq!(proto_histogram.get_sample_count(), count);
1424-
assert!((proto_histogram.get_sample_sum() - sum) < EPSILON);
1428+
assert!((proto_histogram.get_sample_sum() - sum) < f64::EPSILON);
14251429
};
14261430

14271431
local.observe(1.0);
@@ -1456,7 +1460,7 @@ mod tests {
14561460
let ms = vec.collect()[0].take_metric();
14571461
let proto_histogram = ms[0].get_histogram();
14581462
assert_eq!(proto_histogram.get_sample_count(), count);
1459-
assert!((proto_histogram.get_sample_sum() - sum) < EPSILON);
1463+
assert!((proto_histogram.get_sample_sum() - sum) < f64::EPSILON);
14601464
};
14611465

14621466
{

src/macros.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn test_labels_without_trailing_comma() {
4343
"foo" => "bar"
4444
};
4545
assert_eq!(labels.len(), 2);
46-
assert!(labels.get("test").is_some());
46+
assert!(labels.contains_key("test"));
4747
assert_eq!(*(labels.get("test").unwrap()), "hello");
4848
}
4949

@@ -84,6 +84,7 @@ macro_rules! opts {
8484
let opts = $crate::Opts::new($NAME, $HELP);
8585
let lbs = HashMap::<String, String>::new();
8686
$(
87+
#[allow(clippy::redundant_locals)]
8788
let mut lbs = lbs;
8889
lbs.extend($CONST_LABELS.iter().map(|(k, v)| ((*k).into(), (*v).into())));
8990
)*
@@ -104,7 +105,7 @@ fn test_opts_trailing_comma() {
104105

105106
let opts = opts!(name, help, labels! {"test" => "hello", "foo" => "bar",},);
106107
assert_eq!(opts.const_labels.len(), 2);
107-
assert!(opts.const_labels.get("foo").is_some());
108+
assert!(opts.const_labels.contains_key("foo"));
108109
assert_eq!(opts.const_labels.get("foo").unwrap(), "bar");
109110

110111
let opts = opts!(
@@ -114,7 +115,7 @@ fn test_opts_trailing_comma() {
114115
labels! {"ans" => "42",},
115116
);
116117
assert_eq!(opts.const_labels.len(), 3);
117-
assert!(opts.const_labels.get("ans").is_some());
118+
assert!(opts.const_labels.contains_key("ans"));
118119
assert_eq!(opts.const_labels.get("ans").unwrap(), "42");
119120
}
120121

@@ -191,7 +192,7 @@ fn test_histogram_opts_trailing_comma() {
191192
assert_eq!(opts.common_opts.name, name);
192193
assert_eq!(opts.common_opts.help, help);
193194
assert_eq!(opts.buckets.len(), 2);
194-
assert!(opts.common_opts.const_labels.get("key").is_some());
195+
assert!(opts.common_opts.const_labels.contains_key("key"));
195196
assert_eq!(opts.common_opts.const_labels.get("key").unwrap(), "value");
196197
}
197198

0 commit comments

Comments
 (0)