-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Warn for passing Box<&dyn Any>
to function taking &dyn std::any::Any
#12800
Comments
&dyn std::any::Any
&dyn std::any::Any
as a parameter
I don't see an inherent issue with this general pattern. Functions are natural abstractions for splitting up code and sometimes you just have to deal with That looks like a perfectly fine thing to do and it seems odd to me that clippy should blanket-warn all such functions.
This OTOH makes more sense as a lint to me I think -- passing |
Yeah I think making it more specific is better, that's a very valid reason to lint these but the general pattern is okay |
Makes a lot of sense to me, lint for passing |
&dyn std::any::Any
as a parameterBox<&dyn Any>
to function taking &dyn std::any::Any
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: