Skip to content

Commit c05d993

Browse files
committed
Fix LSP and Clippy warnings and errors
1 parent f0d688a commit c05d993

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]
@@ -557,48 +557,48 @@ mod tests {
557557
assert!(local_vec_1.remove_label_values(&["v1", "v2"]).is_err());
558558

559559
local_vec_1.with_label_values(&["v1", "v2"]).inc_by(23.0);
560-
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 23.0) <= EPSILON);
561-
assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
560+
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 23.0) <= f64::EPSILON);
561+
assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);
562562

563563
local_vec_1.flush();
564-
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
565-
assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= EPSILON);
564+
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);
565+
assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= f64::EPSILON);
566566

567567
local_vec_1.flush();
568-
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
569-
assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= EPSILON);
568+
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);
569+
assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= f64::EPSILON);
570570

571571
local_vec_1.with_label_values(&["v1", "v2"]).inc_by(11.0);
572-
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 11.0) <= EPSILON);
573-
assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= EPSILON);
572+
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 11.0) <= f64::EPSILON);
573+
assert!((vec.with_label_values(&["v1", "v2"]).get() - 23.0) <= f64::EPSILON);
574574

575575
local_vec_1.flush();
576-
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
577-
assert!((vec.with_label_values(&["v1", "v2"]).get() - 34.0) <= EPSILON);
576+
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);
577+
assert!((vec.with_label_values(&["v1", "v2"]).get() - 34.0) <= f64::EPSILON);
578578

579579
// When calling `remove_label_values`, it is "flushed" immediately.
580580
assert!(local_vec_1.remove_label_values(&["v1", "v2"]).is_ok());
581-
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
582-
assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
581+
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);
582+
assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);
583583

584584
local_vec_1.with_label_values(&["v1", "v2"]).inc();
585585
assert!(local_vec_1.remove_label_values(&["v1"]).is_err());
586586
assert!(local_vec_1.remove_label_values(&["v1", "v3"]).is_err());
587587

588588
local_vec_1.with_label_values(&["v1", "v2"]).inc_by(13.0);
589-
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 14.0) <= EPSILON);
590-
assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= EPSILON);
589+
assert!((local_vec_1.with_label_values(&["v1", "v2"]).get() - 14.0) <= f64::EPSILON);
590+
assert!((vec.with_label_values(&["v1", "v2"]).get() - 0.0) <= f64::EPSILON);
591591

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

595595
local_vec_1.flush();
596596
local_vec_2.flush();
597-
assert!((vec.with_label_values(&["v1", "v2"]).get() - 21.0) <= EPSILON);
597+
assert!((vec.with_label_values(&["v1", "v2"]).get() - 21.0) <= f64::EPSILON);
598598

599599
local_vec_1.flush();
600600
local_vec_2.flush();
601-
assert!((vec.with_label_values(&["v1", "v2"]).get() - 21.0) <= EPSILON);
601+
assert!((vec.with_label_values(&["v1", "v2"]).get() - 21.0) <= f64::EPSILON);
602602
}
603603

604604
#[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

@@ -1426,7 +1430,7 @@ mod tests {
14261430

14271431
let proto_histogram = m.get_histogram();
14281432
assert_eq!(proto_histogram.get_sample_count(), 1);
1429-
assert!((proto_histogram.get_sample_sum() - 1.0) < EPSILON);
1433+
assert!((proto_histogram.get_sample_sum() - 1.0) < f64::EPSILON);
14301434
assert_eq!(proto_histogram.get_bucket().len(), buckets.len())
14311435
}
14321436

@@ -1442,7 +1446,7 @@ mod tests {
14421446
let m = histogram.metric();
14431447
let proto_histogram = m.get_histogram();
14441448
assert_eq!(proto_histogram.get_sample_count(), count);
1445-
assert!((proto_histogram.get_sample_sum() - sum) < EPSILON);
1449+
assert!((proto_histogram.get_sample_sum() - sum) < f64::EPSILON);
14461450
};
14471451

14481452
local.observe(1.0);
@@ -1477,7 +1481,7 @@ mod tests {
14771481
let ms = vec.collect()[0].take_metric();
14781482
let proto_histogram = ms[0].get_histogram();
14791483
assert_eq!(proto_histogram.get_sample_count(), count);
1480-
assert!((proto_histogram.get_sample_sum() - sum) < EPSILON);
1484+
assert!((proto_histogram.get_sample_sum() - sum) < f64::EPSILON);
14811485
};
14821486

14831487
{

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)