Skip to content

Commit 7f60348

Browse files
committed
pass clippy::cast_possible_truncation
1 parent b67a6ff commit 7f60348

File tree

7 files changed

+19
-14
lines changed

7 files changed

+19
-14
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
clippy::cast_possible_wrap, // unsigned -> signed
3030
clippy::cast_sign_loss, // signed -> unsigned
3131
clippy::cast_lossless,
32+
clippy::cast_possible_truncation,
3233
)]
3334

3435
extern crate rustc_apfloat;

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/foreign_items.rs

Lines changed: 8 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
}
@@ -529,7 +533,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
529533
let val = this.read_scalar(val)?.to_i32()?;
530534
let num = this.read_scalar(num)?.to_machine_usize(this)?;
531535
// The docs say val is "interpreted as unsigned char".
532-
#[allow(clippy::cast_sign_loss)]
536+
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
533537
let val = val as u8;
534538

535539
if let Some(idx) = this
@@ -550,7 +554,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
550554
let val = this.read_scalar(val)?.to_i32()?;
551555
let num = this.read_scalar(num)?.to_machine_usize(this)?;
552556
// The docs say val is "interpreted as unsigned char".
553-
#[allow(clippy::cast_sign_loss)]
557+
#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
554558
let val = val as u8;
555559

556560
let idx = this

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/unix/fs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
785785
trace!("read: FD mapped to {:?}", file_descriptor);
786786
// We want to read at most `count` bytes. We are sure that `count` is not negative
787787
// because it was a target's `usize`. Also we are sure that its smaller than
788-
// `usize::MAX` because it is a host's `isize`.
789-
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()];
790790
// `File::read` never returns a value larger than `count`,
791791
// so this cannot fail.
792792
let result =

src/shims/windows/dlsym.rs

Lines changed: 2 additions & 1 deletion
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"

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;

0 commit comments

Comments
 (0)