Fix size computation when element is a slice
In this case, a dereference is needed before using `mem::size_of_val()`.
This commit is contained in:
parent
40bead02a5
commit
4ec219393a
4 changed files with 38 additions and 2 deletions
|
|
@ -49,7 +49,11 @@ impl<'tcx> LateLintPass<'tcx> for ManualSliceSizeCalculation {
|
|||
{
|
||||
let ctxt = expr.span.ctxt();
|
||||
let mut app = Applicability::MachineApplicable;
|
||||
let deref = "*".repeat(refs_count - 1);
|
||||
let deref = if refs_count > 0 {
|
||||
"*".repeat(refs_count - 1)
|
||||
} else {
|
||||
"&".into()
|
||||
};
|
||||
let val_name = snippet_with_context(cx, receiver.span, ctxt, "slice", &mut app).0;
|
||||
let Some(sugg) = std_or_core(cx) else { return };
|
||||
|
||||
|
|
|
|||
|
|
@ -64,3 +64,16 @@ const fn _const(s_i32: &[i32]) {
|
|||
// True negative:
|
||||
let _ = s_i32.len() * size_of::<i32>(); // Ok, can't use size_of_val in const
|
||||
}
|
||||
|
||||
fn issue_14802() {
|
||||
struct IcedSlice {
|
||||
dst: [u8],
|
||||
}
|
||||
|
||||
impl IcedSlice {
|
||||
fn get_len(&self) -> usize {
|
||||
std::mem::size_of_val(&self.dst)
|
||||
//~^ manual_slice_size_calculation
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,3 +64,16 @@ const fn _const(s_i32: &[i32]) {
|
|||
// True negative:
|
||||
let _ = s_i32.len() * size_of::<i32>(); // Ok, can't use size_of_val in const
|
||||
}
|
||||
|
||||
fn issue_14802() {
|
||||
struct IcedSlice {
|
||||
dst: [u8],
|
||||
}
|
||||
|
||||
impl IcedSlice {
|
||||
fn get_len(&self) -> usize {
|
||||
self.dst.len() * size_of::<u8>()
|
||||
//~^ manual_slice_size_calculation
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,5 +55,11 @@ error: manual slice size calculation
|
|||
LL | let _ = external!(&[1u64][..]).len() * size_of::<u64>();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(external!(&[1u64][..]))`
|
||||
|
||||
error: aborting due to 9 previous errors
|
||||
error: manual slice size calculation
|
||||
--> tests/ui/manual_slice_size_calculation.rs:75:13
|
||||
|
|
||||
LL | self.dst.len() * size_of::<u8>()
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::mem::size_of_val(&self.dst)`
|
||||
|
||||
error: aborting due to 10 previous errors
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue