All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kent Gibson <warthog618@gmail.com>
To: linux-gpio@vger.kernel.org, brgl@bgdev.pl
Cc: Kent Gibson <warthog618@gmail.com>
Subject: [libgpiod][PATCH] bindings: rust: fix clippy lint warnings
Date: Mon, 12 Jun 2023 23:40:55 +0800	[thread overview]
Message-ID: <20230612154055.56556-1-warthog618@gmail.com> (raw)

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(-)

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]
-- 
2.40.1


             reply	other threads:[~2023-06-12 15:41 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-12 15:40 Kent Gibson [this message]
2023-06-14  8:14 ` [libgpiod][PATCH] bindings: rust: fix clippy lint warnings Erik Schilling
2023-06-14  8:29   ` Kent Gibson
2023-06-14  8:40     ` Erik Schilling
2023-06-14  9:06       ` Kent Gibson
2023-06-14  9:16         ` Erik Schilling
2023-06-19  7:36     ` Erik Schilling
2023-06-19  7:49       ` Erik Schilling
2023-06-19  7:57       ` Kent Gibson
2023-06-19  8:13         ` Erik Schilling
2023-06-19  8:33           ` Kent Gibson
2023-06-19  8:50           ` Viresh Kumar
2023-06-19  8:59             ` Erik Schilling

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230612154055.56556-1-warthog618@gmail.com \
    --to=warthog618@gmail.com \
    --cc=brgl@bgdev.pl \
    --cc=linux-gpio@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.