about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorLukas Lueg <lukas.lueg@gmail.com>2022-12-17 14:16:45 +0100
committerLukas Lueg <lukas.lueg@gmail.com>2022-12-24 23:39:54 +0100
commitd7b9e195c2df9e576abd2eba54f3fe6516619054 (patch)
tree752a4a4772635238132d3a2bc58960ed6f01d04b /tests
parent4a09068f878334c73a5a1fbfc455931593f1564e (diff)
downloadrust-d7b9e195c2df9e576abd2eba54f3fe6516619054.tar.gz
rust-d7b9e195c2df9e576abd2eba54f3fe6516619054.zip
Add size_of_ref lint
Fixes #9995
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/size_of_ref.rs27
-rw-r--r--tests/ui/size_of_ref.stderr27
2 files changed, 54 insertions, 0 deletions
diff --git a/tests/ui/size_of_ref.rs b/tests/ui/size_of_ref.rs
new file mode 100644
index 00000000000..1e83ab82907
--- /dev/null
+++ b/tests/ui/size_of_ref.rs
@@ -0,0 +1,27 @@
+#![allow(unused)]
+#![warn(clippy::size_of_ref)]
+
+use std::mem::size_of_val;
+
+fn main() {
+    let x = 5;
+    let y = &x;
+
+    size_of_val(&x); // no lint
+    size_of_val(y); // no lint
+
+    size_of_val(&&x);
+    size_of_val(&y);
+}
+
+struct S {
+    field: u32,
+    data: Vec<u8>,
+}
+
+impl S {
+    /// Get size of object including `self`, in bytes.
+    pub fn size(&self) -> usize {
+        std::mem::size_of_val(&self) + (std::mem::size_of::<u8>() * self.data.capacity())
+    }
+}
diff --git a/tests/ui/size_of_ref.stderr b/tests/ui/size_of_ref.stderr
new file mode 100644
index 00000000000..d4c13ac3290
--- /dev/null
+++ b/tests/ui/size_of_ref.stderr
@@ -0,0 +1,27 @@
+error: argument to `std::mem::size_of_val()` is a reference to a reference
+  --> $DIR/size_of_ref.rs:13:5
+   |
+LL |     size_of_val(&&x);
+   |     ^^^^^^^^^^^^^^^^
+   |
+   = help: dereference the argument to `std::mem::size_of_val()` to get the size of the value instead of the size of the reference-type
+   = note: `-D clippy::size-of-ref` implied by `-D warnings`
+
+error: argument to `std::mem::size_of_val()` is a reference to a reference
+  --> $DIR/size_of_ref.rs:14:5
+   |
+LL |     size_of_val(&y);
+   |     ^^^^^^^^^^^^^^^
+   |
+   = help: dereference the argument to `std::mem::size_of_val()` to get the size of the value instead of the size of the reference-type
+
+error: argument to `std::mem::size_of_val()` is a reference to a reference
+  --> $DIR/size_of_ref.rs:25:9
+   |
+LL |         std::mem::size_of_val(&self) + (std::mem::size_of::<u8>() * self.data.capacity())
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = help: dereference the argument to `std::mem::size_of_val()` to get the size of the value instead of the size of the reference-type
+
+error: aborting due to 3 previous errors
+