Skip to content

Commit 21ac31f

Browse files
committed
Merge branch 'clippy-fixes' into 'main'
Silence clippy errors and warnings See merge request dbus/zbus!305
2 parents 371c19b + 289c492 commit 21ac31f

File tree

7 files changed

+89
-28
lines changed

7 files changed

+89
-28
lines changed

zbus/src/azync/proxy.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,6 @@ mod tests {
713713
let well_known = "org.freedesktop.zbus.async.ProxySignalConnectTest";
714714
let unique_name = conn.unique_name().unwrap().to_string();
715715
let name_owner_changed_id = {
716-
let well_known = well_known.clone();
717716
let signaled = owner_change_signaled.clone();
718717

719718
proxy

zbus/src/fdo.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,10 @@ mod tests {
635635
use ntest::timeout;
636636
use std::{
637637
convert::TryInto,
638-
sync::{Arc, Mutex},
638+
sync::{
639+
atomic::{AtomicBool, Ordering},
640+
Arc,
641+
},
639642
};
640643
use test_env_log::test;
641644

@@ -660,15 +663,14 @@ mod tests {
660663
// Register a well-known name with the session bus and ensure we get the appropriate
661664
// signals called for that.
662665
let conn = crate::Connection::new_session().unwrap();
663-
let owner_change_signaled = Arc::new(Mutex::new(false));
664-
let name_acquired_signaled = Arc::new(Mutex::new(false));
666+
let owner_change_signaled = Arc::new(AtomicBool::new(false));
667+
let name_acquired_signaled = Arc::new(AtomicBool::new(false));
665668

666669
let proxy = fdo::DBusProxy::new(&conn);
667670

668671
let well_known = "org.freedesktop.zbus.FdoSignalTest";
669672
let unique_name = conn.unique_name().unwrap().to_string();
670673
{
671-
let well_known = well_known.clone();
672674
let signaled = owner_change_signaled.clone();
673675
proxy
674676
.connect_name_owner_changed(move |name, _, new_owner| {
@@ -677,20 +679,20 @@ mod tests {
677679
return Ok(());
678680
}
679681
assert_eq!(new_owner, unique_name);
680-
*signaled.lock().unwrap() = true;
682+
signaled.store(true, Ordering::Release);
681683

682684
Ok(())
683685
})
684686
.unwrap();
685687
}
686688
{
687-
let signaled = name_acquired_signaled.clone();
688689
// `NameAcquired` is emitted twice, first when the unique name is assigned on
689690
// connection and secondly after we ask for a specific name.
691+
let signaled = name_acquired_signaled.clone();
690692
proxy
691693
.connect_name_acquired(move |name| {
692694
if name == well_known {
693-
*signaled.lock().unwrap() = true;
695+
signaled.store(true, Ordering::Release);
694696
}
695697

696698
Ok(())
@@ -705,7 +707,9 @@ mod tests {
705707
loop {
706708
proxy.next_signal().unwrap();
707709

708-
if *owner_change_signaled.lock().unwrap() && *name_acquired_signaled.lock().unwrap() {
710+
if owner_change_signaled.load(Ordering::Acquire)
711+
&& name_acquired_signaled.load(Ordering::Acquire)
712+
{
709713
break;
710714
}
711715
}

zbus/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ mod tests {
664664
}
665665

666666
#[test]
667+
#[allow(clippy::mutex_atomic)]
667668
fn issue_122() {
668669
let conn = Connection::new_session().unwrap();
669670
let conn_clone = conn.clone();

zbus/src/object_server.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ impl ObjectServer {
559559
}
560560

561561
#[cfg(test)]
562+
#[allow(clippy::blacklisted_name)]
562563
mod tests {
563564
use std::{
564565
cell::Cell,

zbus/src/proxy.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ impl<'a> From<azync::Proxy<'a>> for Proxy<'a> {
284284
mod tests {
285285
use super::*;
286286
use ntest::timeout;
287-
use std::sync::{Arc, Mutex};
287+
use std::sync::atomic::{AtomicBool, Ordering};
288288
use test_env_log::test;
289289

290290
#[test]
@@ -293,14 +293,13 @@ mod tests {
293293
// Register a well-known name with the session bus and ensure we get the appropriate
294294
// signals called for that.
295295
let conn = Connection::new_session().unwrap();
296-
let owner_change_signaled = Arc::new(Mutex::new(false));
297-
let name_acquired_signaled = Arc::new(Mutex::new(false));
296+
let owner_change_signaled = Arc::new(AtomicBool::new(false));
297+
let name_acquired_signaled = Arc::new(AtomicBool::new(false));
298298

299299
let proxy = fdo::DBusProxy::new(&conn);
300300
let well_known = "org.freedesktop.zbus.ProxySignalTest";
301301
let unique_name = conn.unique_name().unwrap().to_string();
302302
{
303-
let well_known = well_known.clone();
304303
let signaled = owner_change_signaled.clone();
305304
proxy
306305
.connect_signal("NameOwnerChanged", move |m| {
@@ -310,7 +309,7 @@ mod tests {
310309
return Ok(());
311310
}
312311
assert_eq!(new_owner, unique_name);
313-
*signaled.lock().unwrap() = true;
312+
signaled.store(true, Ordering::Release);
314313

315314
Ok(())
316315
})
@@ -323,7 +322,7 @@ mod tests {
323322
proxy
324323
.connect_signal("NameAcquired", move |m| {
325324
if m.body::<&str>()? == well_known {
326-
*signaled.lock().unwrap() = true;
325+
signaled.store(true, Ordering::Release);
327326
}
328327

329328
Ok(())
@@ -338,7 +337,9 @@ mod tests {
338337
loop {
339338
proxy.next_signal().unwrap();
340339

341-
if *owner_change_signaled.lock().unwrap() && *name_acquired_signaled.lock().unwrap() {
340+
if owner_change_signaled.load(Ordering::Acquire)
341+
&& name_acquired_signaled.load(Ordering::Acquire)
342+
{
342343
break;
343344
}
344345
}

zvariant/benches/benchmarks.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ fn big_array_ser_and_de(c: &mut Criterion) {
5656
idx, idx, idx, idx, idx, idx, idx, idx, idx, idx, idx, idx
5757
));
5858
}
59-
for idx in 0..1024 * 10 {
60-
let s = &strings[idx];
59+
for s in &strings {
6160
string_array.push(s.as_str());
6261
dict.insert(s.as_str(), Value::from(s.as_str()));
6362
}

zvariant/src/lib.rs

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![allow(clippy::unusual_byte_groupings)]
12
#![deny(rust_2018_idioms)]
23
#![doc(
34
html_logo_url = "https://storage.googleapis.com/fdo-gitlab-uploads/project/avatar/3213/zbus-logomark.png"
@@ -210,6 +211,7 @@ pub mod export {
210211
}
211212

212213
#[cfg(test)]
214+
#[allow(clippy::blacklisted_name)]
213215
mod tests {
214216
use std::{
215217
collections::HashMap,
@@ -309,6 +311,62 @@ mod tests {
309311
}};
310312
}
311313

314+
fn f64_type_test(
315+
format: EncodingFormat,
316+
value: f64,
317+
expected_len: usize,
318+
expected_value_len: usize,
319+
) -> Vec<u8> {
320+
// Lie that we're starting at byte 1 in the overall message to test padding
321+
let ctxt = Context::<BE>::new(format, 1);
322+
let (encoded, fds) = to_bytes_fds(ctxt, &value).unwrap();
323+
let padding = crate::padding_for_n_bytes(1, 8);
324+
assert_eq!(
325+
encoded.len(),
326+
expected_len + padding,
327+
"invalid encoding using `to_bytes`"
328+
);
329+
330+
let decoded: f64 = from_slice_fds(&encoded, Some(&fds), ctxt).unwrap();
331+
assert!(
332+
(decoded - value).abs() < f64::EPSILON,
333+
"invalid decoding using `from_slice`"
334+
);
335+
336+
// Now encode w/o padding
337+
let ctxt = Context::<BE>::new(format, 0);
338+
let (encoded, _) = to_bytes_fds(ctxt, &value).unwrap();
339+
assert_eq!(
340+
encoded.len(),
341+
expected_len,
342+
"invalid encoding using `to_bytes`"
343+
);
344+
345+
f64_type_test_as_value(format, value, expected_value_len);
346+
encoded
347+
}
348+
349+
fn f64_type_test_as_value(format: EncodingFormat, value: f64, expected_value_len: usize) {
350+
let v: Value<'_> = value.into();
351+
assert_eq!(v.value_signature(), f64::SIGNATURE_STR);
352+
assert_eq!(v, Value::F64(value));
353+
f64_value_test(format, v.clone(), expected_value_len);
354+
let v: f64 = v.try_into().unwrap();
355+
assert!((v - value).abs() < f64::EPSILON);
356+
}
357+
358+
fn f64_value_test(format: EncodingFormat, v: Value<'_>, expected_value_len: usize) {
359+
let ctxt = Context::<LE>::new(format, 0);
360+
let (encoded, fds) = to_bytes_fds(ctxt, &v).unwrap();
361+
assert_eq!(
362+
encoded.len(),
363+
expected_value_len,
364+
"invalid encoding using `to_bytes`"
365+
);
366+
let decoded: Value<'_> = from_slice_fds(&encoded, Some(&fds), ctxt).unwrap();
367+
assert!(decoded == v, "invalid decoding using `from_slice`");
368+
}
369+
312370
#[cfg(feature = "gvariant")]
313371
fn decode_with_gvariant<B, T>(encoded: B) -> T
314372
where
@@ -411,15 +469,15 @@ mod tests {
411469

412470
#[test]
413471
fn f64_value() {
414-
let encoded = basic_type_test!(BE, DBus, 99999.99999_f64, 8, f64, 8, F64, 16);
415-
assert_eq!(LE::read_f64(&encoded), -5759340900185448e-143);
472+
let encoded = f64_type_test(EncodingFormat::DBus, 99999.99999_f64, 8, 16);
473+
assert!((LE::read_f64(&encoded) - -5.759340900185448e-128).abs() < f64::EPSILON);
416474
#[cfg(feature = "gvariant")]
417475
{
418-
assert_eq!(
419-
decode_with_gvariant::<_, f64>(encoded),
420-
-5759340900185448e-143
476+
assert!(
477+
(decode_with_gvariant::<_, f64>(encoded) - -5.759340900185448e-128).abs()
478+
< f64::EPSILON
421479
);
422-
basic_type_test!(BE, GVariant, 99999.99999_f64, 8, f64, 8, F64, 10);
480+
f64_type_test(EncodingFormat::GVariant, 99999.99999_f64, 8, 10);
423481
}
424482
}
425483

@@ -456,8 +514,6 @@ mod tests {
456514
assert_eq!(variant.get_str().unwrap(), "hello world");
457515
}
458516

459-
let v: String = v.try_into().unwrap();
460-
assert_eq!(v, "hello world");
461517
let v: String = v.try_into().unwrap();
462518
assert_eq!(v, "hello world");
463519

@@ -569,8 +625,8 @@ mod tests {
569625
let ctxt = Context::<BE>::new_dbus(0);
570626
let (encoded, fds) = to_bytes_fds(ctxt, &()).unwrap();
571627
assert_eq!(encoded.len(), 0, "invalid encoding using `to_bytes`");
572-
let decoded: () = from_slice_fds(&encoded, Some(&fds), ctxt).unwrap();
573-
assert_eq!(decoded, (), "invalid decoding using `from_slice`");
628+
let _decoded: () = from_slice_fds(&encoded, Some(&fds), ctxt)
629+
.expect("invalid decoding using `from_slice`");
574630
}
575631

576632
#[test]

0 commit comments

Comments
 (0)