From b0dfecd8c13aaf51946ce43db8a6ed398b9a5bf5 Mon Sep 17 00:00:00 2001 From: y21 <30553356+y21@users.noreply.github.com> Date: Mon, 19 Jun 2023 00:36:16 +0200 Subject: [PATCH] add a few more test cases --- tests/ui/type_id_on_box.fixed | 26 +++++++++++++++++++++++--- tests/ui/type_id_on_box.rs | 26 +++++++++++++++++++++++--- tests/ui/type_id_on_box.stderr | 19 +++++++++++++++---- 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 fc177afac..615d809c8 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; + +struct BadBox(Box); + +impl Deref for BadBox { + type Target = Box; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} fn existential() -> impl Any { Box::new(1) as Box @@ -11,10 +24,17 @@ fn existential() -> impl Any { fn main() { let any_box: Box = Box::new(0usize); let _ = (*any_box).type_id(); - let _ = TypeId::of::>(); // don't lint, user probably explicitly wants to do this + let _ = TypeId::of::>(); // Don't lint. We explicitly say "do this instead" if this is intentional let _ = (*any_box).type_id(); let any_box: &Box = &(Box::new(0usize) as Box); - 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 `::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 36cf631a0..74b6c74ae 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; + +struct BadBox(Box); + +impl Deref for BadBox { + type Target = Box; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} fn existential() -> impl Any { Box::new(1) as Box @@ -11,10 +24,17 @@ fn existential() -> impl Any { fn main() { let any_box: Box = Box::new(0usize); let _ = any_box.type_id(); - let _ = TypeId::of::>(); // don't lint, user probably explicitly wants to do this + let _ = TypeId::of::>(); // Don't lint. We explicitly say "do this instead" if this is intentional let _ = (*any_box).type_id(); let any_box: &Box = &(Box::new(0usize) as Box); - 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 `::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 47e0e814d..1525328c0 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` - --> $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` - --> $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` 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::>()` instead, which makes it more clear -error: aborting due to 2 previous errors +error: calling `.type_id()` on a `Box` + --> $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` 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::>()` instead, which makes it more clear + +error: aborting due to 3 previous errors