Skip to contentSkip to conversation

Fix all clippy warnings in drm-rust-next

PATCH Applied John Hubbard 5 participants 16 messages Apr 4 – Apr 6

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover Fix all clippy warnings in drm-rust-next

This series fixes all remaining clippy warnings when building drm-rust-next with CLIPPY=1, using rustc 1.85.0. There are a few clippy lints involved, spread across nova-core and kernel crate code:

* clippy::precedence in nova-core vbios PCI ROM header parsing (2 warnings)
* clippy::precedence in nova-core read_sysmem_flush_page_ga100() (1 warning)
* clippy::incompatible_msrv for slice_ptr_len in rust/kernel/ptr (4 warnings)

v1 is here:
https://lore.kernel.org/[email protected]

Changes in v2:
* Split the vbios from_le_bytes() cleanup into its own patch, per Alex.

* New patch: silence clippy::incompatible_msrv false positives in rust/kernel/ptr.

* Expanded the scope of the fixes: now it makes the entire drm-rust-next kernel clippy-clean, at least for the toolchain that I tested on.

John Hubbard (3):
gpu: nova-core: vbios: use from_le_bytes() for PCI ROM header parsing
gpu: nova-core: fb: fix clippy::precedence warning in

    read_sysmem_flush_page_ga100()

rust: ptr: allow clippy::incompatible_msrv for slice_ptr_len

 drivers/gpu/nova-core/fb/hal/ga100.rs | 7 ++++---
 drivers/gpu/nova-core/vbios.rs        | 7 +------
 rust/kernel/ptr.rs                    | 1 +
 rust/kernel/ptr/projection.rs         | 3 +++
 4 files changed, 9 insertions(+), 9 deletions(-)

base-commit: 7c50d748b4a635bc39802ea3f6b120e66b1b9067

Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 1/3 gpu: nova-core: vbios: use from_le_bytes() for PCI ROM header parsing

Clippy fires two clippy::precedence warnings on the manual byte-shifting expression:
warning: operator precedence can trip the unwary

     --> drivers/gpu/nova-core/vbios.rs:511:17
      |
  511 | /                 u32::from(data[29]) << 24
  512 | |                     | u32::from(data[28]) << 16
  513 | |                     | u32::from(data[27]) << 8
      | |______________________________________________^

Clear the warnings by replacing manual byte-shifting with u32::from_le_bytes(). Using from_le_bytes() is also a tiny code improvement, because it uses less code and is clearer about the intent.

---
 drivers/gpu/nova-core/vbios.rs | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/gpu/nova-core/vbios.rs +1 -6
index 3e3fa5b72524..ebda28e596c5 100644
@@ -507,12 +507,7 @@ fn new(dev: &device::Device, data: &[u8]) -> Result<Self> {
 
         if data.len() >= 30 {
             // Read size_of_block at offset 0x1A.
-            size_of_block = Some(
-                u32::from(data[29]) << 24
-                    | u32::from(data[28]) << 16
-                    | u32::from(data[27]) << 8
-                    | u32::from(data[26]),
-            );
+            size_of_block = Some(u32::from_le_bytes([data[26], data[27], data[28], data[29]]));
         }
 
         // For NBSI images, try to read the nbsiDataOffset at offset 0x16.
Signature · 1 line
2.53.0
#2 lore raw

PATCH v2 2/3 gpu: nova-core: fb: fix clippy::precedence warning in read_sysmem_flush_page_ga100()

In reply to John Hubbard

Clippy warns when shifts and bitwise-OR are mixed without parentheses, because the relative precedence of << and | is easy to misread.

Add explicit parentheses to silence the warning.

---
 drivers/gpu/nova-core/fb/hal/ga100.rs | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/gpu/nova-core/fb/hal/ga100.rs +4 -3
index 1c03783cddef..b5d0f04198f0 100644
@@ -17,9 +17,10 @@
 struct Ga100;
 
 pub(super) fn read_sysmem_flush_page_ga100(bar: &Bar0) -> u64 {
-    u64::from(bar.read(regs::NV_PFB_NISO_FLUSH_SYSMEM_ADDR).adr_39_08()) << FLUSH_SYSMEM_ADDR_SHIFT
-        | u64::from(bar.read(regs::NV_PFB_NISO_FLUSH_SYSMEM_ADDR_HI).adr_63_40())
-            << FLUSH_SYSMEM_ADDR_SHIFT_HI
+    (u64::from(bar.read(regs::NV_PFB_NISO_FLUSH_SYSMEM_ADDR).adr_39_08())
+        << FLUSH_SYSMEM_ADDR_SHIFT)
+        | (u64::from(bar.read(regs::NV_PFB_NISO_FLUSH_SYSMEM_ADDR_HI).adr_63_40())
+            << FLUSH_SYSMEM_ADDR_SHIFT_HI)
 }
 
 pub(super) fn write_sysmem_flush_page_ga100(bar: &Bar0, addr: u64) {
Signature · 1 line
2.53.0
#3 lore raw

Re: [PATCH v2 2/3] gpu: nova-core: fb: fix clippy::precedence warning in read_sysmem_flush_page_ga100()

Quoted 4 lines · John Hubbard

Clippy warns when shifts and bitwise-OR are mixed without parentheses, because the relative precedence of << and | is easy to misread.

Add explicit parentheses to silence the warning.

Please disregard (drop) this patch 2/3, because Miguel is removing the associated lint, so we won't need it.

Quoted 14 lines
---
 drivers/gpu/nova-core/fb/hal/ga100.rs | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/gpu/nova-core/fb/hal/ga100.rs +0 -0
index 1c03783cddef..b5d0f04198f0 100644
@@ -17,9 +17,10 @@
 struct Ga100;
 
 pub(super) fn read_sysmem_flush_page_ga100(bar: &Bar0) -> u64 {

drivers/gpu/nova-core/fb/hal/ga100.rs line 26

Quoted 10 lines
drivers/gpu/nova-core/fb/hal/ga100.rs +4 -3
-    u64::from(bar.read(regs::NV_PFB_NISO_FLUSH_SYSMEM_ADDR).adr_39_08()) << FLUSH_SYSMEM_ADDR_SHIFT
-        | u64::from(bar.read(regs::NV_PFB_NISO_FLUSH_SYSMEM_ADDR_HI).adr_63_40())
-            << FLUSH_SYSMEM_ADDR_SHIFT_HI
+    (u64::from(bar.read(regs::NV_PFB_NISO_FLUSH_SYSMEM_ADDR).adr_39_08())
+        << FLUSH_SYSMEM_ADDR_SHIFT)
+        | (u64::from(bar.read(regs::NV_PFB_NISO_FLUSH_SYSMEM_ADDR_HI).adr_63_40())
+            << FLUSH_SYSMEM_ADDR_SHIFT_HI)
 }
 
 pub(super) fn write_sysmem_flush_page_ga100(bar: &Bar0, addr: u64) {

thanks,

Signature · 1 line
John Hubbard
#4 lore raw

Re: [PATCH v2 2/3] gpu: nova-core: fb: fix clippy::precedence warning in read_sysmem_flush_page_ga100()

Quoted 8 lines · John Hubbard
Quoted 4 lines · John Hubbard

Clippy warns when shifts and bitwise-OR are mixed without parentheses, because the relative precedence of << and | is easy to misread.

Add explicit parentheses to silence the warning.

Please disregard (drop) this patch 2/3, because Miguel is removing the associated lint, so we won't need it.

I'm tempted to consider it anyway, because it is a readability improvement. Any objection?

#5 lore raw

Re: [PATCH v2 2/3] gpu: nova-core: fb: fix clippy::precedence warning in read_sysmem_flush_page_ga100()

Quoted 3 lines · Alexandre Courbot

I'm tempted to consider it anyway, because it is a readability improvement. Any objection?

None at all -- though I think there are conflicting opinions on the matter. I think Gary preferred not to have the parentheses, supported by the fact that Clippy had to roll it back (i.e. linting at that by default) meant the parentheses may not be popular.

Back then I wrote:

Now, seeing quite a bunch of parenthesis for this in C files, from kernel devs and crypto stuff and all over drivers and so on and so forth, means others definitely do not find it obvious or prefer to be explicit for extra clarity.

I have checked again a few git grep patterns, and it seems to me there are substantially more files using parentheses than not in C files, at least with some naive regexes. Can someone double-check?

If we go for wanting them, then I can just enable clippy::precedence_bits to be consistent instead.

It is easier to disable the lint later than trying to enable it later on, though (because we would need to add all the missing parentheses everywhere).

Cheers,
Miguel

maintainer
#6 lore raw

Re: [PATCH v2 2/3] gpu: nova-core: fb: fix clippy::precedence warning in read_sysmem_flush_page_ga100()

In reply to Alexandre Courbot
Quoted 5 lines · Alexandre Courbot
Quoted 4 lines · John Hubbard
Quoted 3 lines · John Hubbard

Clippy warns when shifts and bitwise-OR are mixed without parentheses, because the relative precedence of << and | is easy to misread.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Add explicit parentheses to silence the warning.

Please disregard (drop) this patch 2/3, because Miguel is removing the associated lint, so we won't need it.

I'm tempted to consider it anyway, because it is a readability improvement. Any objection?

I'd rather not. If we have the lint disabled - and I think having it enabled is a bit too much - then I don't want to set a precedence for unnecessary churn like adding or removing unnecessary braces.

- Danilo

#7 lore raw

Re: [PATCH v2 2/3] gpu: nova-core: fb: fix clippy::precedence warning in read_sysmem_flush_page_ga100()

Quoted 11 lines · Danilo Krummrich
Quoted 10 lines · Alexandre Courbot
Quoted 8 lines · John Hubbard
Quoted 4 lines · John Hubbard

Clippy warns when shifts and bitwise-OR are mixed without parentheses, because the relative precedence of << and | is easy to misread.

Add explicit parentheses to silence the warning.

Please disregard (drop) this patch 2/3, because Miguel is removing the associated lint, so we won't need it.

Quoted 6 lines
Quoted 2 lines

I'm tempted to consider it anyway, because it is a readability improvement. Any objection?

I'd rather not. If we have the lint disabled - and I think having it enabled is a bit too much - then I don't want to set a precedence for unnecessary churn like adding or removing unnecessary braces.

Alright - dropping it for now then, thank you (and Miguel) for the input.

#8 lore raw

PATCH v2 3/3 rust: ptr: allow clippy::incompatible_msrv for slice_ptr_len

In reply to John Hubbard

Clippy reports four warnings when building with CLIPPY=1:

warning: current MSRV (Minimum Supported Rust Version) is 1.78.0 but this item is stable since 1.79.0

    --> rust/kernel/ptr/projection.rs:79:26
     |

79 | if self >= slice.len() {
| ^^^^^

warning: current MSRV (Minimum Supported Rust Version) is 1.78.0 but this item is stable since 1.79.0

    --> rust/kernel/ptr/projection.rs:95:29
     |

95 | if self.end > slice.len() {
| ^^^^^

warning: current MSRV (Minimum Supported Rust Version) is 1.78.0 but this item is stable since 1.79.0

     --> rust/kernel/ptr/projection.rs:121:28
      |

121 | (self.start..slice.len()).get(slice)
| ^^^^^

warning: current MSRV (Minimum Supported Rust Version) is 1.78.0 but this item is stable since 1.79.0

     --> rust/kernel/ptr.rs:253:11
      |
  253 |         p.len() * size_of::<T>()
      |           ^^^^^

These are false positives. The <*mut [T]>::len() and <*const [T]>::len() methods are available because the kernel enables #![feature(slice_ptr_len)] in lib.rs. Clippy does not account for feature gates when checking MSRV compatibility.

Silence these false positive via #[allow(clippy::incompatible_msrv)].

---
 rust/kernel/ptr.rs            | 1 +
 rust/kernel/ptr/projection.rs | 3 +++
 2 files changed, 4 insertions(+)
rust/kernel/ptr.rs +1 -0
index bdc2d79ff669..7482653ef160 100644
@@ -248,6 +248,7 @@ fn size(_: *const Self) -> usize {
 }
 
 impl<T> KnownSize for [T] {
+    #[allow(clippy::incompatible_msrv)]
     #[inline(always)]
     fn size(p: *const Self) -> usize {
         p.len() * size_of::<T>()
rust/kernel/ptr/projection.rs +3 -0
index 140ea8e21617..7d1c878b3116 100644
@@ -74,6 +74,7 @@ fn index(self, slice: *mut T) -> *mut Self::Output {
 unsafe impl<T> ProjectIndex<[T]> for usize {
     type Output = T;
 
+    #[allow(clippy::incompatible_msrv)]
     #[inline(always)]
     fn get(self, slice: *mut [T]) -> Option<*mut T> {
         if self >= slice.len() {
@@ -89,6 +90,7 @@ fn get(self, slice: *mut [T]) -> Option<*mut T> {
 unsafe impl<T> ProjectIndex<[T]> for core::ops::Range<usize> {
     type Output = [T];
 
+    #[allow(clippy::incompatible_msrv)]
     #[inline(always)]
     fn get(self, slice: *mut [T]) -> Option<*mut [T]> {
         let new_len = self.end.checked_sub(self.start)?;
@@ -116,6 +118,7 @@ fn get(self, slice: *mut [T]) -> Option<*mut [T]> {
 unsafe impl<T> ProjectIndex<[T]> for core::ops::RangeFrom<usize> {
     type Output = [T];
 
+    #[allow(clippy::incompatible_msrv)]
     #[inline(always)]
     fn get(self, slice: *mut [T]) -> Option<*mut [T]> {
         (self.start..slice.len()).get(slice)
Signature · 1 line
2.53.0
#9 lore raw

Re: [PATCH v2 3/3] rust: ptr: allow clippy::incompatible_msrv for slice_ptr_len

Quoted 8 lines · John Hubbard

Clippy reports four warnings when building with CLIPPY=1:

warning: current MSRV (Minimum Supported Rust Version) is 1.78.0 but this item is stable since 1.79.0

    --> rust/kernel/ptr/projection.rs:79:26
     |

79 | if self >= slice.len() {
| ^^^^^

This will also go away with the bump:

https://lore.kernel.org/rust-for-linux/[email protected]/

Cheers,
Miguel

#10 lore raw

Re: [PATCH v2 3/3] rust: ptr: allow clippy::incompatible_msrv for slice_ptr_len

Quoted 10 lines · Miguel Ojeda
Quoted 8 lines · John Hubbard

Clippy reports four warnings when building with CLIPPY=1:

warning: current MSRV (Minimum Supported Rust Version) is 1.78.0 but this item is stable since 1.79.0

    --> rust/kernel/ptr/projection.rs:79:26
     |

79 | if self >= slice.len() {
| ^^^^^

Quoted 4 lines

This will also go away with the bump:

https://lore.kernel.org/rust-for-linux/[email protected]/

And that bump is destined for this merge window, right?

I'm also wondering how much, if any of this series here is needed soon, to remain warnings-free.

thanks,

Signature · 1 line
John Hubbard
#11 lore raw

Re: [PATCH v2 3/3] rust: ptr: allow clippy::incompatible_msrv for slice_ptr_len

Quoted 2 lines · John Hubbard

And that bump is destined for this merge window, right?

Yes, but I already replied here to this:

https://lore.kernel.org/rust-for-linux/CANiq72nbHtFpqgC-CGyLULSH9C90U2bw7bmovscqffd4HP0Oxw@mail.gmail.com/

So I am wondering if you meant something else in one of them (?).

Confused...

Quoted 2 lines

I'm also wondering how much, if any of this series here is needed soon, to remain warnings-free.

I don't think anything is strictly needed, but the cleanup you did in the first patch looks good and Danilo already took it, so that is great.

Cheers,
Miguel

#12 lore raw

Re: [PATCH v2 3/3] rust: ptr: allow clippy::incompatible_msrv for slice_ptr_len

Quoted 11 lines · Miguel Ojeda
Quoted 2 lines · John Hubbard

And that bump is destined for this merge window, right?

Yes, but I already replied here to this:

https://lore.kernel.org/rust-for-linux/CANiq72nbHtFpqgC-CGyLULSH9C90U2bw7bmovscqffd4HP0Oxw@mail.gmail.com/

So I am wondering if you meant something else in one of them (?).

Confused...

But considerably less confused than I was, when I wrote that second question. haha :)

Anyway, all is clear now. I was a bit rattled when the new drm-rust-next presented me 7 new CLIPPY warnings, but it appears that each one has a resolution, so good.

thanks,

Signature · 1 line
John Hubbard
reviewer
#13 lore raw

Re: [PATCH v2 3/3] rust: ptr: allow clippy::incompatible_msrv for slice_ptr_len

In reply to Miguel Ojeda
Quoted 10 lines · Miguel Ojeda
Quoted 8 lines · John Hubbard

Clippy reports four warnings when building with CLIPPY=1:

warning: current MSRV (Minimum Supported Rust Version) is 1.78.0 but this item is stable since 1.79.0

    --> rust/kernel/ptr/projection.rs:79:26
     |

79 | if self >= slice.len() {
| ^^^^^

Quoted 6 lines

This will also go away with the bump:

https://lore.kernel.org/rust-for-linux/[email protected]/

Cheers,
Miguel

I feel like this lint is hurting us rather than helping. Perhaps it's worth disabling it globally.

Best,
Gary

#14 lore raw

Re: [PATCH v2 3/3] rust: ptr: allow clippy::incompatible_msrv for slice_ptr_len

Quoted 3 lines · Gary Guo

I feel like this lint is hurting us rather than helping. Perhaps it's worth disabling it globally.

Yeah, we can do that. The msrv is still useful to have for other lints, so I kept it.

Cheers,
Miguel