Skip to content

Commit 6227e1e

Browse files
committed
Auto merge of #2422 - RalfJung:integers, r=oli-obk
enable clippy lints against integer casts Cc #1236
2 parents 3d237be + 7f60348 commit 6227e1e

File tree

11 files changed

+46
-22
lines changed

11 files changed

+46
-22
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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#![feature(is_some_with)]
1010
#![feature(nonzero_ops)]
1111
#![feature(local_key_cell_methods)]
12-
#![warn(rust_2018_idioms)]
12+
// Configure clippy and other lints
1313
#![allow(
1414
clippy::collapsible_else_if,
1515
clippy::collapsible_if,
@@ -24,6 +24,13 @@
2424
clippy::derive_hash_xor_eq,
2525
clippy::too_many_arguments
2626
)]
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+
clippy::cast_possible_truncation,
33+
)]
2734

2835
extern crate rustc_apfloat;
2936
extern crate rustc_ast;

src/shims/backtrace.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
171171
);
172172
}
173173

174-
let lineno: u32 = lo.line as u32;
174+
// `u32` is not enough to fit line/colno, which can be `usize`. It seems unlikely that a
175+
// file would have more than 2^32 lines or columns, but whatever, just default to 0.
176+
let lineno: u32 = u32::try_from(lo.line).unwrap_or(0);
175177
// `lo.col` is 0-based - add 1 to make it 1-based for the caller.
176-
let colno: u32 = lo.col.0 as u32 + 1;
178+
let colno: u32 = u32::try_from(lo.col.0 + 1).unwrap_or(0);
177179

178180
let dest = this.force_allocation(dest)?;
179181
if let ty::Adt(adt, _) = dest.layout.ty.kind() {

src/shims/env.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
202202
let env_block_ptr = this.read_pointer(env_block_op)?;
203203
let result = this.deallocate_ptr(env_block_ptr, None, MiriMemoryKind::Runtime.into());
204204
// If the function succeeds, the return value is nonzero.
205-
Ok(result.is_ok() as i32)
205+
Ok(i32::from(result.is_ok()))
206206
}
207207

208208
fn setenv(
@@ -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: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
8282
let align = this.min_align(size, kind);
8383
let ptr = this.allocate_ptr(Size::from_bytes(size), align, kind.into())?;
8484
if zero_init {
85-
// We just allocated this, the access is definitely in-bounds.
86-
this.write_bytes_ptr(ptr.into(), iter::repeat(0u8).take(size as usize)).unwrap();
85+
// We just allocated this, the access is definitely in-bounds and fits into our address space.
86+
this.write_bytes_ptr(
87+
ptr.into(),
88+
iter::repeat(0u8).take(usize::try_from(size).unwrap()),
89+
)
90+
.unwrap();
8791
}
8892
Ok(ptr.into())
8993
}
@@ -526,8 +530,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
526530
"memrchr" => {
527531
let [ptr, val, num] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
528532
let ptr = this.read_pointer(ptr)?;
529-
let val = this.read_scalar(val)?.to_i32()? as u8;
533+
let val = this.read_scalar(val)?.to_i32()?;
530534
let num = this.read_scalar(num)?.to_machine_usize(this)?;
535+
// The docs say val is "interpreted as unsigned char".
536+
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
537+
let val = val as u8;
538+
531539
if let Some(idx) = this
532540
.read_bytes_ptr(ptr, Size::from_bytes(num))?
533541
.iter()
@@ -543,8 +551,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
543551
"memchr" => {
544552
let [ptr, val, num] = this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
545553
let ptr = this.read_pointer(ptr)?;
546-
let val = this.read_scalar(val)?.to_i32()? as u8;
554+
let val = this.read_scalar(val)?.to_i32()?;
547555
let num = this.read_scalar(num)?.to_machine_usize(this)?;
556+
// The docs say val is "interpreted as unsigned char".
557+
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
558+
let val = val as u8;
559+
548560
let idx = this
549561
.read_bytes_ptr(ptr, Size::from_bytes(num))?
550562
.iter()

src/shims/intrinsics/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
117117
let byte_count = ty_layout.size.checked_mul(count, this).ok_or_else(|| {
118118
err_ub_format!("overflow computing total size of `{intrinsic_name}`")
119119
})?;
120-
this.write_bytes_ptr(
121-
ptr,
122-
iter::repeat(val_byte).take(byte_count.bytes() as usize),
123-
)?;
120+
this.write_bytes_ptr(ptr, iter::repeat(val_byte).take(byte_count.bytes_usize()))?;
124121
}
125122

126123
// Floating-point operations

src/shims/tls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl<'tcx> TlsData<'tcx> {
7373
self.keys.try_insert(new_key, TlsEntry { data: Default::default(), dtor }).unwrap();
7474
trace!("New TLS key allocated: {} with dtor {:?}", new_key, dtor);
7575

76-
if max_size.bits() < 128 && new_key >= (1u128 << max_size.bits() as u128) {
76+
if max_size.bits() < 128 && new_key >= (1u128 << max_size.bits()) {
7777
throw_unsup_format!("we ran out of TLS key space");
7878
}
7979
Ok(new_key)

src/shims/unix/fs.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -776,15 +776,17 @@ 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) {
783785
trace!("read: FD mapped to {:?}", file_descriptor);
784786
// We want to read at most `count` bytes. We are sure that `count` is not negative
785787
// because it was a target's `usize`. Also we are sure that its smaller than
786-
// `usize::MAX` because it is a host's `isize`.
787-
let mut bytes = vec![0; count as usize];
788+
// `usize::MAX` because it is bounded by the host's `isize`.
789+
let mut bytes = vec![0; usize::try_from(count).unwrap()];
788790
// `File::read` never returns a value larger than `count`,
789791
// so this cannot fail.
790792
let result =
@@ -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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
8484
} else {
8585
io::stderr().write(buf_cont)
8686
};
87-
res.ok().map(|n| n as u32)
87+
// We write at most `n` bytes, which is a `u32`, so we cannot have written more than that.
88+
res.ok().map(|n| u32::try_from(n).unwrap())
8889
} else {
8990
throw_unsup_format!(
9091
"on Windows, writing to anything except stdout/stderr is not supported"
@@ -102,7 +103,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
102103
// Return whether this was a success. >= 0 is success.
103104
// For the error code we arbitrarily pick 0xC0000185, STATUS_IO_DEVICE_ERROR.
104105
this.write_scalar(
105-
Scalar::from_i32(if written.is_some() { 0 } else { 0xC0000185u32 as i32 }),
106+
Scalar::from_u32(if written.is_some() { 0 } else { 0xC0000185u32 }),
106107
dest,
107108
)?;
108109
}

src/shims/windows/foreign_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
116116
// Initialize with `0`.
117117
this.write_bytes_ptr(
118118
system_info.ptr,
119-
iter::repeat(0u8).take(system_info.layout.size.bytes() as usize),
119+
iter::repeat(0u8).take(system_info.layout.size.bytes_usize()),
120120
)?;
121121
// Set selected fields.
122122
let word_layout = this.machine.layouts.u16;

src/stacked_borrows/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl Item {
2222
assert!(tag.0.get() <= TAG_MASK);
2323
let packed_tag = tag.0.get();
2424
let packed_perm = perm.to_bits() << PERM_SHIFT;
25-
let packed_protected = (protected as u64) << PROTECTED_SHIFT;
25+
let packed_protected = u64::from(protected) << PROTECTED_SHIFT;
2626

2727
let new = Self(packed_tag | packed_perm | packed_protected);
2828

0 commit comments

Comments
 (0)