Skip to content

Commit 4f705c8

Browse files
authored
Merge pull request #36 from riscv-rust/clippy
Apply clippy changes
2 parents 0610364 + 36e3a8a commit 4f705c8

File tree

21 files changed

+195
-148
lines changed

21 files changed

+195
-148
lines changed

.github/workflows/clippy.yaml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
on:
2+
push:
3+
branches: [ master ]
4+
pull_request:
5+
merge_group:
6+
7+
name: Lints compliance check
8+
9+
env:
10+
# Bypass clippy warnings produced by Svd2Rust
11+
CLIPPY_PARAMS: -W clippy::all -D warnings -A clippy::module_inception -A clippy::needless_lifetimes
12+
13+
jobs:
14+
clippy:
15+
strategy:
16+
matrix:
17+
board: [hifive1, hifive1-revb, redv, lofive, lofive-r1]
18+
toolchain: [ stable, nightly ]
19+
include:
20+
# Nightly is only for reference and allowed to fail
21+
- toolchain: nightly
22+
experimental: true
23+
runs-on: ubuntu-latest
24+
continue-on-error: ${{ matrix.experimental || false }}
25+
steps:
26+
- uses: actions/checkout@v4
27+
- name: Update Rust toolchain and install Clippy
28+
run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }} && rustup component add clippy
29+
- name: Install Rust target
30+
run: rustup target install riscv32imc-unknown-none-elf
31+
- name: Run clippy (direct mode)
32+
run: cargo clippy --features board-${{ matrix.board }} -- $CLIPPY_PARAMS
33+
- name: Run clippy (vectored mode)
34+
run: cargo clippy --features virq,board-${{ matrix.board }} -- $CLIPPY_PARAMS
35+
36+
# Job to check that all the lint checks succeeded
37+
clippy-check:
38+
needs:
39+
- clippy
40+
runs-on: ubuntu-latest
41+
if: always()
42+
steps:
43+
- run: jq --exit-status 'all(.result == "success")' <<< '${{ toJson(needs) }}'

e310x-hal/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
## [Unreleased]
99

1010
### Changed
11+
- Apply clippy changes
1112
- Use `portable-atomic` with `zaamo` feature to use native `amo*` operations.
1213
- Official target is now `riscv32imc-unknown-none-elf`, as it does not fully support the A extension.
1314
- Update `e310x` dependency and adapt code

e310x-hal/src/clock.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,10 @@ impl CoreClk {
175175
/// The resulting frequency may differ by 0-2% from the requested
176176
fn configure_pll(&self, pllref_freq: Hertz, divout_freq: Hertz) -> Hertz {
177177
let pllref_freq = pllref_freq.0;
178-
assert!(PLLREF_MIN <= pllref_freq && pllref_freq <= PLLREF_MAX);
178+
assert!((PLLREF_MIN..=PLLREF_MAX).contains(&pllref_freq));
179179

180180
let divout_freq = divout_freq.0;
181-
assert!(DIVOUT_MIN <= divout_freq && divout_freq <= DIVOUT_MAX);
181+
assert!((DIVOUT_MIN..=DIVOUT_MAX).contains(&divout_freq));
182182

183183
// Calculate PLL Output Divider settings
184184
let divider_div;
@@ -205,7 +205,7 @@ impl CoreClk {
205205
2 * (divider_div + 1)
206206
};
207207
let pllout_freq = divout_freq * d;
208-
assert!(PLLOUT_MIN <= pllout_freq && pllout_freq <= PLLOUT_MAX);
208+
assert!((PLLOUT_MIN..=PLLOUT_MAX).contains(&pllout_freq));
209209

210210
// Calculate PLL R ratio
211211
let r = match pllref_freq {
@@ -218,7 +218,7 @@ impl CoreClk {
218218

219219
// Calculate refr frequency
220220
let refr_freq = pllref_freq / r;
221-
assert!(REFR_MIN <= refr_freq && refr_freq <= REFR_MAX);
221+
assert!((REFR_MIN..=REFR_MAX).contains(&refr_freq));
222222

223223
// Calculate PLL Q ratio
224224
let q = match pllout_freq {
@@ -230,7 +230,7 @@ impl CoreClk {
230230

231231
// Calculate the desired vco frequency
232232
let target_vco_freq = pllout_freq * q;
233-
assert!(VCO_MIN <= target_vco_freq && target_vco_freq <= VCO_MAX);
233+
assert!((VCO_MIN..=VCO_MAX).contains(&target_vco_freq));
234234

235235
// Calculate PLL F ratio
236236
let f = target_vco_freq / refr_freq;
@@ -249,15 +249,15 @@ impl CoreClk {
249249
} else {
250250
(f_lo, vco_lo)
251251
};
252-
assert!(VCO_MIN <= vco_freq && vco_freq <= VCO_MAX);
252+
assert!((VCO_MIN..=VCO_MAX).contains(&vco_freq));
253253

254254
// Calculate actual pllout frequency
255255
let pllout_freq = vco_freq / q;
256-
assert!(PLLOUT_MIN <= pllout_freq && pllout_freq <= PLLOUT_MAX);
256+
assert!((PLLOUT_MIN..=PLLOUT_MAX).contains(&pllout_freq));
257257

258258
// Calculate actual divout frequency
259259
let divout_freq = pllout_freq / d;
260-
assert!(DIVOUT_MIN <= divout_freq && divout_freq <= DIVOUT_MAX);
260+
assert!((DIVOUT_MIN..=DIVOUT_MAX).contains(&divout_freq));
261261

262262
// Calculate bit-values
263263
let r: u8 = (r - 1) as u8;

e310x-hal/src/core/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ impl CorePeripherals {
2626
}
2727

2828
/// Steal the peripherals
29+
///
30+
/// # Safety
31+
///
32+
/// Using this function may break the guarantees of the singleton pattern.
2933
pub unsafe fn steal() -> Self {
3034
let p = e310x::Peripherals::steal();
3135
Self::new(p.clint, p.plic)

e310x-hal/src/core/plic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ impl Priority {
4343
}
4444
}
4545

46-
impl Into<u32> for Priority {
46+
impl From<Priority> for u32 {
4747
/// Returns the numeric priority for writing to a
4848
/// interrupt priority or the plic threshold register.
49-
fn into(self) -> u32 {
50-
match self {
49+
fn from(val: Priority) -> Self {
50+
match val {
5151
Priority::P0 => 0,
5252
Priority::P1 => 1,
5353
Priority::P2 => 2,

e310x-hal/src/delay.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use embedded_hal::blocking::delay::{DelayMs, DelayUs};
66
use riscv::register::{mie, mip};
77

88
/// Machine timer (mtime) as a busyloop delay provider
9+
#[derive(Default)]
910
pub struct Delay;
1011

1112
const TICKS_PER_SECOND: u64 = 32768;

e310x-hal/src/device.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ impl DeviceResources {
173173
e310x::Peripherals::take().map(DeviceResources::from)
174174
}
175175

176-
/// Unchecked version of `DeviceResources::take`
176+
/// Unchecked version of [`DeviceResources::take`].
177+
///
178+
/// # Safety
179+
///
180+
/// Using this function may break the guarantees of the singleton pattern.
177181
pub unsafe fn steal() -> Self {
178182
e310x::Peripherals::steal().into()
179183
}

e310x-hal/src/i2c.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ use e310x::{i2c0, I2c0};
1919
use embedded_hal::blocking::i2c::{Read, Write, WriteRead};
2020

2121
/// SDA pin - DO NOT IMPLEMENT THIS TRAIT
22-
pub unsafe trait SdaPin<I2C> {}
23-
/// SCL pin - DO NOT IMPLEMENT THIS TRAIT
24-
pub unsafe trait SclPin<I2C> {}
22+
mod sealed {
23+
/// SDA pin
24+
pub trait SdaPin<I2C> {}
2525

26-
unsafe impl<T> SdaPin<I2c0> for gpio0::Pin12<IOF0<T>> {}
27-
unsafe impl<T> SclPin<I2c0> for gpio0::Pin13<IOF0<T>> {}
26+
/// SCL pin
27+
pub trait SclPin<I2C> {}
28+
}
29+
30+
impl<T> sealed::SdaPin<I2c0> for gpio0::Pin12<IOF0<T>> {}
31+
impl<T> sealed::SclPin<I2c0> for gpio0::Pin13<IOF0<T>> {}
2832

2933
/// I2C error
3034
#[derive(Debug, Eq, PartialEq)]
@@ -61,8 +65,8 @@ impl<SDA, SCL> I2c<I2c0, (SDA, SCL)> {
6165
/// Configures an I2C peripheral
6266
pub fn new(i2c: I2c0, sda: SDA, scl: SCL, speed: Speed, clocks: Clocks) -> Self
6367
where
64-
SDA: SdaPin<I2c0>,
65-
SCL: SclPin<I2c0>,
68+
SDA: sealed::SdaPin<I2c0>,
69+
SCL: sealed::SclPin<I2c0>,
6670
{
6771
// Calculate prescaler value
6872
let desired_speed = match speed {
@@ -115,13 +119,13 @@ impl<I2C: Deref<Target = i2c0::RegisterBlock>, PINS> I2c<I2C, PINS> {
115119
{
116120
self.i2c.cr().write(|w| unsafe {
117121
let mut value: u32 = 0;
118-
f(mem::transmute(&mut value));
122+
f(mem::transmute::<&mut u32, &mut i2c0::cr::W>(&mut value));
119123
w.bits(value)
120124
});
121125
}
122126

123127
fn read_sr(&self) -> i2c0::sr::R {
124-
unsafe { mem::transmute(self.i2c.sr().read()) }
128+
self.i2c.sr().read()
125129
}
126130

127131
fn write_byte(&self, byte: u8) {

e310x-hal/src/pmu.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub trait PMUExt {
132132
///
133133
/// Stores user data `UD` to backup registers.
134134
///
135-
/// # *WARNING*
135+
/// # Safety
136136
///
137137
/// `user_data` value must not contain un-serializable types such as pointers or references.
138138
///
@@ -160,7 +160,7 @@ pub trait PMUExt {
160160
///
161161
/// Restores user data `UD` from backup registers.
162162
///
163-
/// # *WARNING*
163+
/// # Safety
164164
///
165165
/// `user_data` value must not contain un-serializable types such as pointers or references.
166166
///
@@ -269,9 +269,9 @@ impl PMUExt for Pmu {
269269
let ptr_u32 = ptr as *const u32;
270270
let sliced = core::slice::from_raw_parts(ptr_u32, reg_count);
271271

272-
for i in 0..sliced.len() {
273-
backup.backup(i).write(|w| w.bits(sliced[i]));
274-
}
272+
backup.backup_iter().enumerate().for_each(|(i, backup_r)| {
273+
backup_r.write(|w| w.bits(sliced[i]));
274+
});
275275

276276
Ok(())
277277
}
@@ -297,9 +297,9 @@ impl PMUExt for Pmu {
297297
let ptr_u32 = ptr as *mut u32;
298298
let sliced = core::slice::from_raw_parts_mut(ptr_u32, reg_count);
299299

300-
for i in 0..sliced.len() {
301-
sliced[i] = backup.backup(i).read().bits();
302-
}
300+
backup.backup_iter().enumerate().for_each(|(i, backup_r)| {
301+
sliced[i] = backup_r.read().bits();
302+
});
303303

304304
Ok(())
305305
}

e310x-hal/src/pwm.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ mod pwm2_impl {
9191
}
9292

9393
/// PWM channel
94+
#[derive(Copy, Clone)]
9495
pub struct Channel<PWM> {
9596
_pwm: PhantomData<PWM>,
9697
cmp_index: CmpIndex,
@@ -109,17 +110,6 @@ impl<PWM> Channel<PWM> {
109110
}
110111
}
111112

112-
impl<PWM> Clone for Channel<PWM> {
113-
fn clone(&self) -> Self {
114-
Self {
115-
_pwm: self._pwm.clone(),
116-
cmp_index: self.cmp_index.clone(),
117-
}
118-
}
119-
}
120-
121-
impl<PWM> Copy for Channel<PWM> {}
122-
123113
#[doc(hidden)]
124114
pub trait PwmX: Deref<Target = pwm0::RegisterBlock> {
125115
type CmpWidth: Ord;

0 commit comments

Comments
 (0)