Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix needless_range_loop lints #74

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix needless_range_loop lints
Use copy_nonoverlapping instead when it makes sense
sosthene-nitrokey committed Dec 20, 2024

Verified

This commit was signed with the committer’s verified signature.
commit 12447c3d51390633b137f491e457a4ea122b8d6f
14 changes: 9 additions & 5 deletions src/drivers/touch.rs
Original file line number Diff line number Diff line change
@@ -296,9 +296,9 @@ where
let results = unsafe { &mut RESULTS };
// match button {
// buttons::ButtonTop => {
for i in 0..RESULTS_LEN {
if (results[i] & (0xf << 24)) == ((channel as u32) << 24) {
results[i] = (results[i] & (!0xffff))
for item in results {
if (*item & (0xf << 24)) == ((channel as u32) << 24) {
*item = (*item & (!0xffff))
| (self.threshold[(channel as usize) - 3] as i32 + offset) as u32;
}
}
@@ -381,7 +381,9 @@ where
let mut streak = 0u32;

match ctype {
Compare::AboveThreshold => {
Compare::AboveThreshold =>
{
#[allow(clippy::needless_range_loop)]
for i in 0..(40 - AVERAGES) {
if filtered[i] > self.threshold[(5 - bufsel) as usize] {
streak += 1;
@@ -394,7 +396,9 @@ where
}
}
}
Compare::BelowThreshold => {
Compare::BelowThreshold =>
{
#[allow(clippy::needless_range_loop)]
for i in 0..(40 - AVERAGES) {
if filtered[i] < self.threshold[(5 - bufsel) as usize] {
streak += 1;
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -46,6 +46,8 @@ pub mod time;
pub mod traits;

pub mod typestates;
use core::ptr::copy_nonoverlapping;

pub use typestates::init_state::Enabled;

pub mod peripherals;
@@ -502,8 +504,8 @@ pub fn chip_revision() -> &'static str {
pub fn uuid() -> [u8; 16] {
const UUID: *const u8 = 0x0009_FC70 as _;
let mut uuid: [u8; 16] = [0; 16];
for i in 0..16 {
uuid[i] = unsafe { *UUID.offset(i as isize) };
unsafe {
copy_nonoverlapping(UUID, uuid.as_mut_ptr(), 16);
}
uuid
}
14 changes: 8 additions & 6 deletions src/peripherals/pfr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use core::result::Result;
// use cortex_m_semihosting::{heprint,heprintln};
use crate::{drivers::clocks::Clocks, typestates::init_state};
use core::ptr::copy_nonoverlapping;

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

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

for i in 0..128 {
cfpa_bytes[i] = unsafe { *cfpa_ptr.offset(i as isize) };
unsafe {
copy_nonoverlapping(cfpa_ptr, cfpa_bytes.as_mut_ptr(), 128);
}

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

const CFPA_PTR: *const u32 = (0x0009_DE00 + 512) as *const u32;
for i in 0..128 {
cfpa_bytes[i] = unsafe { *CFPA_PTR.offset(i as isize) };
unsafe {
copy_nonoverlapping(CFPA_PTR, cfpa_bytes.as_mut_ptr(), 128);
}

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

const CFPA_PTR: *const u32 = (0x0009_DE00 + 512 + 512) as *const u32;
for i in 0..128 {
cfpa_bytes[i] = unsafe { *CFPA_PTR.offset(i as isize) };
unsafe {
copy_nonoverlapping(CFPA_PTR, cfpa_bytes.as_mut_ptr(), 128);
}

let cfpa: &Cfpa = unsafe { core::mem::transmute(cfpa_bytes.as_ptr()) };
8 changes: 2 additions & 6 deletions src/peripherals/puf.rs
Original file line number Diff line number Diff line change
@@ -117,9 +117,7 @@ impl<T> Puf<init_state::Enabled<T>> {
assert!(key_size >= 64);
assert!(key_index < 16);

for i in 0..key_code.len() {
key_code[i] = 0;
}
key_code.fill(0);

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

for i in 0..ac_buffer.len() {
ac_buffer[i] = 0;
}
ac_buffer.fill(0);

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