Skip to content

Commit 0ba2a3a

Browse files
committed
enable clippy::as_conversions to fully rule out as-casts
1 parent d5240cc commit 0ba2a3a

File tree

16 files changed

+61
-57
lines changed

16 files changed

+61
-57
lines changed

src/tools/miri/src/borrow_tracker/tree_borrows/unimap.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use std::mem;
1717

1818
use rustc_data_structures::fx::FxHashMap;
1919

20+
use crate::helpers::ToUsize;
21+
2022
/// Intermediate key between a UniKeyMap and a UniValMap.
2123
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2224
pub struct UniIndex {
@@ -158,7 +160,7 @@ where
158160
impl<V> UniValMap<V> {
159161
/// Whether this index has an associated value.
160162
pub fn contains_idx(&self, idx: UniIndex) -> bool {
161-
self.data.get(idx.idx as usize).and_then(Option::as_ref).is_some()
163+
self.data.get(idx.idx.to_usize()).and_then(Option::as_ref).is_some()
162164
}
163165

164166
/// Reserve enough space to insert the value at the right index.
@@ -174,29 +176,29 @@ impl<V> UniValMap<V> {
174176

175177
/// Assign a value to the index. Permanently overwrites any previous value.
176178
pub fn insert(&mut self, idx: UniIndex, val: V) {
177-
self.extend_to_length(idx.idx as usize + 1);
178-
self.data[idx.idx as usize] = Some(val)
179+
self.extend_to_length(idx.idx.to_usize() + 1);
180+
self.data[idx.idx.to_usize()] = Some(val)
179181
}
180182

181183
/// Get the value at this index, if it exists.
182184
pub fn get(&self, idx: UniIndex) -> Option<&V> {
183-
self.data.get(idx.idx as usize).and_then(Option::as_ref)
185+
self.data.get(idx.idx.to_usize()).and_then(Option::as_ref)
184186
}
185187

186188
/// Get the value at this index mutably, if it exists.
187189
pub fn get_mut(&mut self, idx: UniIndex) -> Option<&mut V> {
188-
self.data.get_mut(idx.idx as usize).and_then(Option::as_mut)
190+
self.data.get_mut(idx.idx.to_usize()).and_then(Option::as_mut)
189191
}
190192

191193
/// Delete any value associated with this index.
192194
/// Returns None if the value was not present, otherwise
193195
/// returns the previously stored value.
194196
pub fn remove(&mut self, idx: UniIndex) -> Option<V> {
195-
if idx.idx as usize >= self.data.len() {
197+
if idx.idx.to_usize() >= self.data.len() {
196198
return None;
197199
}
198200
let mut res = None;
199-
mem::swap(&mut res, &mut self.data[idx.idx as usize]);
201+
mem::swap(&mut res, &mut self.data[idx.idx.to_usize()]);
200202
res
201203
}
202204
}
@@ -209,8 +211,8 @@ pub struct UniEntry<'a, V> {
209211
impl<'a, V> UniValMap<V> {
210212
/// Get a wrapper around a mutable access to the value corresponding to `idx`.
211213
pub fn entry(&'a mut self, idx: UniIndex) -> UniEntry<'a, V> {
212-
self.extend_to_length(idx.idx as usize + 1);
213-
UniEntry { inner: &mut self.data[idx.idx as usize] }
214+
self.extend_to_length(idx.idx.to_usize() + 1);
215+
UniEntry { inner: &mut self.data[idx.idx.to_usize()] }
214216
}
215217
}
216218

src/tools/miri/src/concurrency/cpu_affinity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl CpuAffinityMask {
2525
let mut this = Self([0; Self::CPU_MASK_BYTES]);
2626

2727
// the default affinity mask includes only the available CPUs
28-
for i in 0..cpu_count as usize {
28+
for i in 0..cpu_count.to_usize() {
2929
this.set(cx, i);
3030
}
3131

src/tools/miri/src/concurrency/vector_clock.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_span::{DUMMY_SP, Span, SpanData};
77
use smallvec::SmallVec;
88

99
use super::data_race::NaReadType;
10+
use crate::helpers::ToUsize;
1011

1112
/// A vector clock index, this is associated with a thread id
1213
/// but in some cases one vector index may be shared with
@@ -157,7 +158,7 @@ impl VClock {
157158

158159
#[inline]
159160
pub(super) fn index_mut(&mut self, index: VectorIdx) -> &mut VTimestamp {
160-
self.0.as_mut_slice().get_mut(index.to_u32() as usize).unwrap()
161+
self.0.as_mut_slice().get_mut(index.to_u32().to_usize()).unwrap()
161162
}
162163

163164
/// Get a mutable slice to the internal vector with minimum `min_len`
@@ -420,7 +421,7 @@ impl Index<VectorIdx> for VClock {
420421

421422
#[inline]
422423
fn index(&self, index: VectorIdx) -> &VTimestamp {
423-
self.as_slice().get(index.to_u32() as usize).unwrap_or(&VTimestamp::ZERO)
424+
self.as_slice().get(index.to_u32().to_usize()).unwrap_or(&VTimestamp::ZERO)
424425
}
425426
}
426427

src/tools/miri/src/helpers.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,3 +1412,14 @@ pub(crate) fn windows_check_buffer_size((success, len): (bool, u64)) -> u32 {
14121412
u32::try_from(len).unwrap()
14131413
}
14141414
}
1415+
1416+
/// We don't support 16-bit systems, so let's have ergonomic conversion from `u32` to `usize`.
1417+
pub trait ToUsize {
1418+
fn to_usize(self) -> usize;
1419+
}
1420+
1421+
impl ToUsize for u32 {
1422+
fn to_usize(self) -> usize {
1423+
self.try_into().unwrap()
1424+
}
1425+
}

src/tools/miri/src/intrinsics/simd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
634634
let index_len = index.len();
635635

636636
assert_eq!(left_len, right_len);
637-
assert_eq!(index_len as u64, dest_len);
637+
assert_eq!(u64::try_from(index_len).unwrap(), dest_len);
638638

639639
for i in 0..dest_len {
640640
let src_index: u64 =

src/tools/miri/src/lib.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,7 @@
4141
rustc::potential_query_instability,
4242
rustc::untranslatable_diagnostic,
4343
)]
44-
#![warn(
45-
rust_2018_idioms,
46-
unqualified_local_imports,
47-
clippy::cast_possible_wrap, // unsigned -> signed
48-
clippy::cast_sign_loss, // signed -> unsigned
49-
clippy::cast_lossless,
50-
clippy::cast_possible_truncation,
51-
)]
44+
#![warn(rust_2018_idioms, unqualified_local_imports, clippy::as_conversions)]
5245
// Needed for rustdoc from bootstrap (with `-Znormalize-docs`).
5346
#![recursion_limit = "256"]
5447

@@ -140,7 +133,7 @@ pub use crate::eval::{
140133
AlignmentCheck, BacktraceStyle, IsolatedOp, MiriConfig, MiriEntryFnType, RejectOpWith,
141134
ValidationMode, create_ecx, eval_entry,
142135
};
143-
pub use crate::helpers::{AccessKind, EvalContextExt as _};
136+
pub use crate::helpers::{AccessKind, EvalContextExt as _, ToUsize as _};
144137
pub use crate::intrinsics::EvalContextExt as _;
145138
pub use crate::machine::{
146139
AllocExtra, DynMachineCallback, FrameExtra, MachineCallback, MemoryKind, MiriInterpCx,

src/tools/miri/src/shims/foreign_items.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
639639
let val = this.read_scalar(val)?.to_i32()?;
640640
let num = this.read_target_usize(num)?;
641641
// The docs say val is "interpreted as unsigned char".
642-
#[expect(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
642+
#[expect(clippy::as_conversions)]
643643
let val = val as u8;
644644

645645
// C requires that this must always be a valid pointer (C18 §7.1.4).
@@ -665,7 +665,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
665665
let val = this.read_scalar(val)?.to_i32()?;
666666
let num = this.read_target_usize(num)?;
667667
// The docs say val is "interpreted as unsigned char".
668-
#[expect(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
668+
#[expect(clippy::as_conversions)]
669669
let val = val as u8;
670670

671671
// C requires that this must always be a valid pointer (C18 §7.1.4).
@@ -676,7 +676,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
676676
.iter()
677677
.position(|&c| c == val);
678678
if let Some(idx) = idx {
679-
let new_ptr = ptr.wrapping_offset(Size::from_bytes(idx as u64), this);
679+
let new_ptr = ptr.wrapping_offset(Size::from_bytes(idx), this);
680680
this.write_pointer(new_ptr, dest)?;
681681
} else {
682682
this.write_null(dest)?;

src/tools/miri/src/shims/native_lib.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,10 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
9494
// Try getting the function from the shared library.
9595
// On windows `_lib_path` will be unused, hence the name starting with `_`.
9696
let (lib, _lib_path) = this.machine.native_lib.as_ref().unwrap();
97-
let func: libloading::Symbol<'_, unsafe extern "C" fn()> = unsafe {
98-
match lib.get(link_name.as_str().as_bytes()) {
99-
Ok(x) => x,
100-
Err(_) => {
101-
return None;
102-
}
103-
}
104-
};
97+
let func: libloading::Symbol<'_, unsafe extern "C" fn()> =
98+
unsafe { lib.get(link_name.as_str().as_bytes()).ok()? };
99+
#[expect(clippy::as_conversions)] // fn-ptr to raw-ptr cast needs `as`.
100+
let fn_ptr = *func.deref() as *mut std::ffi::c_void;
105101

106102
// FIXME: this is a hack!
107103
// The `libloading` crate will automatically load system libraries like `libc`.
@@ -116,7 +112,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
116112
// using the `libc` crate where this interface is public.
117113
let mut info = std::mem::MaybeUninit::<libc::Dl_info>::uninit();
118114
unsafe {
119-
if libc::dladdr(*func.deref() as *const _, info.as_mut_ptr()) != 0 {
115+
if libc::dladdr(fn_ptr, info.as_mut_ptr()) != 0 {
120116
let info = info.assume_init();
121117
#[cfg(target_os = "cygwin")]
122118
let fname_ptr = info.dli_fname.as_ptr();
@@ -129,8 +125,9 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
129125
}
130126
}
131127
}
128+
132129
// Return a pointer to the function.
133-
Some(CodePtr(*func.deref() as *mut _))
130+
Some(CodePtr(fn_ptr))
134131
}
135132
}
136133

src/tools/miri/src/shims/unix/android/thread.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::helpers::check_min_vararg_count;
77
use crate::shims::unix::thread::{EvalContextExt as _, ThreadNameResult};
88
use crate::*;
99

10-
const TASK_COMM_LEN: usize = 16;
10+
const TASK_COMM_LEN: u32 = 16;
1111

1212
pub fn prctl<'tcx>(
1313
ecx: &mut MiriInterpCx<'tcx>,
@@ -29,16 +29,20 @@ pub fn prctl<'tcx>(
2929
let thread = ecx.pthread_self()?;
3030
// The Linux kernel silently truncates long names.
3131
// https://www.man7.org/linux/man-pages/man2/PR_SET_NAME.2const.html
32-
let res =
33-
ecx.pthread_setname_np(thread, name, TASK_COMM_LEN, /* truncate */ true)?;
32+
let res = ecx.pthread_setname_np(
33+
thread,
34+
name,
35+
TASK_COMM_LEN.to_usize(),
36+
/* truncate */ true,
37+
)?;
3438
assert_eq!(res, ThreadNameResult::Ok);
3539
Scalar::from_u32(0)
3640
}
3741
op if op == pr_get_name => {
3842
let [name] = check_min_vararg_count("prctl(PR_GET_NAME, ...)", varargs)?;
3943
let name = ecx.read_scalar(name)?;
4044
let thread = ecx.pthread_self()?;
41-
let len = Scalar::from_target_usize(TASK_COMM_LEN as u64, ecx);
45+
let len = Scalar::from_target_usize(TASK_COMM_LEN.into(), ecx);
4246
ecx.check_ptr_access(
4347
name.to_pointer(ecx)?,
4448
Size::from_bytes(TASK_COMM_LEN),

src/tools/miri/src/shims/unix/fs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ impl UnixFileDescription for FileHandle {
121121
use std::os::windows::io::AsRawHandle;
122122

123123
use windows_sys::Win32::Foundation::{
124-
ERROR_IO_PENDING, ERROR_LOCK_VIOLATION, FALSE, HANDLE, TRUE,
124+
ERROR_IO_PENDING, ERROR_LOCK_VIOLATION, FALSE, TRUE,
125125
};
126126
use windows_sys::Win32::Storage::FileSystem::{
127127
LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY, LockFileEx, UnlockFile,
128128
};
129129

130-
let fh = self.file.as_raw_handle() as HANDLE;
130+
let fh = self.file.as_raw_handle();
131131

132132
use FlockOp::*;
133133
let (ret, lock_nb) = match op {

0 commit comments

Comments
 (0)