8000 rust: enable `clippy::cast_lossless` lint · Rust-for-Linux/linux@b7c8d7a · GitHub
[go: up one dir, main page]

Skip to content

Commit b7c8d7a

Browse files
tamirdojeda
authored andcommitted
rust: enable clippy::cast_lossless lint
Before Rust 1.29.0, Clippy introduced the `cast_lossless` lint [1]: > Rust’s `as` keyword will perform many kinds of conversions, including > silently lossy conversions. Conversion functions such as `i32::from` > will only perform lossless conversions. Using the conversion functions > prevents conversions from becoming silently lossy if the input types > ever change, and makes it clear for people reading the code that the > conversion is lossless. 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#cast_lossless [1] Suggested-by: Benno Lossin <benno.lossin@proton.me> Link: https://lore.kernel.org/all/D8ORTXSUTKGL.1KOJAGBM8F8TN@proton.me/ Reviewed-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Boqun Feng <boqun.feng@gmail.com> Signed-off-by: Tamir Duberstein <tamird@gmail.com> Acked-by: FUJITA Tomonori <fujita.tomonori@gmail.com> Acked-by: Jocelyn Falempe <jfalempe@redhat.com> Acked-by: Danilo Krummrich <dakr@kernel.org> Link: https://lore.kernel.org/r/20250615-ptr-as-ptr-v12-5-f43b024581e8@gmail.com Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 5e30550 commit b7c8d7a

File tree

7 files changed

+9
-6
lines changed

7 files changed

+9
-6
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ export rust_common_flags := --edition=2021 \
481481
-Wclippy::all \
482482
-Wclippy::as_ptr_cast_mut \
483483
-Wclippy::as_underscore \
484+
-Wclippy::cast_lossless \
484485
-Wclippy::ignored_unit_patterns \
485486
-Wclippy::mut_mut \
486487
-Wclippy::needless_bitwise_bool \

drivers/gpu/drm/drm_panic_qr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ impl DecFifo {
404404
let mut out = 0;
405405
let mut exp = 1;
406406
for i in 0..poplen {
407-
out += self.decimals[self.len + i] as u16 * exp;
407+
out += u16::from(self.decimals[self.len + i]) * exp;
408408
exp *= 10;
409409
}
410410
Some((out, NUM_CHARS_BITS[poplen]))
@@ -425,7 +425,7 @@ impl Iterator for SegmentIterator<'_> {
425425
match self.segment {
426426
Segment::Binary(data) => {
427427
if self.offset < data.len() {
428-
let byte = data[self.offset] as u16;
428+
let byte = u16::from(data[self.offset]);
429429
self.offset += 1;
430430
Some((byte, 8))
431431
} else {

drivers/gpu/nova-core/regs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl NV_PMC_BOOT_0 {
3232
pub(crate) fn chipset(self) -> Result<Chipset> {
3333
self.architecture()
3434
.map(|arch| {
35-
((arch as u32) << Self::IMPLEMENTATION.len()) | self.implementation() as u32
35+
((arch as u32) << Self::IMPLEMENTATION.len()) | u32::from(self.implementation())
3636
})
3737
.and_then(Chipset::try_from)
3838
}

drivers/gpu/nova-core/regs/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ macro_rules! register {
264264
pub(crate) fn [<set_ $field>](mut self, value: $to_type) -> Self {
265265
const MASK: u32 = $name::[<$field:upper _MASK>];
266266
const SHIFT: u32 = $name::[<$field:upper _SHIFT>];
267-
let value = ((value as u32) << SHIFT) & MASK;
267+
let value = (u32::from(value) << SHIFT) & MASK;
268268
self.0 = (self.0 & !MASK) | value;
269269

270270
self

rust/bindings/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
)]
2626

2727
#[allow(dead_code)]
28+
#[allow(clippy::cast_lossless)]
2829
#[allow(clippy::ptr_as_ptr)]
2930
#[allow(clippy::undocumented_unsafe_blocks)]
3031
#[cfg_attr(CONFIG_RUSTC_HAS_UNNECESSARY_TRANSMUTES, allow(unnecessary_transmutes))]

rust/kernel/net/phy.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl Device {
142142
// SAFETY: The struct invariant ensures that we may access
143143
// this field without additional synchronization.
144144
let bit_field = unsafe { &(*self.0.get())._bitfield_1 };
145-
bit_field.get(13, 1) == bindings::AUTONEG_ENABLE as u64
145+
bit_field.get(13, 1) == u64::from(bindings::AUTONEG_ENABLE)
146146
}
147147

148148
/// Gets the current auto-negotiation state.
@@ -427,7 +427,7 @@ impl<T: Driver> Adapter<T> {
427427
// where we hold `phy_device->lock`, so the accessors on
428428
// `Device` are okay to call.
429429
let dev = unsafe { Device::from_raw(phydev) };
430-
T::match_phy_device(dev) as i32
430+
T::match_phy_device(dev).into()
431431
}
432432

433433
/// # Safety

rust/uapi/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![cfg_attr(test, allow(unsafe_op_in_unsafe_fn))]
1515
#![allow(
1616
clippy::all,
17+
clippy::cast_lossless,
1718
clippy::ptr_as_ptr,
1819
clippy::undocumented_unsafe_blocks,
1920
dead_code,

0 commit comments

Comments
 (0)
0