Skip to content

Commit 1ec92e6

Browse files
Fix needless_range_loop lints
Use copy_nonoverlapping instead when it makes sense
1 parent 4f1c820 commit 1ec92e6

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

src/drivers/touch.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,9 @@ where
296296
let results = unsafe { &mut RESULTS };
297297
// match button {
298298
// buttons::ButtonTop => {
299-
for i in 0..RESULTS_LEN {
300-
if (results[i] & (0xf << 24)) == ((channel as u32) << 24) {
301-
results[i] = (results[i] & (!0xffff))
299+
for item in results {
300+
if (*item & (0xf << 24)) == ((channel as u32) << 24) {
301+
*item = (*item & (!0xffff))
302302
| (self.threshold[(channel as usize) - 3] as i32 + offset) as u32;
303303
}
304304
}
@@ -381,7 +381,9 @@ where
381381
let mut streak = 0u32;
382382

383383
match ctype {
384-
Compare::AboveThreshold => {
384+
Compare::AboveThreshold =>
385+
{
386+
#[allow(clippy::needless_range_loop)]
385387
for i in 0..(40 - AVERAGES) {
386388
if filtered[i] > self.threshold[(5 - bufsel) as usize] {
387389
streak += 1;
@@ -394,7 +396,9 @@ where
394396
}
395397
}
396398
}
397-
Compare::BelowThreshold => {
399+
Compare::BelowThreshold =>
400+
{
401+
#[allow(clippy::needless_range_loop)]
398402
for i in 0..(40 - AVERAGES) {
399403
if filtered[i] < self.threshold[(5 - bufsel) as usize] {
400404
streak += 1;

src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ pub mod time;
4646
pub mod traits;
4747

4848
pub mod typestates;
49+
use core::ptr::copy_nonoverlapping;
50+
4951
pub use typestates::init_state::Enabled;
5052

5153
pub mod peripherals;
@@ -502,8 +504,8 @@ pub fn chip_revision() -> &'static str {
502504
pub fn uuid() -> [u8; 16] {
503505
const UUID: *const u8 = 0x0009_FC70 as _;
504506
let mut uuid: [u8; 16] = [0; 16];
505-
for i in 0..16 {
506-
uuid[i] = unsafe { *UUID.offset(i as isize) };
507+
unsafe {
508+
copy_nonoverlapping(UUID, uuid.as_mut_ptr(), 16);
507509
}
508510
uuid
509511
}

src/peripherals/pfr.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use core::result::Result;
22
// use cortex_m_semihosting::{heprint,heprintln};
33
use crate::{drivers::clocks::Clocks, typestates::init_state};
4+
use core::ptr::copy_nonoverlapping;
45

56
#[derive(Copy, Clone, PartialEq)]
67
pub enum KeyType {
@@ -333,6 +334,7 @@ impl Pfr<init_state::Enabled> {
333334
/// returns previous versions of the CFPA page (not seen on scratch, ping, or pong pages).
334335
/// This method always returns the most recently updated Cfpa from ping or pong pages.
335336
pub fn read_latest_cfpa(&mut self) -> Result<Cfpa, u32> {
337+
use core::ptr::copy_nonoverlapping;
336338
let mut cfpa_bytes = [0u32; 128];
337339

338340
let ping_ptr = (0x0009_DE00 + 512) as *const u32;
@@ -347,8 +349,8 @@ impl Pfr<init_state::Enabled> {
347349
pong_ptr
348350
};
349351

350-
for i in 0..128 {
351-
cfpa_bytes[i] = unsafe { *cfpa_ptr.offset(i as isize) };
352+
unsafe {
353+
copy_nonoverlapping(cfpa_ptr, cfpa_bytes.as_mut_ptr(), 128);
352354
}
353355

354356
let cfpa: &Cfpa = unsafe { core::mem::transmute(cfpa_bytes.as_ptr()) };
@@ -360,8 +362,8 @@ impl Pfr<init_state::Enabled> {
360362
let mut cfpa_bytes = [0u32; 128];
361363

362364
const CFPA_PTR: *const u32 = (0x0009_DE00 + 512) as *const u32;
363-
for i in 0..128 {
364-
cfpa_bytes[i] = unsafe { *CFPA_PTR.offset(i as isize) };
365+
unsafe {
366+
copy_nonoverlapping(CFPA_PTR, cfpa_bytes.as_mut_ptr(), 128);
365367
}
366368

367369
let cfpa: &Cfpa = unsafe { core::mem::transmute(cfpa_bytes.as_ptr()) };
@@ -373,8 +375,8 @@ impl Pfr<init_state::Enabled> {
373375
let mut cfpa_bytes = [0u32; 128];
374376

375377
const CFPA_PTR: *const u32 = (0x0009_DE00 + 512 + 512) as *const u32;
376-
for i in 0..128 {
377-
cfpa_bytes[i] = unsafe { *CFPA_PTR.offset(i as isize) };
378+
unsafe {
379+
copy_nonoverlapping(CFPA_PTR, cfpa_bytes.as_mut_ptr(), 128);
378380
}
379381

380382
let cfpa: &Cfpa = unsafe { core::mem::transmute(cfpa_bytes.as_ptr()) };

src/peripherals/puf.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,7 @@ impl<T> Puf<init_state::Enabled<T>> {
117117
assert!(key_size >= 64);
118118
assert!(key_index < 16);
119119

120-
for i in 0..key_code.len() {
121-
key_code[i] = 0;
122-
}
120+
key_code.fill(0);
123121

124122
self.raw
125123
.keysize
@@ -166,9 +164,7 @@ impl Puf<init_state::Enabled> {
166164
return Err(Error::NotAllowed);
167165
}
168166

169-
for i in 0..ac_buffer.len() {
170-
ac_buffer[i] = 0;
171-
}
167+
ac_buffer.fill(0);
172168

173169
self.raw.ctrl.write(|w| w.enroll().set_bit());
174170

0 commit comments

Comments
 (0)