diff mbox series

[libgpiod] bindings: rust: fix clippy lint warnings

Message ID 20230612154055.56556-1-warthog618@gmail.com
State New
Headers show
Series [libgpiod] bindings: rust: fix clippy lint warnings | expand

Commit Message

Kent Gibson June 12, 2023, 3:40 p.m. UTC
clippy from Rust 1.70 reports a host of warnings due to casting and type
conversions across the FFI interface to libgpiod.
These casts and conversions are required to support old versions of Rust
that do not support recent Rust FFI extensions.

Disable the warnings on a case by case basis, and move
casting/conversion inside the corresponding unsafe section where
appropriate to highlight that the FFI conversion is the cause.

clippy also finds a couple of genuine uneccessary casts, so remove
those too.

Signed-off-by: Kent Gibson <warthog618@gmail.com>
---

There is also at least one instance of reformatting by rustfmt,
but that seems appropriate too.

Cheers,
Kent.

 bindings/rust/gpiosim-sys/src/lib.rs         |  1 +
 bindings/rust/gpiosim-sys/src/sim.rs         |  6 ++++--
 bindings/rust/libgpiod/src/chip.rs           |  5 ++++-
 bindings/rust/libgpiod/src/edge_event.rs     |  1 +
 bindings/rust/libgpiod/src/event_buffer.rs   |  3 +++
 bindings/rust/libgpiod/src/info_event.rs     |  1 +
 bindings/rust/libgpiod/src/lib.rs            |  1 +
 bindings/rust/libgpiod/src/line_config.rs    | 11 +++++++----
 bindings/rust/libgpiod/src/line_info.rs      |  1 +
 bindings/rust/libgpiod/src/line_request.rs   | 14 +++++++++-----
 bindings/rust/libgpiod/src/line_settings.rs  |  2 ++
 bindings/rust/libgpiod/src/request_config.rs |  5 ++++-
 bindings/rust/libgpiod/tests/chip.rs         |  2 +-
 13 files changed, 39 insertions(+), 14 deletions(-)

Comments

Kent Gibson June 14, 2023, 8:29 a.m. UTC | #1
On Wed, Jun 14, 2023 at 10:14:08AM +0200, Erik Schilling wrote:
> On Mon Jun 12, 2023 at 5:40 PM CEST, Kent Gibson wrote:
> > clippy from Rust 1.70 reports a host of warnings due to casting and type
> > conversions across the FFI interface to libgpiod.
> > These casts and conversions are required to support old versions of Rust
> > that do not support recent Rust FFI extensions.
> 
> Could you elaborate which extensions are relevant here? Would it be
> realistic to just update the minimum Rust version instead of needing
> to include these suppression directives?
> 

Types were added in core::ffi[1] in 1.64 for just this purpose.
e.g. c_uint[2]
Though c_size_t[3] still remains in Experimental.

And I guess the clippy lints followed soon after.

Wrt setting the MSRV, but I assumed not, hence the allows.

Cheers,
Kent.

[1] https://doc.rust-lang.org/stable/core/ffi/index.html
[2] https://doc.rust-lang.org/stable/core/ffi/type.c_uint.html
[3] https://doc.rust-lang.org/stable/core/ffi/type.c_size_t.html
Erik Schilling June 14, 2023, 8:40 a.m. UTC | #2
On Wed Jun 14, 2023 at 10:29 AM CEST, Kent Gibson wrote:
> On Wed, Jun 14, 2023 at 10:14:08AM +0200, Erik Schilling wrote:
> > On Mon Jun 12, 2023 at 5:40 PM CEST, Kent Gibson wrote:
> > > clippy from Rust 1.70 reports a host of warnings due to casting and type
> > > conversions across the FFI interface to libgpiod.
> > > These casts and conversions are required to support old versions of Rust
> > > that do not support recent Rust FFI extensions.
> > 
> > Could you elaborate which extensions are relevant here? Would it be
> > realistic to just update the minimum Rust version instead of needing
> > to include these suppression directives?
> > 
>
> Types were added in core::ffi[1] in 1.64 for just this purpose.
> e.g. c_uint[2]
> Though c_size_t[3] still remains in Experimental.
>
> And I guess the clippy lints followed soon after.
>
> Wrt setting the MSRV, but I assumed not, hence the allows.

For me bindgen seems to generate usize of size_t, thats why I asked.
Does that depend on the Rust version somehow? Or more concretely:
When will things like `gpiod_line_config_get_num_configured_offsets`
not get translated to `usize` so that we need a cast?

On my end (with latest toolchain and nightly), I do not see any
clippy warnings with `cargo clippy`. How exactly did you produce those
warnings?
Erik Schilling June 14, 2023, 9:16 a.m. UTC | #3
On Wed Jun 14, 2023 at 11:06 AM CEST, Kent Gibson wrote:
> On Wed, Jun 14, 2023 at 10:40:56AM +0200, Erik Schilling wrote:
> > On Wed Jun 14, 2023 at 10:29 AM CEST, Kent Gibson wrote:
> > > On Wed, Jun 14, 2023 at 10:14:08AM +0200, Erik Schilling wrote:
> > > > On Mon Jun 12, 2023 at 5:40 PM CEST, Kent Gibson wrote:
> > > > > clippy from Rust 1.70 reports a host of warnings due to casting and type
> > > > > conversions across the FFI interface to libgpiod.
> > > > > These casts and conversions are required to support old versions of Rust
> > > > > that do not support recent Rust FFI extensions.
> > > > 
> > > > Could you elaborate which extensions are relevant here? Would it be
> > > > realistic to just update the minimum Rust version instead of needing
> > > > to include these suppression directives?
> > > > 
> > >
> > > Types were added in core::ffi[1] in 1.64 for just this purpose.
> > > e.g. c_uint[2]
> > > Though c_size_t[3] still remains in Experimental.
> > >
> > > And I guess the clippy lints followed soon after.
> > >
> > > Wrt setting the MSRV, but I assumed not, hence the allows.
> > 
> > For me bindgen seems to generate usize of size_t, thats why I asked.
> > Does that depend on the Rust version somehow? Or more concretely:
> > When will things like `gpiod_line_config_get_num_configured_offsets`
> > not get translated to `usize` so that we need a cast?
> > 
>
> No idea - outside my area.
>
> > On my end (with latest toolchain and nightly), I do not see any
> > clippy warnings with `cargo clippy`. How exactly did you produce those
> > warnings?
> > 
>
> Interesting.  With stable on libgpiod master in the rust/libgpiod
> directory, and with these in my environment:

Oh... My fault... I saw the -sys somewhere and tried to run things in
libgpiod-sys (after inspecting the generated bindings). Need to revisit
this in a calm moment.

Thanks for the additional detail!

- Erik
Erik Schilling June 19, 2023, 7:36 a.m. UTC | #4
Sorry, got distracted while sorting out the MSRV mess that I sent a
separate patch for [4].

On Wed Jun 14, 2023 at 10:29 AM CEST, Kent Gibson wrote:
> On Wed, Jun 14, 2023 at 10:14:08AM +0200, Erik Schilling wrote:
> > On Mon Jun 12, 2023 at 5:40 PM CEST, Kent Gibson wrote:
> > > clippy from Rust 1.70 reports a host of warnings due to casting and type
> > > conversions across the FFI interface to libgpiod.
> > > These casts and conversions are required to support old versions of Rust
> > > that do not support recent Rust FFI extensions.
> > 
> > Could you elaborate which extensions are relevant here? Would it be
> > realistic to just update the minimum Rust version instead of needing
> > to include these suppression directives?
> > 
>
> Types were added in core::ffi[1] in 1.64 for just this purpose.
> e.g. c_uint[2]
> Though c_size_t[3] still remains in Experimental.
>
> [1] https://doc.rust-lang.org/stable/core/ffi/index.html
> [2] https://doc.rust-lang.org/stable/core/ffi/type.c_uint.html
> [3] https://doc.rust-lang.org/stable/core/ffi/type.c_size_t.html

I do not think that this is the reason why we need the casts...
bindgen generates bindings using std::os::raw::c_uint [5] which is
stable since 1.1.0 (and was previously defined as u32 [6]). I think we
can just drop the casts entirely? I can run cargo clippy --fix on latest
stable (1.70.0), then go back to 1.60 and everything is still building.
I am having trouble to execute the tests in that version due to some
linkage errors, but that should not be the fault of the casts.

Did I got this correct or am I misunderstanding your reasoning?

Note: One needs to fix a bug that cargo clippy --fix introduces since
it replaces nth(0) with next() in event_buffers.rs and introduces a
unconditional recursion.

[4] https://lore.kernel.org/r/20230616-msrv-v1-1-1f0ca25b8222@linaro.org/
[5] https://doc.rust-lang.org/stable/std/os/raw/type.c_uint.html
[6] https://doc.rust-lang.org/1.60.0/std/os/raw/type.c_uint.html

- Erik
Erik Schilling June 19, 2023, 7:49 a.m. UTC | #5
> Note: One needs to fix a bug that cargo clippy --fix introduces since
> it replaces nth(0) with next() in event_buffers.rs and introduces a
> unconditional recursion.

This seems to be a known false-positive:
https://github.com/rust-lang/rust-clippy/issues/9820

- Erik
Kent Gibson June 19, 2023, 7:57 a.m. UTC | #6
On Mon, Jun 19, 2023 at 09:36:48AM +0200, Erik Schilling wrote:
> Sorry, got distracted while sorting out the MSRV mess that I sent a
> separate patch for [4].
> 
> I do not think that this is the reason why we need the casts...
> bindgen generates bindings using std::os::raw::c_uint [5] which is
> stable since 1.1.0 (and was previously defined as u32 [6]). I think we
> can just drop the casts entirely? I can run cargo clippy --fix on latest
> stable (1.70.0), then go back to 1.60 and everything is still building.
> I am having trouble to execute the tests in that version due to some
> linkage errors, but that should not be the fault of the casts.
> 
> Did I got this correct or am I misunderstanding your reasoning?
> 

My reasoning was simply that building the bindings as you suggested
resulted in lint warnings, which is noisy and iritating when trying to
lint my own code.  And I assumed that changing the code or limiting the
rust version was not an option.

But I'm just the messenger.  Your question would be better directed at
Viresh - it is his code so he should be able to tell you why the casts
are there.
IIRC we needed the casts historically, though I don't recall the rust
version we were using at the time.
If we've moved beyond that then I have no problem with the casts being
removedi, in fact in my initial comment I lamented the fact they were
necessary.

> Note: One needs to fix a bug that cargo clippy --fix introduces since
> it replaces nth(0) with next() in event_buffers.rs and introduces a
> unconditional recursion.
> 

Who is using --fix??

I did put an allow in there for that one, with a comment about the
recursion, though I'm not sure the comment is sufficiently clear without
the warning in front of you - and you no longer get that with the allow
in place.

Cheers,
Kent.
Erik Schilling June 19, 2023, 8:13 a.m. UTC | #7
On Mon Jun 19, 2023 at 9:57 AM CEST, Kent Gibson wrote:
> On Mon, Jun 19, 2023 at 09:36:48AM +0200, Erik Schilling wrote:
> > Sorry, got distracted while sorting out the MSRV mess that I sent a
> > separate patch for [4].
> > 
> > I do not think that this is the reason why we need the casts...
> > bindgen generates bindings using std::os::raw::c_uint [5] which is
> > stable since 1.1.0 (and was previously defined as u32 [6]). I think we
> > can just drop the casts entirely? I can run cargo clippy --fix on latest
> > stable (1.70.0), then go back to 1.60 and everything is still building.
> > I am having trouble to execute the tests in that version due to some
> > linkage errors, but that should not be the fault of the casts.
> > 
> > Did I got this correct or am I misunderstanding your reasoning?
> > 
>
> My reasoning was simply that building the bindings as you suggested
> resulted in lint warnings, which is noisy and iritating when trying to
> lint my own code.

I fully agree that we should fix them! I was just confused about the
explanation.

> But I'm just the messenger.  Your question would be better directed at
> Viresh - it is his code so he should be able to tell you why the casts
> are there.
> IIRC we needed the casts historically, though I don't recall the rust
> version we were using at the time.
> If we've moved beyond that then I have no problem with the casts being
> removedi, in fact in my initial comment I lamented the fact they were
> necessary.

ACK. Not sure how the situation was when the patches came together, but
today I would suggest to just drop the casts. Do you want to respin your
series or shall I send the fixes?

> > Note: One needs to fix a bug that cargo clippy --fix introduces since
> > it replaces nth(0) with next() in event_buffers.rs and introduces a
> > unconditional recursion.
> > 
>
> Who is using --fix??

I do all the time (and spend the time I saved typing on reviewing)!
(Thought mostly I use the suggested actions suggested by the LSP in
the editor instead of the CLI tools)

> I did put an allow in there for that one, with a comment about the
> recursion, though I'm not sure the comment is sufficiently clear without
> the warning in front of you - and you no longer get that with the allow
> in place.

Ah. Did not notice that... My brain was only thinking about the casts.

- Erik
Kent Gibson June 19, 2023, 8:33 a.m. UTC | #8
On Mon, Jun 19, 2023 at 10:13:52AM +0200, Erik Schilling wrote:
> On Mon Jun 19, 2023 at 9:57 AM CEST, Kent Gibson wrote:
> > On Mon, Jun 19, 2023 at 09:36:48AM +0200, Erik Schilling wrote:
> > > Sorry, got distracted while sorting out the MSRV mess that I sent a
> > > separate patch for [4].
> > > 
> >
> > My reasoning was simply that building the bindings as you suggested
> > resulted in lint warnings, which is noisy and iritating when trying to
> > lint my own code.
> 
> I fully agree that we should fix them! I was just confused about the
> explanation.
> 
> > But I'm just the messenger.  Your question would be better directed at
> > Viresh - it is his code so he should be able to tell you why the casts
> > are there.
> > IIRC we needed the casts historically, though I don't recall the rust
> > version we were using at the time.
> > If we've moved beyond that then I have no problem with the casts being
> > removed, in fact in my initial comment I lamented the fact they were
> > necessary.
> 
> ACK. Not sure how the situation was when the patches came together, but
> today I would suggest to just drop the casts. Do you want to respin your
> series or shall I send the fixes?
> 

You go ahead.

Cheers,
Kent.
Viresh Kumar June 19, 2023, 8:50 a.m. UTC | #9
I missed these earlier, thanks for cc'ing me now Kent.

On 19-06-23, 10:13, Erik Schilling wrote:
> On Mon Jun 19, 2023 at 9:57 AM CEST, Kent Gibson wrote:
> > My reasoning was simply that building the bindings as you suggested
> > resulted in lint warnings, which is noisy and iritating when trying to
> > lint my own code.
> 
> I fully agree that we should fix them! I was just confused about the
> explanation.
> 
> > But I'm just the messenger.  Your question would be better directed at
> > Viresh - it is his code so he should be able to tell you why the casts
> > are there.
> > IIRC we needed the casts historically, though I don't recall the rust
> > version we were using at the time.
> > If we've moved beyond that then I have no problem with the casts being
> > removedi, in fact in my initial comment I lamented the fact they were
> > necessary.

I don't remember the versions used at that time, but here [1] are few
of the auto-generated FFI bindings that I used to keep in my series
then, in case these can help.

IIRC, some of these were required for 32 bit builds. Don't remember
the exact details but there were build / clippy errors / warnings
without them.

I am fine with updating code based on latest version of Rust and get
these removed.

Erik: Please build for 32 bit ARM too btw.
Erik Schilling June 19, 2023, 8:59 a.m. UTC | #10
On Mon Jun 19, 2023 at 10:50 AM CEST, Viresh Kumar wrote:
> I missed these earlier, thanks for cc'ing me now Kent.
>
> On 19-06-23, 10:13, Erik Schilling wrote:
> > On Mon Jun 19, 2023 at 9:57 AM CEST, Kent Gibson wrote:
> > > My reasoning was simply that building the bindings as you suggested
> > > resulted in lint warnings, which is noisy and iritating when trying to
> > > lint my own code.
> > 
> > I fully agree that we should fix them! I was just confused about the
> > explanation.
> > 
> > > But I'm just the messenger.  Your question would be better directed at
> > > Viresh - it is his code so he should be able to tell you why the casts
> > > are there.
> > > IIRC we needed the casts historically, though I don't recall the rust
> > > version we were using at the time.
> > > If we've moved beyond that then I have no problem with the casts being
> > > removedi, in fact in my initial comment I lamented the fact they were
> > > necessary.
>
> I don't remember the versions used at that time, but here [1] are few
> of the auto-generated FFI bindings that I used to keep in my series
> then, in case these can help.
>
> IIRC, some of these were required for 32 bit builds. Don't remember
> the exact details but there were build / clippy errors / warnings
> without them.
>
> I am fine with updating code based on latest version of Rust and get
> these removed.
>
> Erik: Please build for 32 bit ARM too btw.

Will do. Will try to come up with some test suite that may be
upstreamable too.

>
> -- 
> viresh
>
> [1] https://lore.kernel.org/all/401d9417d895b8b1b19ca577c84347d89f7e0fbd.1667815011.git.viresh.kumar@linaro.org/
diff mbox series

Patch

diff --git a/bindings/rust/gpiosim-sys/src/lib.rs b/bindings/rust/gpiosim-sys/src/lib.rs
index eed2a42..0360333 100644
--- a/bindings/rust/gpiosim-sys/src/lib.rs
+++ b/bindings/rust/gpiosim-sys/src/lib.rs
@@ -37,6 +37,7 @@  pub enum Value {
 
 impl Value {
     pub(crate) fn new(val: gpiosim_value) -> Result<Self> {
+        #[allow(clippy::unnecessary_cast)]
         Ok(match val {
             GPIOSIM_VALUE_INACTIVE => Value::InActive,
             GPIOSIM_VALUE_ACTIVE => Value::Active,
diff --git a/bindings/rust/gpiosim-sys/src/sim.rs b/bindings/rust/gpiosim-sys/src/sim.rs
index 896596f..cac5659 100644
--- a/bindings/rust/gpiosim-sys/src/sim.rs
+++ b/bindings/rust/gpiosim-sys/src/sim.rs
@@ -155,8 +155,9 @@  impl SimBank {
     }
 
     fn val(&self, offset: Offset) -> Result<Value> {
+        #[allow(clippy::unnecessary_cast)]
         // SAFETY: `gpiosim_bank` is guaranteed to be valid here.
-        let ret = unsafe { gpiosim_bank_get_value(self.bank, offset) };
+        let ret = unsafe { gpiosim_bank_get_value(self.bank, offset) as i32};
 
         if ret == -1 {
             Err(Error::OperationFailed(
@@ -164,7 +165,7 @@  impl SimBank {
                 errno::errno(),
             ))
         } else {
-            Value::new(ret as i32)
+            Value::new(ret)
         }
     }
 
@@ -185,6 +186,7 @@  impl SimBank {
     }
 
     fn set_num_lines(&self, num: usize) -> Result<()> {
+        #[allow(clippy::useless_conversion)]
         // SAFETY: `gpiosim_bank` is guaranteed to be valid here.
         let ret = unsafe { gpiosim_bank_set_num_lines(self.bank, num.try_into().unwrap()) };
         if ret == -1 {
diff --git a/bindings/rust/libgpiod/src/chip.rs b/bindings/rust/libgpiod/src/chip.rs
index f4de008..f160d23 100644
--- a/bindings/rust/libgpiod/src/chip.rs
+++ b/bindings/rust/libgpiod/src/chip.rs
@@ -278,8 +278,11 @@  impl Info {
 
     /// Get the number of GPIO lines exposed by the chip.
     pub fn num_lines(&self) -> usize {
+        #[allow(clippy::unnecessary_cast)]
         // SAFETY: `gpiod_chip` is guaranteed to be valid here.
-        unsafe { gpiod::gpiod_chip_info_get_num_lines(self.info) as usize }
+        unsafe {
+            gpiod::gpiod_chip_info_get_num_lines(self.info) as usize
+        }
     }
 }
 
diff --git a/bindings/rust/libgpiod/src/edge_event.rs b/bindings/rust/libgpiod/src/edge_event.rs
index d324ce6..64d20e2 100644
--- a/bindings/rust/libgpiod/src/edge_event.rs
+++ b/bindings/rust/libgpiod/src/edge_event.rs
@@ -40,6 +40,7 @@  impl Event {
 
     /// Get the event type.
     pub fn event_type(&self) -> Result<EdgeKind> {
+        #[allow(clippy::unnecessary_cast)]
         // SAFETY: `gpiod_edge_event` is guaranteed to be valid here.
         EdgeKind::new(unsafe { gpiod::gpiod_edge_event_get_event_type(self.0) } as u32)
     }
diff --git a/bindings/rust/libgpiod/src/event_buffer.rs b/bindings/rust/libgpiod/src/event_buffer.rs
index 1deaf2b..89611bc 100644
--- a/bindings/rust/libgpiod/src/event_buffer.rs
+++ b/bindings/rust/libgpiod/src/event_buffer.rs
@@ -54,6 +54,7 @@  impl<'a> Iterator for Events<'a> {
     }
 
     fn next(&mut self) -> Option<Self::Item> {
+        #[allow(clippy::iter_nth_zero)] // as using next() is self referential
         self.nth(0)
     }
 }
@@ -81,6 +82,7 @@  impl Buffer {
             ));
         }
 
+        #[allow(clippy::unnecessary_cast)]
         // SAFETY: `gpiod_edge_event_buffer` is guaranteed to be valid here.
         let capacity = unsafe { gpiod::gpiod_edge_event_buffer_get_capacity(buffer) as usize };
 
@@ -103,6 +105,7 @@  impl Buffer {
             self.events[i] = ptr::null_mut();
         }
 
+        #[allow(clippy::useless_conversion)]
         // SAFETY: `gpiod_line_request` is guaranteed to be valid here.
         let ret = unsafe {
             gpiod::gpiod_line_request_read_edge_events(
diff --git a/bindings/rust/libgpiod/src/info_event.rs b/bindings/rust/libgpiod/src/info_event.rs
index b0ceb3b..7651f1c 100644
--- a/bindings/rust/libgpiod/src/info_event.rs
+++ b/bindings/rust/libgpiod/src/info_event.rs
@@ -33,6 +33,7 @@  impl Event {
 
     /// Get the event type of the status change event.
     pub fn event_type(&self) -> Result<InfoChangeKind> {
+        #[allow(clippy::unnecessary_cast)]
         // SAFETY: `gpiod_info_event` is guaranteed to be valid here.
         InfoChangeKind::new(unsafe { gpiod::gpiod_info_event_get_event_type(self.event) } as u32)
     }
diff --git a/bindings/rust/libgpiod/src/lib.rs b/bindings/rust/libgpiod/src/lib.rs
index 26354e5..314e157 100644
--- a/bindings/rust/libgpiod/src/lib.rs
+++ b/bindings/rust/libgpiod/src/lib.rs
@@ -184,6 +184,7 @@  pub mod line {
 
     impl Value {
         pub fn new(val: gpiod::gpiod_line_value) -> Result<Self> {
+            #[allow(clippy::unnecessary_cast)]
             Ok(match val {
                 GPIOD_LINE_VALUE_INACTIVE => Value::InActive,
                 GPIOD_LINE_VALUE_ACTIVE => Value::Active,
diff --git a/bindings/rust/libgpiod/src/line_config.rs b/bindings/rust/libgpiod/src/line_config.rs
index e973cde..405e675 100644
--- a/bindings/rust/libgpiod/src/line_config.rs
+++ b/bindings/rust/libgpiod/src/line_config.rs
@@ -106,20 +106,23 @@  impl Config {
     /// Get a mapping of offsets to line settings stored by this object.
     pub fn line_settings(&self) -> Result<SettingsMap> {
         let mut map = SettingsMap::new();
+        #[allow(clippy::unnecessary_cast)]
         // SAFETY: gpiod_line_config is guaranteed to be valid here
-        let num_lines = unsafe { gpiod::gpiod_line_config_get_num_configured_offsets(self.config) };
-        let mut offsets = vec![0; num_lines as usize];
+        let num_lines =
+            unsafe { gpiod::gpiod_line_config_get_num_configured_offsets(self.config) as usize };
+        let mut offsets = vec![0; num_lines];
 
+        #[allow(clippy::unnecessary_cast)]
         // SAFETY: gpiod_line_config is guaranteed to be valid here.
         let num_stored = unsafe {
             gpiod::gpiod_line_config_get_configured_offsets(
                 self.config,
                 offsets.as_mut_ptr(),
                 num_lines,
-            )
+            ) as usize
         };
 
-        for offset in &offsets[0..num_stored as usize] {
+        for offset in &offsets[0..num_stored] {
             // SAFETY: `gpiod_line_config` is guaranteed to be valid here.
             let settings =
                 unsafe { gpiod::gpiod_line_config_get_line_settings(self.config, *offset) };
diff --git a/bindings/rust/libgpiod/src/line_info.rs b/bindings/rust/libgpiod/src/line_info.rs
index 702f1b4..bff9ece 100644
--- a/bindings/rust/libgpiod/src/line_info.rs
+++ b/bindings/rust/libgpiod/src/line_info.rs
@@ -142,6 +142,7 @@  impl Info {
 
     /// Get the debounce period of the line.
     pub fn debounce_period(&self) -> Duration {
+        #[allow(clippy::unnecessary_cast)]
         // SAFETY: `gpiod_line_info` is guaranteed to be valid here.
         Duration::from_micros(unsafe {
             gpiod::gpiod_line_info_get_debounce_period_us(self.info) as u64
diff --git a/bindings/rust/libgpiod/src/line_request.rs b/bindings/rust/libgpiod/src/line_request.rs
index b175eea..51cc7cd 100644
--- a/bindings/rust/libgpiod/src/line_request.rs
+++ b/bindings/rust/libgpiod/src/line_request.rs
@@ -27,23 +27,27 @@  impl Request {
 
     /// Get the number of lines in the request.
     pub fn num_lines(&self) -> usize {
+        #[allow(clippy::unnecessary_cast)]
         // SAFETY: `gpiod_line_request` is guaranteed to be valid here.
-        unsafe { gpiod::gpiod_line_request_get_num_requested_lines(self.request) as usize }
+        unsafe {
+            gpiod::gpiod_line_request_get_num_requested_lines(self.request) as usize
+        }
     }
 
     /// Get the offsets of lines in the request.
     pub fn offsets(&self) -> Vec<Offset> {
-        let mut offsets = vec![0; self.num_lines() as usize];
+        let mut offsets = vec![0; self.num_lines()];
 
+        #[allow(clippy::unnecessary_cast)]
         // SAFETY: `gpiod_line_request` is guaranteed to be valid here.
         let num_offsets = unsafe {
             gpiod::gpiod_line_request_get_requested_offsets(
                 self.request,
                 offsets.as_mut_ptr(),
                 self.num_lines(),
-            )
+            ) as usize
         };
-        offsets.shrink_to(num_offsets as usize);
+        offsets.shrink_to(num_offsets);
         offsets
     }
 
@@ -145,7 +149,7 @@  impl Request {
 
     /// Set values of all lines associated with the request.
     pub fn set_values(&mut self, values: &[Value]) -> Result<&mut Self> {
-        if values.len() != self.num_lines() as usize {
+        if values.len() != self.num_lines() {
             return Err(Error::InvalidArguments);
         }
 
diff --git a/bindings/rust/libgpiod/src/line_settings.rs b/bindings/rust/libgpiod/src/line_settings.rs
index 918d6c2..3b1d2b6 100644
--- a/bindings/rust/libgpiod/src/line_settings.rs
+++ b/bindings/rust/libgpiod/src/line_settings.rs
@@ -218,6 +218,7 @@  impl Settings {
 
     /// Get the debounce period.
     pub fn debounce_period(&self) -> Result<Duration> {
+        #[allow(clippy::unnecessary_cast)]
         // SAFETY: `gpiod_line_settings` is guaranteed to be valid here.
         Ok(Duration::from_micros(unsafe {
             gpiod::gpiod_line_settings_get_debounce_period_us(self.settings) as u64
@@ -243,6 +244,7 @@  impl Settings {
 
     /// Get the event clock setting.
     pub fn event_clock(&self) -> Result<EventClock> {
+        #[allow(clippy::unnecessary_cast)]
         // SAFETY: `gpiod_line_settings` is guaranteed to be valid here.
         EventClock::new(unsafe { gpiod::gpiod_line_settings_get_event_clock(self.settings) } as u32)
     }
diff --git a/bindings/rust/libgpiod/src/request_config.rs b/bindings/rust/libgpiod/src/request_config.rs
index 0c6c5c1..48edfaf 100644
--- a/bindings/rust/libgpiod/src/request_config.rs
+++ b/bindings/rust/libgpiod/src/request_config.rs
@@ -82,8 +82,11 @@  impl Config {
 
     /// Get the edge event buffer size setting for the request config.
     pub fn event_buffer_size(&self) -> usize {
+        #[allow(clippy::unnecessary_cast)]
         // SAFETY: `gpiod_request_config` is guaranteed to be valid here.
-        unsafe { gpiod::gpiod_request_config_get_event_buffer_size(self.config) as usize }
+        unsafe {
+            gpiod::gpiod_request_config_get_event_buffer_size(self.config) as usize
+        }
     }
 }
 
diff --git a/bindings/rust/libgpiod/tests/chip.rs b/bindings/rust/libgpiod/tests/chip.rs
index f264708..60b4ecc 100644
--- a/bindings/rust/libgpiod/tests/chip.rs
+++ b/bindings/rust/libgpiod/tests/chip.rs
@@ -59,7 +59,7 @@  mod chip {
             assert_eq!(info.label().unwrap(), LABEL);
             assert_eq!(info.name().unwrap(), sim.chip_name());
             assert_eq!(chip.path().unwrap(), sim.dev_path().to_str().unwrap());
-            assert_eq!(info.num_lines(), NGPIO as usize);
+            assert_eq!(info.num_lines(), NGPIO);
         }
 
         #[test]