Skip to content

Commit b477a87

Browse files
committed
Implemented the Clippy recommendations
1 parent a4915bb commit b477a87

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+241
-248
lines changed

boxslab/src/lib.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* limitations under the License.
1616
*/
1717

18-
1918
use std::{
2019
mem::MaybeUninit,
2120
ops::{Deref, DerefMut},
@@ -115,7 +114,7 @@ impl<T: SlabPool> Slab<T> {
115114
}))
116115
}
117116

118-
pub fn new(new_object: T::SlabType) -> Option<BoxSlab<T>> {
117+
pub fn try_new(new_object: T::SlabType) -> Option<BoxSlab<T>> {
119118
let slab = T::get_slab();
120119
let mut inner = slab.0.lock().unwrap();
121120
if let Some(index) = inner.map.first_false_index() {
@@ -180,40 +179,37 @@ mod tests {
180179
#[test]
181180
fn simple_alloc_free() {
182181
{
183-
let a = Slab::<TestSlab>::new(Test { val: Arc::new(10) }).unwrap();
182+
let a = Slab::<TestSlab>::try_new(Test { val: Arc::new(10) }).unwrap();
184183
assert_eq!(*a.val.deref(), 10);
185184
let inner = TestSlab::get_slab().0.lock().unwrap();
186-
assert_eq!(inner.map.is_empty(), false);
185+
assert!(!inner.map.is_empty());
187186
}
188187
// Validates that the 'Drop' got executed
189188
let inner = TestSlab::get_slab().0.lock().unwrap();
190-
assert_eq!(inner.map.is_empty(), true);
189+
assert!(inner.map.is_empty());
191190
println!("Box Size {}", std::mem::size_of::<Box<Test>>());
192191
println!("BoxSlab Size {}", std::mem::size_of::<BoxSlab<TestSlab>>());
193192
}
194193

195194
#[test]
196195
fn alloc_full_block() {
197196
{
198-
let a = Slab::<TestSlab>::new(Test { val: Arc::new(10) }).unwrap();
199-
let b = Slab::<TestSlab>::new(Test { val: Arc::new(11) }).unwrap();
200-
let c = Slab::<TestSlab>::new(Test { val: Arc::new(12) }).unwrap();
197+
let a = Slab::<TestSlab>::try_new(Test { val: Arc::new(10) }).unwrap();
198+
let b = Slab::<TestSlab>::try_new(Test { val: Arc::new(11) }).unwrap();
199+
let c = Slab::<TestSlab>::try_new(Test { val: Arc::new(12) }).unwrap();
201200
// Test that at overflow, we return None
202-
assert_eq!(
203-
Slab::<TestSlab>::new(Test { val: Arc::new(13) }).is_none(),
204-
true
205-
);
201+
assert!(Slab::<TestSlab>::try_new(Test { val: Arc::new(13) }).is_none(),);
206202
assert_eq!(*b.val.deref(), 11);
207203

208204
{
209205
let inner = TestSlab::get_slab().0.lock().unwrap();
210206
// Test that the bitmap is marked as full
211-
assert_eq!(inner.map.is_full(), true);
207+
assert!(inner.map.is_full());
212208
}
213209

214210
// Purposefully drop, to test that new allocation is possible
215211
std::mem::drop(b);
216-
let d = Slab::<TestSlab>::new(Test { val: Arc::new(21) }).unwrap();
212+
let d = Slab::<TestSlab>::try_new(Test { val: Arc::new(21) }).unwrap();
217213
assert_eq!(*d.val.deref(), 21);
218214

219215
// Ensure older allocations are still valid
@@ -223,16 +219,16 @@ mod tests {
223219

224220
// Validates that the 'Drop' got executed - test that the bitmap is empty
225221
let inner = TestSlab::get_slab().0.lock().unwrap();
226-
assert_eq!(inner.map.is_empty(), true);
222+
assert!(inner.map.is_empty());
227223
}
228224

229225
#[test]
230226
fn test_drop_logic() {
231227
let root = Arc::new(10);
232228
{
233-
let _a = Slab::<TestSlab>::new(Test { val: root.clone() }).unwrap();
234-
let _b = Slab::<TestSlab>::new(Test { val: root.clone() }).unwrap();
235-
let _c = Slab::<TestSlab>::new(Test { val: root.clone() }).unwrap();
229+
let _a = Slab::<TestSlab>::try_new(Test { val: root.clone() }).unwrap();
230+
let _b = Slab::<TestSlab>::try_new(Test { val: root.clone() }).unwrap();
231+
let _c = Slab::<TestSlab>::try_new(Test { val: root.clone() }).unwrap();
236232
assert_eq!(Arc::strong_count(&root), 4);
237233
}
238234
// Test that Drop was correctly called on all the members of the pool

examples/onoff_light/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn main() {
4040
};
4141
let dev_att = Box::new(dev_att::HardCodedDevAtt::new());
4242

43-
let mut matter = core::Matter::new(dev_info, dev_att, comm_data).unwrap();
43+
let mut matter = core::Matter::new(&dev_info, dev_att, &comm_data).unwrap();
4444
let dm = matter.get_data_model();
4545
{
4646
let mut node = dm.node.write().unwrap();

matter/src/acl.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ impl std::fmt::Display for AclMgr {
487487
}
488488

489489
#[cfg(test)]
490+
#[allow(clippy::bool_assert_comparison)]
490491
mod tests {
491492
use crate::{
492493
data_model::objects::{Access, Privilege},

matter/src/cert/asn1_writer.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use super::{CertConsumer, MAX_DEPTH};
1919
use crate::error::Error;
2020
use chrono::{TimeZone, Utc};
21+
use log::warn;
2122

2223
#[derive(Debug)]
2324
pub struct ASN1Writer<'a> {
@@ -255,10 +256,22 @@ impl<'a> CertConsumer for ASN1Writer<'a> {
255256
}
256257

257258
fn utctime(&mut self, _tag: &str, epoch: u32) -> Result<(), Error> {
258-
let mut matter_epoch = Utc.ymd(2000, 1, 1).and_hms(0, 0, 0).timestamp();
259+
let mut matter_epoch = Utc
260+
.with_ymd_and_hms(2000, 1, 1, 0, 0, 0)
261+
.unwrap()
262+
.timestamp();
263+
259264
matter_epoch += epoch as i64;
260265

261-
let dt = Utc.timestamp(matter_epoch, 0);
266+
let dt = match Utc.timestamp_opt(matter_epoch, 0) {
267+
chrono::LocalResult::None => return Err(Error::InvalidTime),
268+
chrono::LocalResult::Single(s) => s,
269+
chrono::LocalResult::Ambiguous(_, a) => {
270+
warn!("Ambiguous time for epoch {epoch}; returning latest timestamp: {a}");
271+
a
272+
}
273+
};
274+
262275
let time_str = format!("{}Z", dt.format("%y%m%d%H%M%S"));
263276
self.write_str(0x17, time_str.as_bytes())
264277
}

matter/src/cert/mod.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -348,20 +348,17 @@ impl DistNames {
348348
w.start_seq(tag)?;
349349
for (id, value) in &self.dn {
350350
if let Ok(tag) = num::FromPrimitive::from_u8(*id).ok_or(Error::InvalidData) {
351-
match tag {
352-
DnTags::NocCat => {
353-
w.start_set("")?;
354-
w.start_seq("")?;
355-
w.oid("Chip NOC CAT Id:", &OID_MATTER_NOC_CAT_ID)?;
356-
w.utf8str("", format!("{:08X}", value).as_str())?;
357-
w.end_seq()?;
358-
w.end_set()?;
359-
}
360-
_ => {
361-
let index: usize = (*id as usize) - (DnTags::NodeId as usize);
362-
let this = &dn_encoding[index];
363-
encode_u64_dn(*value, this.0, this.1, w)?;
364-
}
351+
if let DnTags::NocCat = tag {
352+
w.start_set("")?;
353+
w.start_seq("")?;
354+
w.oid("Chip NOC CAT Id:", &OID_MATTER_NOC_CAT_ID)?;
355+
w.utf8str("", format!("{:08X}", value).as_str())?;
356+
w.end_seq()?;
357+
w.end_set()?;
358+
} else {
359+
let index: usize = (*id as usize) - (DnTags::NodeId as usize);
360+
let this = &dn_encoding[index];
361+
encode_u64_dn(*value, this.0, this.1, w)?;
365362
}
366363
} else {
367364
error!("Non Matter DNs are not yet supported {}", id);
@@ -452,7 +449,7 @@ impl Cert {
452449

453450
pub fn as_asn1(&self, buf: &mut [u8]) -> Result<usize, Error> {
454451
let mut w = ASN1Writer::new(buf);
455-
let _ = self.encode(&mut w)?;
452+
self.encode(&mut w)?;
456453
Ok(w.as_slice().len())
457454
}
458455

@@ -657,7 +654,7 @@ mod tests {
657654

658655
for input in test_input.iter() {
659656
println!("Testing next input...");
660-
let root = tlv::get_root_node(*input).unwrap();
657+
let root = tlv::get_root_node(input).unwrap();
661658
let cert = Cert::from_tlv(&root).unwrap();
662659
let mut buf = [0u8; 1024];
663660
let buf_len = buf.len();

matter/src/cert/printer.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use super::{CertConsumer, MAX_DEPTH};
1919
use crate::error::Error;
2020
use chrono::{TimeZone, Utc};
21+
use log::warn;
2122
use std::fmt;
2223

2324
pub struct CertPrinter<'a, 'b> {
@@ -118,15 +119,23 @@ impl<'a, 'b> CertConsumer for CertPrinter<'a, 'b> {
118119
Ok(())
119120
}
120121
fn utctime(&mut self, tag: &str, epoch: u32) -> Result<(), Error> {
121-
let mut matter_epoch = Utc.ymd(2000, 1, 1).and_hms(0, 0, 0).timestamp();
122+
let mut matter_epoch = Utc
123+
.with_ymd_and_hms(2000, 1, 1, 0, 0, 0)
124+
.unwrap()
125+
.timestamp();
126+
122127
matter_epoch += epoch as i64;
123-
let _ = writeln!(
124-
self.f,
125-
"{} {} {}",
126-
SPACE[self.level],
127-
tag,
128-
Utc.timestamp(matter_epoch, 0)
129-
);
128+
129+
let dt = match Utc.timestamp_opt(matter_epoch, 0) {
130+
chrono::LocalResult::None => return Err(Error::InvalidTime),
131+
chrono::LocalResult::Single(s) => s,
132+
chrono::LocalResult::Ambiguous(_, a) => {
133+
warn!("Ambiguous time for epoch {epoch}; returning latest timestamp: {a}");
134+
a
135+
}
136+
};
137+
138+
let _ = writeln!(self.f, "{} {} {}", SPACE[self.level], tag, dt);
130139
Ok(())
131140
}
132141
}

matter/src/core.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ impl Matter {
5757
/// requires a set of device attestation certificates and keys. It is the responsibility of
5858
/// this object to return the device attestation details when queried upon.
5959
pub fn new(
60-
dev_det: BasicInfoConfig,
60+
dev_det: &BasicInfoConfig,
6161
dev_att: Box<dyn DevAttDataFetcher>,
62-
dev_comm: CommissioningData,
62+
dev_comm: &CommissioningData,
6363
) -> Result<Box<Matter>, Error> {
6464
let mdns = Mdns::get()?;
6565
mdns.set_values(dev_det.vid, dev_det.pid, dev_comm.discriminator);

matter/src/data_model/cluster_basic_information.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub struct BasicInfoCluster {
7474
}
7575

7676
impl BasicInfoCluster {
77-
pub fn new(cfg: BasicInfoConfig) -> Result<Box<Self>, Error> {
77+
pub fn new(cfg: &BasicInfoConfig) -> Result<Box<Self>, Error> {
7878
let mut cluster = Box::new(BasicInfoCluster {
7979
base: Cluster::new(ID)?,
8080
});

matter/src/data_model/core.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub struct DataModel {
5050

5151
impl DataModel {
5252
pub fn new(
53-
dev_details: BasicInfoConfig,
53+
dev_details: &BasicInfoConfig,
5454
dev_att: Box<dyn DevAttDataFetcher>,
5555
fabric_mgr: Arc<FabricMgr>,
5656
acl_mgr: Arc<AclMgr>,
@@ -171,7 +171,7 @@ impl DataModel {
171171
// Set the cluster's data version
172172
attr_encoder.set_data_ver(cluster_data_ver);
173173
let mut access_req = AccessReq::new(accessor, path, Access::READ);
174-
Cluster::read_attribute(c, &mut access_req, attr_encoder, &attr_details);
174+
Cluster::read_attribute(c, &mut access_req, attr_encoder, attr_details);
175175
Ok(())
176176
});
177177
if let Err(e) = result {

matter/src/data_model/device_types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type WriteNode<'a> = RwLockWriteGuard<'a, Box<Node>>;
3434

3535
pub fn device_type_add_root_node(
3636
node: &mut WriteNode,
37-
dev_info: BasicInfoConfig,
37+
dev_info: &BasicInfoConfig,
3838
dev_att: Box<dyn DevAttDataFetcher>,
3939
fabric_mgr: Arc<FabricMgr>,
4040
acl_mgr: Arc<AclMgr>,

0 commit comments

Comments
 (0)