Skip to content

Commit 9622619

Browse files
refactor: add as_conversions clippy correctness lint (#1021)
Co-authored-by: Benoît CORTIER <git.divisible626@passmail.com>
1 parent d3e0cb1 commit 9622619

File tree

52 files changed

+525
-263
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+525
-263
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn_to_numeric_cast_any = "warn"
9292
ptr_cast_constness = "warn"
9393

9494
# == Correctness == #
95-
#as_conversions = "warn"
95+
as_conversions = "warn"
9696
cast_lossless = "warn"
9797
cast_possible_truncation = "warn"
9898
cast_possible_wrap = "warn"

benches/src/perfenc.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async fn main() -> Result<(), anyhow::Error> {
6868
let mut updates = DisplayUpdates::new(file, DesktopSize { width, height }, fps);
6969
while let Some(up) = updates.next_update().await? {
7070
if let DisplayUpdate::Bitmap(ref up) = up {
71-
total_raw += up.data.len() as u64;
71+
total_raw += u64::try_from(up.data.len())?;
7272
} else {
7373
eprintln!("Invalid update");
7474
break;
@@ -78,7 +78,7 @@ async fn main() -> Result<(), anyhow::Error> {
7878
let Some(frag) = iter.next().await else {
7979
break;
8080
};
81-
let len = frag?.data.len() as u64;
81+
let len = u64::try_from(frag?.data.len())?;
8282
total_enc += len;
8383
}
8484
n_updates += 1;
@@ -87,6 +87,7 @@ async fn main() -> Result<(), anyhow::Error> {
8787
}
8888
println!();
8989

90+
#[expect(clippy::as_conversions, reason = "casting u64 to f64")]
9091
let ratio = total_enc as f64 / total_raw as f64;
9192
let percent = 100.0 - ratio * 100.0;
9293
println!("Encoder: {encoder:?}");

clippy.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
msrv = "1.84"
1+
msrv = "1.87"
22
semicolon-outside-block-ignore-multiline = true
33
accept-comment-above-statement = true
44
accept-comment-above-attributes = true

crates/ironrdp-client/src/app.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ impl App {
6666
let Some((window, _)) = self.window.as_mut() else {
6767
return;
6868
};
69+
#[expect(clippy::as_conversions, reason = "casting f64 to u32")]
6970
let scale_factor = (window.scale_factor() * 100.0) as u32;
7071

7172
let width = u16::try_from(size.width).expect("reasonable width");
@@ -222,8 +223,10 @@ impl ApplicationHandler<RdpOutputEvent> for App {
222223
}
223224
WindowEvent::CursorMoved { position, .. } => {
224225
let win_size = window.inner_size();
225-
let x = (position.x / win_size.width as f64 * self.buffer_size.0 as f64) as u16;
226-
let y = (position.y / win_size.height as f64 * self.buffer_size.1 as f64) as u16;
226+
#[expect(clippy::as_conversions, reason = "casting f64 to u16")]
227+
let x = (position.x / f64::from(win_size.width) * f64::from(self.buffer_size.0)) as u16;
228+
#[expect(clippy::as_conversions, reason = "casting f64 to u16")]
229+
let y = (position.y / f64::from(win_size.height) * f64::from(self.buffer_size.1)) as u16;
227230
let operation = ironrdp::input::Operation::MouseMove(ironrdp::input::MousePosition { x, y });
228231

229232
let input_events = self.input_database.apply(core::iter::once(operation));
@@ -239,6 +242,7 @@ impl ApplicationHandler<RdpOutputEvent> for App {
239242
operations.push(ironrdp::input::Operation::WheelRotations(
240243
ironrdp::input::WheelRotations {
241244
is_vertical: false,
245+
#[expect(clippy::as_conversions, reason = "casting f32 to i16")]
242246
rotation_units: (delta_x * 100.) as i16,
243247
},
244248
));
@@ -248,6 +252,7 @@ impl ApplicationHandler<RdpOutputEvent> for App {
248252
operations.push(ironrdp::input::Operation::WheelRotations(
249253
ironrdp::input::WheelRotations {
250254
is_vertical: true,
255+
#[expect(clippy::as_conversions, reason = "casting f32 to i16")]
251256
rotation_units: (delta_y * 100.) as i16,
252257
},
253258
));
@@ -258,6 +263,7 @@ impl ApplicationHandler<RdpOutputEvent> for App {
258263
operations.push(ironrdp::input::Operation::WheelRotations(
259264
ironrdp::input::WheelRotations {
260265
is_vertical: false,
266+
#[expect(clippy::as_conversions, reason = "casting f64 to i16")]
261267
rotation_units: delta.x as i16,
262268
},
263269
));
@@ -267,6 +273,7 @@ impl ApplicationHandler<RdpOutputEvent> for App {
267273
operations.push(ironrdp::input::Operation::WheelRotations(
268274
ironrdp::input::WheelRotations {
269275
is_vertical: true,
276+
#[expect(clippy::as_conversions, reason = "casting f64 to i16")]
270277
rotation_units: delta.y as i16,
271278
},
272279
));

crates/ironrdp-client/src/rdp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ async fn connect(
235235

236236
let upgraded = ironrdp_tokio::mark_as_upgraded(should_upgrade, &mut connector);
237237

238-
let erased_stream = Box::new(upgraded_stream) as Box<dyn AsyncReadWrite + Unpin + Send + Sync>;
238+
let erased_stream: Box<dyn AsyncReadWrite + Unpin + Send + Sync> = Box::new(upgraded_stream);
239239
let mut upgraded_framed = ironrdp_tokio::TokioFramed::new_with_leftover(erased_stream, leftover_bytes);
240240

241241
let connection_result = ironrdp_tokio::connect_finalize(
@@ -336,7 +336,7 @@ async fn connect_ws(
336336
.await?;
337337

338338
let (ws, leftover_bytes) = framed.into_inner();
339-
let erased_stream = Box::new(ws) as Box<dyn AsyncReadWrite + Unpin + Send + Sync>;
339+
let erased_stream: Box<dyn AsyncReadWrite + Unpin + Send + Sync> = Box::new(ws);
340340
let upgraded_framed = ironrdp_tokio::TokioFramed::new_with_leftover(erased_stream, leftover_bytes);
341341

342342
Ok((connection_result, upgraded_framed))

crates/ironrdp-cliprdr-native/src/windows/clipboard_data_ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl<'a> ClipboardDataRef<'a> {
2727
};
2828

2929
// SAFETY: It is safe to call `GlobalLock` on the valid handle.
30-
let data = unsafe { GlobalLock(handle) } as *const u8;
30+
let data = unsafe { GlobalLock(handle) }.cast::<u8>().cast_const();
3131

3232
if data.is_null() {
3333
// Can't lock data handle, handle is not valid anymore (e.g. clipboard has changed)

crates/ironrdp-cliprdr-native/src/windows/clipboard_impl.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::ptr::with_exposed_provenance_mut;
12
use core::time::Duration;
23
use std::collections::HashSet;
34
use std::sync::mpsc;
@@ -320,17 +321,19 @@ pub(crate) unsafe extern "system" fn clipboard_subproc(
320321

321322
// SAFETY: `data` is a valid pointer, returned by `Box::into_raw`, transferred to OS earlier
322323
// via `SetWindowSubclass` call.
323-
let _ = unsafe { Box::from_raw(data as *mut WinClipboardImpl) };
324+
let _ = unsafe { Box::from_raw(with_exposed_provenance_mut::<WinClipboardImpl>(data)) };
324325
return LRESULT(0);
325326
}
326327

327328
// SAFETY: `data` is a valid pointer, returned by `Box::into_raw`, transferred to OS earlier
328329
// via `SetWindowSubclass` call.
329-
let ctx = unsafe { &mut *(data as *mut WinClipboardImpl) };
330+
let ctx = unsafe { &mut *(with_exposed_provenance_mut::<WinClipboardImpl>(data)) };
330331

331332
match msg {
332333
// We need to keep track of window state to distinguish between local and remote copy
333-
WM_ACTIVATE | WM_ACTIVATEAPP => ctx.window_is_active = wparam.0 != WA_INACTIVE as usize, // `as` conversion is fine for constants
334+
WM_ACTIVATE | WM_ACTIVATEAPP => {
335+
ctx.window_is_active = wparam.0 != usize::try_from(WA_INACTIVE).expect("WA_INACTIVE fits into usize")
336+
}
334337
// Sent by the OS when OS clipboard content is changed
335338
WM_CLIPBOARDUPDATE => {
336339
// SAFETY: `GetClipboardOwner` is always safe to call.
@@ -347,8 +350,9 @@ pub(crate) unsafe extern "system" fn clipboard_subproc(
347350
}
348351
// Sent by the OS when delay-rendered data is requested for rendering.
349352
WM_RENDERFORMAT => {
350-
#[expect(clippy::cast_possible_truncation)] // should never truncate in practice
351-
ctx.handle_event(BackendEvent::RenderFormat(ClipboardFormatId::new(wparam.0 as u32)));
353+
ctx.handle_event(BackendEvent::RenderFormat(ClipboardFormatId::new(
354+
u32::try_from(wparam.0).expect("should never truncate in practice"),
355+
)));
352356
}
353357
// Sent by the OS when all delay-rendered data is requested for rendering.
354358
WM_RENDERALLFORMATS => {

crates/ironrdp-cliprdr-native/src/windows/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,14 @@ impl WinClipboard {
200200
//
201201
// SAFETY: `window` is a valid window handle, `clipboard_subproc` is in the static memory,
202202
// `ctx` is valid and its ownership is transferred to the subclass via `into_raw`.
203-
let winapi_result =
204-
unsafe { SetWindowSubclass(window, Some(clipboard_subproc), 0, Box::into_raw(ctx) as usize) };
203+
let winapi_result = unsafe {
204+
SetWindowSubclass(
205+
window,
206+
Some(clipboard_subproc),
207+
0,
208+
Box::into_raw(ctx).expose_provenance(),
209+
)
210+
};
205211

206212
if winapi_result == FALSE {
207213
return Err(WinCliprdrError::WindowSubclass);

crates/ironrdp-cliprdr-native/src/windows/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl GlobalMemoryBuffer {
2626
// - `dst` is valid for writes of `data.len()` bytes, we allocated enough above.
2727
// - Both `data` and `dst` are properly aligned: u8 alignment is 1
2828
// - Memory regions are not overlapping, `dst` was allocated by us just above.
29-
unsafe { core::ptr::copy_nonoverlapping(data.as_ptr(), dst as *mut u8, data.len()) };
29+
unsafe { core::ptr::copy_nonoverlapping(data.as_ptr(), dst.cast::<u8>(), data.len()) };
3030

3131
// SAFETY: We called `GlobalLock` on this handle just above.
3232
if let Err(error) = unsafe { GlobalUnlock(handle) } {

crates/ironrdp-cliprdr/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#![cfg_attr(doc, doc = include_str!("../README.md"))]
22
#![doc(html_logo_url = "https://cdnweb.devolutions.net/images/projects/devolutions/logos/devolutions-icon-shadow.svg")]
33
#![allow(clippy::arithmetic_side_effects)] // FIXME: remove
4-
#![allow(clippy::cast_lossless)] // FIXME: remove
5-
#![allow(clippy::cast_possible_truncation)] // FIXME: remove
6-
#![allow(clippy::cast_possible_wrap)] // FIXME: remove
7-
#![allow(clippy::cast_sign_loss)] // FIXME: remove
84

95
pub mod backend;
106
pub mod pdu;

0 commit comments

Comments
 (0)