Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lint for possibly incorrect type_id usage in reflection #182

Open
MrGVSV opened this issue Nov 22, 2024 · 0 comments
Open

Add lint for possibly incorrect type_id usage in reflection #182

MrGVSV opened this issue Nov 22, 2024 · 0 comments
Labels
A-Linter Related to the linter and custom lints C-Feature Make something new possible

Comments

@MrGVSV
Copy link
Contributor

MrGVSV commented Nov 22, 2024

Problem

Reflection relies on TypeId for a lot of things. And to get this TypeId it seems sensible to do something like value.type_id(). However, in a majority of cases within reflection code, this is not what you want.

A lot of times, value isn't a concrete type, but a type-erased dyn Reflect trait object. This means the TypeId returned by value.type_id() is actually the TypeId of the trait object, not the underlying type.

Instead, what you really want is to get the TypeId from the associated TypeInfo. This can be done either by calling and unwrapping value.get_represented_type_info() or, in 0.15, calling value.reflect_type_info() (assuming value is a dyn Reflect and not a dyn PartialReflect). Then with the TypeInfo you can call info.type_id().

In other words, this is wrong:

let value: Box<dyn Reflect> = Box::new(123u32);
let type_id = value.type_id();
assert_eq!(type_id, TypeId::of::<u32>());

This is right:

let value: Box<dyn Reflect> = Box::new(123u32);
let type_id = value.get_represented_type_info().unwrap().type_id();
assert_eq!(type_id, TypeId::of::<u32>());

Solution

We should add a lint to warn against possibly incorrect usages of std::any::Any::type_id when the type of the receiver is dyn Reflect (or dyn PartialReflect starting in 0.15).

It should point out the potential footgun with using std::any::Any::type_id and point the user to an appropriate alternative: Reflect::get_represented_type_info (or Reflect::reflect_type_info/PartialReflect::get_represented_type_info for 0.15).

Possible name: reflect_object_type_id?

Possible group: SUSPICIOUS

@BD103 BD103 added A-Linter Related to the linter and custom lints C-Feature Make something new possible labels Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Linter Related to the linter and custom lints C-Feature Make something new possible
Projects
Status: Todo
Development

No branches or pull requests

2 participants