Skip to content

Commit 5e30550

Browse files
tamirdojeda
authored andcommitted
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. Link: https://rust-lang.github.io/rust-clippy/master/index.html#as_underscore [1] Reviewed-by: Benno Lossin <[email protected]> Reviewed-by: Boqun Feng <[email protected]> Signed-off-by: Tamir Duberstein <[email protected]> Acked-by: Greg Kroah-Hartman <[email protected]> Acked-by: Danilo Krummrich <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Changed `isize` to `c_long`. - Miguel ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 23773bd commit 5e30550

File tree

16 files changed

+70
-60
lines changed

16 files changed

+70
-60
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ export rust_common_flags := --edition=2021 \
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ kernel::pci_device_table!(
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl<T: Operations> OperationsVTable<T> {
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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,12 @@ impl<T: Operations> Request<T> {
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl<T: RawDeviceId, U, const N: usize> IdArray<T, U, N> {
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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ struct DevresInner<T> {
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> {
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> {
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub struct Attrs(u32);
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl<T: drm::Driver> Device<T> {
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl Error {
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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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> {
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> {
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 {
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 {
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 {
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 {
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
};

0 commit comments

Comments
 (0)