History

rust: enable `clippy::as_underscore` lint

In Rust 1.63.0, Clippy introduced the `as_underscore` lint [1]: > The conversion might include lossy conversion or a dangerous cast that > might go undetected due to the type being inferred. > > The lint is allowed by default as using `_` is less wordy than always > specifying the type. Always specifying the type is especially helpful in function call contexts where the inferred type may change at a distance. Specifying the type also allows Clippy to spot more cases of `useless_conversion`. The primary downside is the need to specify the type in trivial getters. There are 4 such functions: 3 have become slightly less ergonomic, 1 was revealed to be a `useless_conversion`. While this doesn't eliminate unchecked `as` conversions, it makes such conversions easier to scrutinize. It also has the slight benefit of removing a degree of freedom on which to bikeshed. Thus apply the changes and enable the lint -- no functional change intended.

Tamir Duberstein committed 1 year ago

Commit 5e305501 parent 23773bd16 files · +70 −60Browse files

Unified · Split

16 files
  1. Makefile +1 −0
  2. drivers/gpu/nova-core/driver.rs +1 −1
  3. rust/kernel/block/mq/operations.rs +1 −1
  4. rust/kernel/block/mq/request.rs +6 −1
  5. rust/kernel/device_id.rs +1 −1
  6. rust/kernel/devres.rs +7 −6
  7. rust/kernel/dma.rs +1 −1
  8. rust/kernel/drm/device.rs +1 −1
  9. rust/kernel/error.rs +1 −1
  10. rust/kernel/io.rs +9 −9
  11. rust/kernel/miscdevice.rs +1 −1
  12. rust/kernel/mm/virt.rs +26 −26
  13. rust/kernel/of.rs +3 −3
  14. rust/kernel/pci.rs +6 −3
  15. rust/kernel/str.rs +4 −4
  16. rust/kernel/workqueue.rs +1 −1
Makefile+1−0
@@ -480,6 +480,7 @@ export rust_common_flags := --edition=2021 \ · 479 unmodified lines
480480 -Wunreachable_pub \
481481 -Wclippy::all \
482482 -Wclippy::as_ptr_cast_mut \
483+ -Wclippy::as_underscore \
483484 -Wclippy::ignored_unit_patterns \
484485 -Wclippy::mut_mut \
485486 -Wclippy::needless_bitwise_bool \
drivers/gpu/nova-core/driver.rs+1−1
@@ -19,7 +19,7 @@ kernel::pci_device_table!( · 18 unmodified lines
1919 MODULE_PCI_TABLE,
2020 <NovaCore as pci::Driver>::IdInfo,
2121 [(
22− pci::DeviceId::from_id(bindings::PCI_VENDOR_ID_NVIDIA, bindings::PCI_ANY_ID as _),
22+ pci::DeviceId::from_id(bindings::PCI_VENDOR_ID_NVIDIA, bindings::PCI_ANY_ID as u32),
2323 ()
2424 )]
2525 );
rust/kernel/block/mq/operations.rs+1−1
@@ -101,7 +101,7 @@ impl<T: Operations> OperationsVTable<T> { · 100 unmodified lines
101101 if let Err(e) = ret {
102102 e.to_blk_status()
103103 } else {
104− bindings::BLK_STS_OK as _
104+ bindings::BLK_STS_OK as bindings::blk_status_t
105105 }
106106 }
107107
rust/kernel/block/mq/request.rs+6−1
@@ -125,7 +125,12 @@ impl<T: Operations> Request<T> { · 124 unmodified lines
125125 // success of the call to `try_set_end` guarantees that there are no
126126 // `ARef`s pointing to this request. Therefore it is safe to hand it
127127 // back to the block layer.
128− unsafe { bindings::blk_mq_end_request(request_ptr, bindings::BLK_STS_OK as _) };
128+ unsafe {
129+ bindings::blk_mq_end_request(
130+ request_ptr,
131+ bindings::BLK_STS_OK as bindings::blk_status_t,
132+ )
133+ };
129134
130135 Ok(())
131136 }
rust/kernel/device_id.rs+1−1
@@ -82,7 +82,7 @@ impl<T: RawDeviceId, U, const N: usize> IdArray<T, U, N> { · 81 unmodified lines
8282 unsafe {
8383 raw_ids[i]
8484 .as_mut_ptr()
85− .byte_offset(T::DRIVER_DATA_OFFSET as _)
85+ .byte_add(T::DRIVER_DATA_OFFSET)
8686 .cast::<usize>()
8787 .write(i);
8888 }
rust/kernel/devres.rs+7−6
@@ -61,19 +61,19 @@ struct DevresInner<T> { · 60 unmodified lines
6161 /// unsafe fn new(paddr: usize) -> Result<Self>{
6262 /// // SAFETY: By the safety requirements of this function [`paddr`, `paddr` + `SIZE`) is
6363 /// // valid for `ioremap`.
64−/// let addr = unsafe { bindings::ioremap(paddr as _, SIZE as _) };
64+/// let addr = unsafe { bindings::ioremap(paddr as bindings::phys_addr_t, SIZE) };
6565 /// if addr.is_null() {
6666 /// return Err(ENOMEM);
6767 /// }
6868 ///
69−/// Ok(IoMem(IoRaw::new(addr as _, SIZE)?))
69+/// Ok(IoMem(IoRaw::new(addr as usize, SIZE)?))
7070 /// }
7171 /// }
7272 ///
7373 /// impl<const SIZE: usize> Drop for IoMem<SIZE> {
7474 /// fn drop(&mut self) {
7575 /// // SAFETY: `self.0.addr()` is guaranteed to be properly mapped by `Self::new`.
76−/// unsafe { bindings::iounmap(self.0.addr() as _); };
76+/// unsafe { bindings::iounmap(self.0.addr() as *mut c_void); };
7777 /// }
7878 /// }
7979 ///
@@ -115,8 +115,9 @@ impl<T> DevresInner<T> { · 35 unmodified lines
115115
116116 // SAFETY: `devm_add_action` guarantees to call `Self::devres_callback` once `dev` is
117117 // detached.
118− let ret =
119− unsafe { bindings::devm_add_action(dev.as_raw(), Some(inner.callback), data as _) };
118+ let ret = unsafe {
119+ bindings::devm_add_action(dev.as_raw(), Some(inner.callback), data.cast_mut().cast())
120+ };
120121
121122 if ret != 0 {
122123 // SAFETY: We just created another reference to `inner` in order to pass it to
@@ -130,7 +131,7 @@ impl<T> DevresInner<T> { · 7 unmodified lines
130131 }
131132
132133 fn as_ptr(&self) -> *const Self {
133− self as _
134+ self
134135 }
135136
136137 fn remove_action(this: &Arc<Self>) -> bool {
rust/kernel/dma.rs+1−1
@@ -38,7 +38,7 @@ pub struct Attrs(u32); · 37 unmodified lines
3838 impl Attrs {
3939 /// Get the raw representation of this attribute.
4040 pub(crate) fn as_raw(self) -> crate::ffi::c_ulong {
41− self.0 as _
41+ self.0 as crate::ffi::c_ulong
4242 }
4343
4444 /// Check whether `flags` is contained in `self`.
rust/kernel/drm/device.rs+1−1
@@ -89,7 +89,7 @@ impl<T: drm::Driver> Device<T> { · 88 unmodified lines
8989 driver_features: drm::driver::FEAT_GEM,
9090 ioctls: T::IOCTLS.as_ptr(),
9191 num_ioctls: T::IOCTLS.len() as i32,
92− fops: &Self::GEM_FOPS as _,
92+ fops: &Self::GEM_FOPS,
9393 };
9494
9595 const GEM_FOPS: bindings::file_operations = drm::gem::create_fops();
rust/kernel/error.rs+1−1
@@ -153,7 +153,7 @@ impl Error { · 152 unmodified lines
153153 /// Returns the error encoded as a pointer.
154154 pub fn to_ptr<T>(self) -> *mut T {
155155 // SAFETY: `self.0` is a valid error due to its invariant.
156− unsafe { bindings::ERR_PTR(self.0.get() as _).cast() }
156+ unsafe { bindings::ERR_PTR(self.0.get() as crate::ffi::c_long).cast() }
157157 }
158158
159159 /// Returns a string representing the error, if one exists.
rust/kernel/io.rs+9−9
@@ -5,7 +5,7 @@ · 4 unmodified lines
55 //! C header: [`include/asm-generic/io.h`](srctree/include/asm-generic/io.h)
66
77 use crate::error::{code::EINVAL, Result};
8−use crate::{bindings, build_assert};
8+use crate::{bindings, build_assert, ffi::c_void};
99
1010 /// Raw representation of an MMIO region.
1111 ///
@@ -56,7 +56,7 @@ impl<const SIZE: usize> IoRaw<SIZE> { · 44 unmodified lines
5656 /// # Examples
5757 ///
5858 /// ```no_run
59−/// # use kernel::{bindings, io::{Io, IoRaw}};
59+/// # use kernel::{bindings, ffi::c_void, io::{Io, IoRaw}};
6060 /// # use core::ops::Deref;
6161 ///
6262 /// // See also [`pci::Bar`] for a real example.
@@ -70,19 +70,19 @@ impl<const SIZE: usize> IoRaw<SIZE> { · 7 unmodified lines
7070 /// unsafe fn new(paddr: usize) -> Result<Self>{
7171 /// // SAFETY: By the safety requirements of this function [`paddr`, `paddr` + `SIZE`) is
7272 /// // valid for `ioremap`.
73−/// let addr = unsafe { bindings::ioremap(paddr as _, SIZE as _) };
73+/// let addr = unsafe { bindings::ioremap(paddr as bindings::phys_addr_t, SIZE) };
7474 /// if addr.is_null() {
7575 /// return Err(ENOMEM);
7676 /// }
7777 ///
78−/// Ok(IoMem(IoRaw::new(addr as _, SIZE)?))
78+/// Ok(IoMem(IoRaw::new(addr as usize, SIZE)?))
7979 /// }
8080 /// }
8181 ///
8282 /// impl<const SIZE: usize> Drop for IoMem<SIZE> {
8383 /// fn drop(&mut self) {
8484 /// // SAFETY: `self.0.addr()` is guaranteed to be properly mapped by `Self::new`.
85−/// unsafe { bindings::iounmap(self.0.addr() as _); };
85+/// unsafe { bindings::iounmap(self.0.addr() as *mut c_void); };
8686 /// }
8787 /// }
8888 ///
@@ -119,7 +119,7 @@ macro_rules! define_read { · 30 unmodified lines
119119 let addr = self.io_addr_assert::<$type_name>(offset);
120120
121121 // SAFETY: By the type invariant `addr` is a valid address for MMIO operations.
122− unsafe { bindings::$c_fn(addr as _) }
122+ unsafe { bindings::$c_fn(addr as *const c_void) }
123123 }
124124
125125 /// Read IO data from a given offset.
@@ -131,7 +131,7 @@ macro_rules! define_read { · 5 unmodified lines
131131 let addr = self.io_addr::<$type_name>(offset)?;
132132
133133 // SAFETY: By the type invariant `addr` is a valid address for MMIO operations.
134− Ok(unsafe { bindings::$c_fn(addr as _) })
134+ Ok(unsafe { bindings::$c_fn(addr as *const c_void) })
135135 }
136136 };
137137 }
@@ -148,7 +148,7 @@ macro_rules! define_write { · 10 unmodified lines
148148 let addr = self.io_addr_assert::<$type_name>(offset);
149149
150150 // SAFETY: By the type invariant `addr` is a valid address for MMIO operations.
151− unsafe { bindings::$c_fn(value, addr as _, ) }
151+ unsafe { bindings::$c_fn(value, addr as *mut c_void) }
152152 }
153153
154154 /// Write IO data from a given offset.
@@ -160,7 +160,7 @@ macro_rules! define_write { · 5 unmodified lines
160160 let addr = self.io_addr::<$type_name>(offset)?;
161161
162162 // SAFETY: By the type invariant `addr` is a valid address for MMIO operations.
163− unsafe { bindings::$c_fn(value, addr as _) }
163+ unsafe { bindings::$c_fn(value, addr as *mut c_void) }
164164 Ok(())
165165 }
166166 };
rust/kernel/miscdevice.rs+1−1
@@ -34,7 +34,7 @@ impl MiscDeviceOptions { · 33 unmodified lines
3434 pub const fn into_raw<T: MiscDevice>(self) -> bindings::miscdevice {
3535 // SAFETY: All zeros is valid for this C type.
3636 let mut result: bindings::miscdevice = unsafe { MaybeUninit::zeroed().assume_init() };
37− result.minor = bindings::MISC_DYNAMIC_MINOR as _;
37+ result.minor = bindings::MISC_DYNAMIC_MINOR as ffi::c_int;
3838 result.name = self.name.as_char_ptr();
3939 result.fops = MiscdeviceVTable::<T>::build();
4040 result
rust/kernel/mm/virt.rs+26−26
@@ -392,80 +392,80 @@ pub mod flags { · 391 unmodified lines
392392 use crate::bindings;
393393
394394 /// No flags are set.
395− pub const NONE: vm_flags_t = bindings::VM_NONE as _;
395+ pub const NONE: vm_flags_t = bindings::VM_NONE as vm_flags_t;
396396
397397 /// Mapping allows reads.
398− pub const READ: vm_flags_t = bindings::VM_READ as _;
398+ pub const READ: vm_flags_t = bindings::VM_READ as vm_flags_t;
399399
400400 /// Mapping allows writes.
401− pub const WRITE: vm_flags_t = bindings::VM_WRITE as _;
401+ pub const WRITE: vm_flags_t = bindings::VM_WRITE as vm_flags_t;
402402
403403 /// Mapping allows execution.
404− pub const EXEC: vm_flags_t = bindings::VM_EXEC as _;
404+ pub const EXEC: vm_flags_t = bindings::VM_EXEC as vm_flags_t;
405405
406406 /// Mapping is shared.
407− pub const SHARED: vm_flags_t = bindings::VM_SHARED as _;
407+ pub const SHARED: vm_flags_t = bindings::VM_SHARED as vm_flags_t;
408408
409409 /// Mapping may be updated to allow reads.
410− pub const MAYREAD: vm_flags_t = bindings::VM_MAYREAD as _;
410+ pub const MAYREAD: vm_flags_t = bindings::VM_MAYREAD as vm_flags_t;
411411
412412 /// Mapping may be updated to allow writes.
413− pub const MAYWRITE: vm_flags_t = bindings::VM_MAYWRITE as _;
413+ pub const MAYWRITE: vm_flags_t = bindings::VM_MAYWRITE as vm_flags_t;
414414
415415 /// Mapping may be updated to allow execution.
416− pub const MAYEXEC: vm_flags_t = bindings::VM_MAYEXEC as _;
416+ pub const MAYEXEC: vm_flags_t = bindings::VM_MAYEXEC as vm_flags_t;
417417
418418 /// Mapping may be updated to be shared.
419− pub const MAYSHARE: vm_flags_t = bindings::VM_MAYSHARE as _;
419+ pub const MAYSHARE: vm_flags_t = bindings::VM_MAYSHARE as vm_flags_t;
420420
421421 /// Page-ranges managed without `struct page`, just pure PFN.
422− pub const PFNMAP: vm_flags_t = bindings::VM_PFNMAP as _;
422+ pub const PFNMAP: vm_flags_t = bindings::VM_PFNMAP as vm_flags_t;
423423
424424 /// Memory mapped I/O or similar.
425− pub const IO: vm_flags_t = bindings::VM_IO as _;
425+ pub const IO: vm_flags_t = bindings::VM_IO as vm_flags_t;
426426
427427 /// Do not copy this vma on fork.
428− pub const DONTCOPY: vm_flags_t = bindings::VM_DONTCOPY as _;
428+ pub const DONTCOPY: vm_flags_t = bindings::VM_DONTCOPY as vm_flags_t;
429429
430430 /// Cannot expand with mremap().
431− pub const DONTEXPAND: vm_flags_t = bindings::VM_DONTEXPAND as _;
431+ pub const DONTEXPAND: vm_flags_t = bindings::VM_DONTEXPAND as vm_flags_t;
432432
433433 /// Lock the pages covered when they are faulted in.
434− pub const LOCKONFAULT: vm_flags_t = bindings::VM_LOCKONFAULT as _;
434+ pub const LOCKONFAULT: vm_flags_t = bindings::VM_LOCKONFAULT as vm_flags_t;
435435
436436 /// Is a VM accounted object.
437− pub const ACCOUNT: vm_flags_t = bindings::VM_ACCOUNT as _;
437+ pub const ACCOUNT: vm_flags_t = bindings::VM_ACCOUNT as vm_flags_t;
438438
439439 /// Should the VM suppress accounting.
440− pub const NORESERVE: vm_flags_t = bindings::VM_NORESERVE as _;
440+ pub const NORESERVE: vm_flags_t = bindings::VM_NORESERVE as vm_flags_t;
441441
442442 /// Huge TLB Page VM.
443− pub const HUGETLB: vm_flags_t = bindings::VM_HUGETLB as _;
443+ pub const HUGETLB: vm_flags_t = bindings::VM_HUGETLB as vm_flags_t;
444444
445445 /// Synchronous page faults. (DAX-specific)
446− pub const SYNC: vm_flags_t = bindings::VM_SYNC as _;
446+ pub const SYNC: vm_flags_t = bindings::VM_SYNC as vm_flags_t;
447447
448448 /// Architecture-specific flag.
449− pub const ARCH_1: vm_flags_t = bindings::VM_ARCH_1 as _;
449+ pub const ARCH_1: vm_flags_t = bindings::VM_ARCH_1 as vm_flags_t;
450450
451451 /// Wipe VMA contents in child on fork.
452− pub const WIPEONFORK: vm_flags_t = bindings::VM_WIPEONFORK as _;
452+ pub const WIPEONFORK: vm_flags_t = bindings::VM_WIPEONFORK as vm_flags_t;
453453
454454 /// Do not include in the core dump.
455− pub const DONTDUMP: vm_flags_t = bindings::VM_DONTDUMP as _;
455+ pub const DONTDUMP: vm_flags_t = bindings::VM_DONTDUMP as vm_flags_t;
456456
457457 /// Not soft dirty clean area.
458− pub const SOFTDIRTY: vm_flags_t = bindings::VM_SOFTDIRTY as _;
458+ pub const SOFTDIRTY: vm_flags_t = bindings::VM_SOFTDIRTY as vm_flags_t;
459459
460460 /// Can contain `struct page` and pure PFN pages.
461− pub const MIXEDMAP: vm_flags_t = bindings::VM_MIXEDMAP as _;
461+ pub const MIXEDMAP: vm_flags_t = bindings::VM_MIXEDMAP as vm_flags_t;
462462
463463 /// MADV_HUGEPAGE marked this vma.
464− pub const HUGEPAGE: vm_flags_t = bindings::VM_HUGEPAGE as _;
464+ pub const HUGEPAGE: vm_flags_t = bindings::VM_HUGEPAGE as vm_flags_t;
465465
466466 /// MADV_NOHUGEPAGE marked this vma.
467− pub const NOHUGEPAGE: vm_flags_t = bindings::VM_NOHUGEPAGE as _;
467+ pub const NOHUGEPAGE: vm_flags_t = bindings::VM_NOHUGEPAGE as vm_flags_t;
468468
469469 /// KSM may merge identical pages.
470− pub const MERGEABLE: vm_flags_t = bindings::VM_MERGEABLE as _;
470+ pub const MERGEABLE: vm_flags_t = bindings::VM_MERGEABLE as vm_flags_t;
471471 }
rust/kernel/of.rs+3−3
@@ -22,7 +22,7 @@ unsafe impl RawDeviceId for DeviceId { · 21 unmodified lines
2222 const DRIVER_DATA_OFFSET: usize = core::mem::offset_of!(bindings::of_device_id, data);
2323
2424 fn index(&self) -> usize {
25− self.0.data as _
25+ self.0.data as usize
2626 }
2727 }
2828
@@ -34,10 +34,10 @@ impl DeviceId { · 5 unmodified lines
3434 // SAFETY: FFI type is valid to be zero-initialized.
3535 let mut of: bindings::of_device_id = unsafe { core::mem::zeroed() };
3636
37− // TODO: Use `clone_from_slice` once the corresponding types do match.
37+ // TODO: Use `copy_from_slice` once stabilized for `const`.
3838 let mut i = 0;
3939 while i < src.len() {
40− of.compatible[i] = src[i] as _;
40+ of.compatible[i] = src[i];
4141 i += 1;
4242 }
4343
rust/kernel/pci.rs+6−3
@@ -171,7 +171,7 @@ unsafe impl RawDeviceId for DeviceId { · 170 unmodified lines
171171 const DRIVER_DATA_OFFSET: usize = core::mem::offset_of!(bindings::pci_device_id, driver_data);
172172
173173 fn index(&self) -> usize {
174− self.0.driver_data as _
174+ self.0.driver_data
175175 }
176176 }
177177
@@ -206,7 +206,10 @@ macro_rules! pci_device_table { · 28 unmodified lines
206206 /// MODULE_PCI_TABLE,
207207 /// <MyDriver as pci::Driver>::IdInfo,
208208 /// [
209−/// (pci::DeviceId::from_id(bindings::PCI_VENDOR_ID_REDHAT, bindings::PCI_ANY_ID as _), ())
209+/// (
210+/// pci::DeviceId::from_id(bindings::PCI_VENDOR_ID_REDHAT, bindings::PCI_ANY_ID as u32),
211+/// (),
212+/// )
210213 /// ]
211214 /// );
212215 ///
@@ -330,7 +333,7 @@ impl<const SIZE: usize> Bar<SIZE> { · 117 unmodified lines
330333 // `ioptr` is valid by the safety requirements.
331334 // `num` is valid by the safety requirements.
332335 unsafe {
333− bindings::pci_iounmap(pdev.as_raw(), ioptr as _);
336+ bindings::pci_iounmap(pdev.as_raw(), ioptr as *mut kernel::ffi::c_void);
334337 bindings::pci_release_region(pdev.as_raw(), num);
335338 }
336339 }
rust/kernel/str.rs+4−4
@@ -728,9 +728,9 @@ impl RawFormatter { · 727 unmodified lines
728728 pub(crate) unsafe fn from_ptrs(pos: *mut u8, end: *mut u8) -> Self {
729729 // INVARIANT: The safety requirements guarantee the type invariants.
730730 Self {
731− beg: pos as _,
732− pos: pos as _,
733− end: end as _,
731+ beg: pos as usize,
732+ pos: pos as usize,
733+ end: end as usize,
734734 }
735735 }
736736
@@ -755,7 +755,7 @@ impl RawFormatter { · 18 unmodified lines
755755 ///
756756 /// N.B. It may point to invalid memory.
757757 pub(crate) fn pos(&self) -> *mut u8 {
758− self.pos as _
758+ self.pos as *mut u8
759759 }
760760
761761 /// Returns the number of bytes written to the formatter.
rust/kernel/workqueue.rs+1−1
@@ -198,7 +198,7 @@ impl Queue { · 197 unmodified lines
198198 unsafe {
199199 w.__enqueue(move |work_ptr| {
200200 bindings::queue_work_on(
201− bindings::wq_misc_consts_WORK_CPU_UNBOUND as _,
201+ bindings::wq_misc_consts_WORK_CPU_UNBOUND as ffi::c_int,
202202 queue_ptr,
203203 work_ptr,
204204 )

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t