Warn for passing Box<&dyn Any>
to function taking &dyn std::any::Any
#12800
Labels
A-lint
Area: New lints
What it does
Issue a warning if a function takes any of
&dyn std::any::Any
&mut dyn std::any::Any
The recommendation is to either use an existing trait type or create a "marker trait" to specify what the method can actually process in order to avoid passing (by accident) object references of unexpected types.
EDIT: Only warn when
Box<&dyn Any>
is passed as-is. Recommendbox.as_ref()
Advantage
Beside the obvious advantages of a more concrete type, there's major pitfalls withSince&dyn Any
:&Box<Any>
itself implementsAny
, it can easily happen that one accidentally passes the box directly instead of the desiredbox.as_ref()
. This can be the cause of quite hard to debug issues.Drawbacks
While it's more often than not a shortcut for not defining a trait type, there are likely legitimate usecases for passing&dyn Any
(examples?)Example
Updated suggestion:
Could be written as:
Previous suggestion:
Could be written as:
The text was updated successfully, but these errors were encountered: