Skip to content

Commit c8bd6b9

Browse files
authored
Depanic effort (#10)
This pull request introduces several improvements to code quality, safety, and CI workflows. The most significant changes include stricter linting rules, improvements to the `SA0` enum, and updates to the GitHub Actions workflow for CI checks. **Linting and Code Safety Improvements:** * Added a `[lints.rust]` section to `Cargo.toml` to forbid the use of `unsafe_code`. Updated `[lints.clippy]` to deny a broader set of lints, including `unwrap_used`, `todo`, `unimplemented`, and others, making the codebase more robust and less prone to runtime errors. * Added `#[allow(clippy::unwrap_used)]` with safety comments in the accelerometer reading code, making explicit where `unwrap` is considered safe due to slice length guarantees. **Codebase Simplification and Modernization:** * Updated the `SA0` enum to derive `Default` and use the `#[default]` attribute for the `Gnd` variant, removing the manual `impl Default` block and simplifying the code. **CI/CD Workflow Updates:** * Replaced the use of the `giraffate/clippy-action` with a direct `cargo clippy -- -Dwarnings` command in the GitHub Actions workflow, streamlining the linting process. * Enabled the previously commented-out `semver` check in the CI workflow, ensuring that semantic versioning checks are now part of the automated pipeline.
1 parent c93e811 commit c8bd6b9

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

.github/workflows/check.yml

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,22 @@ jobs:
5757
toolchain: ${{ matrix.toolchain }}
5858
components: clippy
5959
- name: cargo clippy
60-
uses: giraffate/clippy-action@v1
61-
with:
62-
reporter: 'github-pr-check'
63-
github_token: ${{ secrets.GITHUB_TOKEN }}
60+
run: cargo clippy -- -Dwarnings
6461

6562
# Enable once we have a released crate
66-
# semver:
67-
# runs-on: ubuntu-latest
68-
# name: semver
69-
# steps:
70-
# - uses: actions/checkout@v4
71-
# with:
72-
# submodules: true
73-
# - name: Install stable
74-
# uses: dtolnay/rust-toolchain@stable
75-
# with:
76-
# components: rustfmt
77-
# - name: cargo-semver-checks
78-
# uses: obi1kenobi/cargo-semver-checks-action@v2
63+
semver:
64+
runs-on: ubuntu-latest
65+
name: semver
66+
steps:
67+
- uses: actions/checkout@v4
68+
with:
69+
submodules: true
70+
- name: Install stable
71+
uses: dtolnay/rust-toolchain@stable
72+
with:
73+
components: rustfmt
74+
- name: cargo-semver-checks
75+
uses: obi1kenobi/cargo-semver-checks-action@v2
7976

8077
doc:
8178
# run docs generation on nightly rather than stable. This enables features like

Cargo.toml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,19 @@ tokio = { version = "1.42.0", features = ["rt", "macros"] }
2525
[target.'cfg(target_os = "none")'.dependencies]
2626
# dependencies for no-std targets
2727

28+
[lints.rust]
29+
unsafe_code = "forbid"
30+
2831
[lints.clippy]
29-
suspicious = "forbid"
30-
correctness = "forbid"
31-
perf = "forbid"
32-
style = "forbid"
32+
correctness = "deny"
33+
expect_used = "deny"
34+
indexing_slicing = "deny"
35+
panic = "deny"
36+
panic_in_result_fn = "deny"
37+
perf = "deny"
38+
suspicious = "deny"
39+
style = "deny"
40+
todo = "deny"
41+
unimplemented = "deny"
42+
unreachable = "deny"
43+
unwrap_used = "deny"

src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,15 @@ pub mod registers;
1818
pub use registers::*;
1919

2020
/// SA0 pin logic level representation.
21+
#[derive(Default)]
2122
pub enum SA0 {
23+
#[default]
2224
/// SA0 tied to GND (default).
2325
Gnd,
2426
/// SA0 tied to V+.
2527
Vplus,
2628
}
2729

28-
impl Default for SA0 {
29-
fn default() -> Self {
30-
Self::Gnd
31-
}
32-
}
33-
3430
impl From<SA0> for u8 {
3531
fn from(connection: SA0) -> Self {
3632
match connection {
@@ -180,8 +176,14 @@ impl<I2C: embedded_hal_async::i2c::I2c, DELAY: embedded_hal_async::delay::DelayN
180176
let mut acc_bytes: [u8; 6] = [0; 6];
181177
self.read_regs(Register::XOutLow, &mut acc_bytes).await?;
182178
Ok((
179+
#[allow(clippy::unwrap_used)]
180+
// panic safety: slice is always of length 2
183181
i16::from_le_bytes(acc_bytes[0..2].try_into().unwrap()),
182+
#[allow(clippy::unwrap_used)]
183+
// panic safety: slice is always of length 2
184184
i16::from_le_bytes(acc_bytes[2..4].try_into().unwrap()),
185+
#[allow(clippy::unwrap_used)]
186+
// panic safety: slice is always of length 2
185187
i16::from_le_bytes(acc_bytes[4..6].try_into().unwrap()),
186188
))
187189
}

0 commit comments

Comments
 (0)