Skip to content

Commit b359b07

Browse files
Merge #123
123: Added clippy to the CI workflow r=almindor a=romancardenas I've been working with this crate for a while and noticed that it didn't include clippy as part of the CI process. This PR checks that clippy does not trigger any warning for new contributions. I also ran `cargo clippy --fix` to "fix" the crate. If you think this is useful, I can adapt other repos (e.g., riscv-rt or e310x). Cheers! Co-authored-by: Román Cárdenas <[email protected]>
2 parents 620c5bf + 2d68fbf commit b359b07

File tree

8 files changed

+49
-15
lines changed

8 files changed

+49
-15
lines changed

.github/workflows/clippy.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Clippy
2+
3+
on:
4+
push:
5+
branches: [ staging, trying, master ]
6+
pull_request:
7+
branches: [ master ]
8+
9+
defaults:
10+
run:
11+
shell: bash
12+
13+
env:
14+
CLIPPY_PARAMS: -W clippy::all -W clippy::pedantic -W clippy::nursery -W clippy::cargo
15+
16+
jobs:
17+
clippy:
18+
name: Clippy
19+
runs-on: ubuntu-latest
20+
strategy:
21+
matrix:
22+
cargo_flags:
23+
- "--no-default-features"
24+
- "--all-features"
25+
steps:
26+
- name: Checkout source code
27+
uses: actions/checkout@v3
28+
29+
- name: Install Rust toolchain
30+
uses: dtolnay/rust-toolchain@stable
31+
with:
32+
toolchain: stable
33+
components: clippy
34+
35+
- name: Run clippy
36+
run: cargo clippy --all ${{ matrix.cargo_flags }} -- -D warnings

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
//! a target-specific implementation instead, typically provided by a HAL or RTOS crate.
2727
2828
#![no_std]
29+
#![allow(clippy::missing_safety_doc)]
2930

3031
pub mod asm;
3132
pub mod delay;

src/register/mcounteren.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl Mcounteren {
3030
/// Supervisor "hpm\[x\]" Enable (bits 3-31)
3131
#[inline]
3232
pub fn hpm(&self, index: usize) -> bool {
33-
assert!(3 <= index && index < 32);
33+
assert!((3..32).contains(&index));
3434
self.bits.get_bit(index)
3535
}
3636
}
@@ -54,12 +54,12 @@ set_clear_csr!(
5454

5555
#[inline]
5656
pub unsafe fn set_hpm(index: usize) {
57-
assert!(3 <= index && index < 32);
57+
assert!((3..32).contains(&index));
5858
_set(1 << index);
5959
}
6060

6161
#[inline]
6262
pub unsafe fn clear_hpm(index: usize) {
63-
assert!(3 <= index && index < 32);
63+
assert!((3..32).contains(&index));
6464
_clear(1 << index);
6565
}

src/register/mstatus.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// which would be the best way we implement this using Rust?
99

1010
use bit_field::BitField;
11-
use core::mem::size_of;
1211

1312
/// mstatus register
1413
#[derive(Clone, Copy, Debug)]
@@ -205,7 +204,7 @@ impl Mstatus {
205204
/// signals the presence of some dirty state
206205
#[inline]
207206
pub fn sd(&self) -> bool {
208-
self.bits.get_bit(size_of::<usize>() * 8 - 1)
207+
self.bits.get_bit(usize::BITS as usize - 1)
209208
}
210209
}
211210

src/register/pmpcfgx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl Pmpcsr {
7272
3 => Range::NAPOT,
7373
_ => unreachable!(),
7474
},
75-
locked: byte.get_bit(7) as bool,
75+
locked: byte.get_bit(7),
7676
}
7777
}
7878
}

src/register/scause.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! scause register
22
33
use bit_field::BitField;
4-
use core::mem::size_of;
54

65
/// scause register
76
#[derive(Clone, Copy)]
@@ -90,7 +89,7 @@ impl Scause {
9089
/// Returns the code field
9190
#[inline]
9291
pub fn code(&self) -> usize {
93-
let bit = 1 << (size_of::<usize>() * 8 - 1);
92+
let bit = 1 << (usize::BITS as usize - 1);
9493
self.bits & !bit
9594
}
9695

@@ -107,7 +106,7 @@ impl Scause {
107106
/// Is trap cause an interrupt.
108107
#[inline]
109108
pub fn is_interrupt(&self) -> bool {
110-
self.bits.get_bit(size_of::<usize>() * 8 - 1)
109+
self.bits.get_bit(usize::BITS as usize - 1)
111110
}
112111

113112
/// Is trap cause an exception.
@@ -139,7 +138,7 @@ pub unsafe fn set(cause: Trap) {
139138
Interrupt::UserExternal => 8,
140139
Interrupt::SupervisorExternal => 9,
141140
Interrupt::Unknown => panic!("unknown interrupt"),
142-
} | (1 << (size_of::<usize>() * 8 - 1)))
141+
} | (1 << (usize::BITS as usize - 1)))
143142
} // interrupt bit is 1
144143
Trap::Exception(e) => match e {
145144
Exception::InstructionMisaligned => 0,

src/register/scounteren.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl Scounteren {
3030
/// User "hpm\[x\]" Enable (bits 3-31)
3131
#[inline]
3232
pub fn hpm(&self, index: usize) -> bool {
33-
assert!(3 <= index && index < 32);
33+
assert!((3..32).contains(&index));
3434
self.bits.get_bit(index)
3535
}
3636
}
@@ -54,12 +54,12 @@ set_clear_csr!(
5454

5555
#[inline]
5656
pub unsafe fn set_hpm(index: usize) {
57-
assert!(3 <= index && index < 32);
57+
assert!((3..32).contains(&index));
5858
_set(1 << index);
5959
}
6060

6161
#[inline]
6262
pub unsafe fn clear_hpm(index: usize) {
63-
assert!(3 <= index && index < 32);
63+
assert!((3..32).contains(&index));
6464
_clear(1 << index);
6565
}

src/register/sstatus.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
pub use super::mstatus::FS;
44
use bit_field::BitField;
5-
use core::mem::size_of;
65

76
/// Supervisor Status Register
87
#[derive(Clone, Copy, Debug)]
@@ -92,7 +91,7 @@ impl Sstatus {
9291
/// signals the presence of some dirty state
9392
#[inline]
9493
pub fn sd(&self) -> bool {
95-
self.bits.get_bit(size_of::<usize>() * 8 - 1)
94+
self.bits.get_bit(usize::BITS as usize - 1)
9695
}
9796
}
9897

0 commit comments

Comments
 (0)