RS.CLIPPY.SIZE_OF_REF
Argument to `size_of_val()` is a double-reference, which is almost certainly unintended
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: size_of_ref. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Checks for calls to size_of_val() where the argument is
a reference to a reference.
Why is this bad?
Calling size_of_val() with a reference to a reference as the argument
yields the size of the reference-type, not the size of the value behind
the reference.
Example
struct Foo {
buffer: [u8],
}
impl Foo {
fn size(&self) -> usize {
// Note that `&self` as an argument is a `&&Foo`: Because `self`
// is already a reference, `&self` is a double-reference.
// The return value of `size_of_val()` therefore is the
// size of the reference-type, not the size of `self`.
size_of_val(&self)
}
}
Use instead:
struct Foo {
buffer: [u8],
}
impl Foo {
fn size(&self) -> usize {
// Correct
size_of_val(self)
}
}