pass clippy::cast_sign_loss and clippy::cast_possible_wrap

This commit is contained in:
Ralf Jung 2022-07-23 08:35:45 -04:00
parent 3f0fdf290f
commit b67a6ff099
6 changed files with 25 additions and 7 deletions

View File

@ -230,6 +230,7 @@ impl<'mir, 'tcx> GlobalStateInner {
// Wrapping "addr - base_addr"
let dl = ecx.data_layout();
#[allow(clippy::cast_possible_wrap)] // we want to wrap here
let neg_base_addr = (base_addr as i64).wrapping_neg();
Some((
alloc_id,

View File

@ -24,7 +24,12 @@
clippy::derive_hash_xor_eq,
clippy::too_many_arguments
)]
#![warn(rust_2018_idioms, clippy::cast_lossless)]
#![warn(
rust_2018_idioms,
clippy::cast_possible_wrap, // unsigned -> signed
clippy::cast_sign_loss, // signed -> unsigned
clippy::cast_lossless,
)]
extern crate rustc_apfloat;
extern crate rustc_ast;

View File

@ -459,7 +459,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
// The reason we need to do this wacky of a conversion is because
// `libc::getpid` returns an i32, however, `std::process::id()` return an u32.
// So we un-do the conversion that stdlib does and turn it back into an i32.
#[allow(clippy::cast_possible_wrap)]
Ok(std::process::id() as i32)
}

View File

@ -526,8 +526,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
"memrchr" => {
let [ptr, val, num] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let ptr = this.read_pointer(ptr)?;
let val = this.read_scalar(val)?.to_i32()? as u8;
let val = this.read_scalar(val)?.to_i32()?;
let num = this.read_scalar(num)?.to_machine_usize(this)?;
// The docs say val is "interpreted as unsigned char".
#[allow(clippy::cast_sign_loss)]
let val = val as u8;
if let Some(idx) = this
.read_bytes_ptr(ptr, Size::from_bytes(num))?
.iter()
@ -543,8 +547,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
"memchr" => {
let [ptr, val, num] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
let ptr = this.read_pointer(ptr)?;
let val = this.read_scalar(val)?.to_i32()? as u8;
let val = this.read_scalar(val)?.to_i32()?;
let num = this.read_scalar(num)?.to_machine_usize(this)?;
// The docs say val is "interpreted as unsigned char".
#[allow(clippy::cast_sign_loss)]
let val = val as u8;
let idx = this
.read_bytes_ptr(ptr, Size::from_bytes(num))?
.iter()

View File

@ -776,7 +776,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
// We cap the number of read bytes to the largest value that we are able to fit in both the
// host's and target's `isize`. This saves us from having to handle overflows later.
let count = count.min(this.machine_isize_max() as u64).min(isize::MAX as u64);
let count = count
.min(u64::try_from(this.machine_isize_max()).unwrap())
.min(u64::try_from(isize::MAX).unwrap());
let communicate = this.machine.communicate();
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
// We cap the number of written bytes to the largest value that we are able to fit in both the
// host's and target's `isize`. This saves us from having to handle overflows later.
let count = count.min(this.machine_isize_max() as u64).min(isize::MAX as u64);
let count = count
.min(u64::try_from(this.machine_isize_max()).unwrap())
.min(u64::try_from(isize::MAX).unwrap());
let communicate = this.machine.communicate();
if let Some(file_descriptor) = this.machine.file_handler.handles.get(&fd) {

View File

@ -102,7 +102,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
// Return whether this was a success. >= 0 is success.
// For the error code we arbitrarily pick 0xC0000185, STATUS_IO_DEVICE_ERROR.
this.write_scalar(
Scalar::from_i32(if written.is_some() { 0 } else { 0xC0000185u32 as i32 }),
Scalar::from_u32(if written.is_some() { 0 } else { 0xC0000185u32 }),
dest,
)?;
}