Skip to content

Commit b67a6ff

Browse files
committed
pass clippy::cast_sign_loss and clippy::cast_possible_wrap
1 parent 3f0fdf2 commit b67a6ff

File tree

6 files changed

+25
-7
lines changed

6 files changed

+25
-7
lines changed

src/intptrcast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ impl<'mir, 'tcx> GlobalStateInner {
230230

231231
// Wrapping "addr - base_addr"
232232
let dl = ecx.data_layout();
233+
#[allow(clippy::cast_possible_wrap)] // we want to wrap here
233234
let neg_base_addr = (base_addr as i64).wrapping_neg();
234235
Some((
235236
alloc_id,

src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
clippy::derive_hash_xor_eq,
2525
clippy::too_many_arguments
2626
)]
27-
#![warn(rust_2018_idioms, clippy::cast_lossless)]
27+
#![warn(
28+
rust_2018_idioms,
29+
clippy::cast_possible_wrap, // unsigned -> signed
30+
clippy::cast_sign_loss, // signed -> unsigned
31+
clippy::cast_lossless,
32+
)]
2833

2934
extern crate rustc_apfloat;
3035
extern crate rustc_ast;

src/shims/env.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
459459
// The reason we need to do this wacky of a conversion is because
460460
// `libc::getpid` returns an i32, however, `std::process::id()` return an u32.
461461
// So we un-do the conversion that stdlib does and turn it back into an i32.
462-
462+
#[allow(clippy::cast_possible_wrap)]
463463
Ok(std::process::id() as i32)
464464
}
465465

src/shims/foreign_items.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
526526
"memrchr" => {
527527
let [ptr, val, num] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
528528
let ptr = this.read_pointer(ptr)?;
529-
let val = this.read_scalar(val)?.to_i32()? as u8;
529+
let val = this.read_scalar(val)?.to_i32()?;
530530
let num = this.read_scalar(num)?.to_machine_usize(this)?;
531+
// The docs say val is "interpreted as unsigned char".
532+
#[allow(clippy::cast_sign_loss)]
533+
let val = val as u8;
534+
531535
if let Some(idx) = this
532536
.read_bytes_ptr(ptr, Size::from_bytes(num))?
533537
.iter()
@@ -543,8 +547,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
543547
"memchr" => {
544548
let [ptr, val, num] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
545549
let ptr = this.read_pointer(ptr)?;
546-
let val = this.read_scalar(val)?.to_i32()? as u8;
550+
let val = this.read_scalar(val)?.to_i32()?;
547551
let num = this.read_scalar(num)?.to_machine_usize(this)?;
552+
// The docs say val is "interpreted as unsigned char".
553+
#[allow(clippy::cast_sign_loss)]
554+
let val = val as u8;
555+
548556
let idx = this
549557
.read_bytes_ptr(ptr, Size::from_bytes(num))?
550558
.iter()

src/shims/unix/fs.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
776776

777777
// We cap the number of read bytes to the largest value that we are able to fit in both the
778778
// host's and target's `isize`. This saves us from having to handle overflows later.
779-
let count = count.min(this.machine_isize_max() as u64).min(isize::MAX as u64);
779+
let count = count
780+
.min(u64::try_from(this.machine_isize_max()).unwrap())
781+
.min(u64::try_from(isize::MAX).unwrap());
780782
let communicate = this.machine.communicate();
781783

782784
if let Some(file_descriptor) = this.machine.file_handler.handles.get_mut(&fd) {
@@ -827,7 +829,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
827829

828830
// We cap the number of written bytes to the largest value that we are able to fit in both the
829831
// host's and target's `isize`. This saves us from having to handle overflows later.
830-
let count = count.min(this.machine_isize_max() as u64).min(isize::MAX as u64);
832+
let count = count
833+
.min(u64::try_from(this.machine_isize_max()).unwrap())
834+
.min(u64::try_from(isize::MAX).unwrap());
831835
let communicate = this.machine.communicate();
832836

833837
if let Some(file_descriptor) = this.machine.file_handler.handles.get(&fd) {

src/shims/windows/dlsym.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
102102
// Return whether this was a success. >= 0 is success.
103103
// For the error code we arbitrarily pick 0xC0000185, STATUS_IO_DEVICE_ERROR.
104104
this.write_scalar(
105-
Scalar::from_i32(if written.is_some() { 0 } else { 0xC0000185u32 as i32 }),
105+
Scalar::from_u32(if written.is_some() { 0 } else { 0xC0000185u32 }),
106106
dest,
107107
)?;
108108
}

0 commit comments

Comments
 (0)