about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authory21 <30553356+y21@users.noreply.github.com>2023-06-19 00:36:16 +0200
committery21 <30553356+y21@users.noreply.github.com>2023-06-19 00:36:16 +0200
commitb0dfecd8c13aaf51946ce43db8a6ed398b9a5bf5 (patch)
tree18c673a70e168757b783801a0b17e575630a7f4d /tests
parent26ac76c15f4a12b2debde692dfff02a08d516f6e (diff)
downloadrust-b0dfecd8c13aaf51946ce43db8a6ed398b9a5bf5.tar.gz
rust-b0dfecd8c13aaf51946ce43db8a6ed398b9a5bf5.zip
add a few more test cases
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/type_id_on_box.fixed26
-rw-r--r--tests/ui/type_id_on_box.rs26
-rw-r--r--tests/ui/type_id_on_box.stderr19
3 files changed, 61 insertions, 10 deletions
diff --git a/tests/ui/type_id_on_box.fixed b/tests/ui/type_id_on_box.fixed
index fc177afacd4..615d809c897 100644
--- a/tests/ui/type_id_on_box.fixed
+++ b/tests/ui/type_id_on_box.fixed
@@ -3,6 +3,19 @@
 #![warn(clippy::type_id_on_box)]
 
 use std::any::{Any, TypeId};
+use std::ops::Deref;
+
+type SomeBox = Box<dyn Any>;
+
+struct BadBox(Box<dyn Any>);
+
+impl Deref for BadBox {
+    type Target = Box<dyn Any>;
+
+    fn deref(&self) -> &Self::Target {
+        &self.0
+    }
+}
 
 fn existential() -> impl Any {
     Box::new(1) as Box<dyn Any>
@@ -11,10 +24,17 @@ fn existential() -> impl Any {
 fn main() {
     let any_box: Box<dyn Any> = Box::new(0usize);
     let _ = (*any_box).type_id();
-    let _ = TypeId::of::<Box<dyn Any>>(); // don't lint, user probably explicitly wants to do this
+    let _ = TypeId::of::<Box<dyn Any>>(); // Don't lint. We explicitly say "do this instead" if this is intentional
     let _ = (*any_box).type_id();
     let any_box: &Box<dyn Any> = &(Box::new(0usize) as Box<dyn Any>);
-    let _ = (**any_box).type_id(); // 2 derefs are needed here
+    let _ = (**any_box).type_id(); // 2 derefs are needed here to get to the `dyn Any`
+
     let b = existential();
-    let _ = b.type_id(); // don't lint
+    let _ = b.type_id(); // Don't lint.
+
+    let b: SomeBox = Box::new(0usize);
+    let _ = (*b).type_id();
+
+    let b = BadBox(Box::new(0usize));
+    let _ = b.type_id(); // Don't lint. This is a call to `<BadBox as Any>::type_id`. Not `std::boxed::Box`!
 }
diff --git a/tests/ui/type_id_on_box.rs b/tests/ui/type_id_on_box.rs
index 36cf631a092..74b6c74ae5f 100644
--- a/tests/ui/type_id_on_box.rs
+++ b/tests/ui/type_id_on_box.rs
@@ -3,6 +3,19 @@
 #![warn(clippy::type_id_on_box)]
 
 use std::any::{Any, TypeId};
+use std::ops::Deref;
+
+type SomeBox = Box<dyn Any>;
+
+struct BadBox(Box<dyn Any>);
+
+impl Deref for BadBox {
+    type Target = Box<dyn Any>;
+
+    fn deref(&self) -> &Self::Target {
+        &self.0
+    }
+}
 
 fn existential() -> impl Any {
     Box::new(1) as Box<dyn Any>
@@ -11,10 +24,17 @@ fn existential() -> impl Any {
 fn main() {
     let any_box: Box<dyn Any> = Box::new(0usize);
     let _ = any_box.type_id();
-    let _ = TypeId::of::<Box<dyn Any>>(); // don't lint, user probably explicitly wants to do this
+    let _ = TypeId::of::<Box<dyn Any>>(); // Don't lint. We explicitly say "do this instead" if this is intentional
     let _ = (*any_box).type_id();
     let any_box: &Box<dyn Any> = &(Box::new(0usize) as Box<dyn Any>);
-    let _ = any_box.type_id(); // 2 derefs are needed here
+    let _ = any_box.type_id(); // 2 derefs are needed here to get to the `dyn Any`
+
     let b = existential();
-    let _ = b.type_id(); // don't lint
+    let _ = b.type_id(); // Don't lint.
+
+    let b: SomeBox = Box::new(0usize);
+    let _ = b.type_id();
+
+    let b = BadBox(Box::new(0usize));
+    let _ = b.type_id(); // Don't lint. This is a call to `<BadBox as Any>::type_id`. Not `std::boxed::Box`!
 }
diff --git a/tests/ui/type_id_on_box.stderr b/tests/ui/type_id_on_box.stderr
index 47e0e814daf..1525328c0d0 100644
--- a/tests/ui/type_id_on_box.stderr
+++ b/tests/ui/type_id_on_box.stderr
@@ -1,5 +1,5 @@
 error: calling `.type_id()` on a `Box<dyn Any>`
-  --> $DIR/type_id_on_box.rs:13:13
+  --> $DIR/type_id_on_box.rs:26:13
    |
 LL |     let _ = any_box.type_id();
    |             -------^^^^^^^^^^
@@ -11,9 +11,9 @@ LL |     let _ = any_box.type_id();
    = note: `-D clippy::type-id-on-box` implied by `-D warnings`
 
 error: calling `.type_id()` on a `Box<dyn Any>`
-  --> $DIR/type_id_on_box.rs:17:13
+  --> $DIR/type_id_on_box.rs:30:13
    |
-LL |     let _ = any_box.type_id(); // 2 derefs are needed here
+LL |     let _ = any_box.type_id(); // 2 derefs are needed here to get to the `dyn Any`
    |             -------^^^^^^^^^^
    |             |
    |             help: consider dereferencing first: `(**any_box)`
@@ -21,5 +21,16 @@ LL |     let _ = any_box.type_id(); // 2 derefs are needed here
    = note: this returns the type id of the literal type `Box<dyn Any>` instead of the type id of the boxed value, which is most likely not what you want
    = note: if this is intentional, use `TypeId::of::<Box<dyn Any>>()` instead, which makes it more clear
 
-error: aborting due to 2 previous errors
+error: calling `.type_id()` on a `Box<dyn Any>`
+  --> $DIR/type_id_on_box.rs:36:13
+   |
+LL |     let _ = b.type_id();
+   |             -^^^^^^^^^^
+   |             |
+   |             help: consider dereferencing first: `(*b)`
+   |
+   = note: this returns the type id of the literal type `Box<dyn Any>` instead of the type id of the boxed value, which is most likely not what you want
+   = note: if this is intentional, use `TypeId::of::<Box<dyn Any>>()` instead, which makes it more clear
+
+error: aborting due to 3 previous errors