You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would flag method calls from the standard library that actually only call another method, e.g:
.into() -> use Type::from(..) instead
.try_into() -> use Type::try_from(..) instead
.parse() -> use Type::from_str(..) instead
etc. ... (there are probably more of these?)
Advantage
You can easily jump to the definition of the conversion code with rust-analyzer. Because if you go to definition of e.g. .into() with rust-analyzer it will just take you to the definition of the blanket implementation:
impl<T,U>constInto<U>forTwhereU: ~constFrom<T>,{/// Calls `U::from(self)`.////// That is, this conversion is whatever the implementation of/// <code>[From]<T> for U</code> chooses to do.#[inline]fninto(self) -> U{U::from(self)}}
which is technically correct but not very helpful since you're probably trying to get to the definition of the actual conversion code (ref rust-lang/rust-analyzer#15315) . Especially in large code bases it can be hard to manually find where a trait is implemented.
Drawbacks
It makes code more verbose and can easily lead to false positives since Clippy isn't type-aware (however implementing methods called into or try_into outside of the std traits is probably an anti-pattern anyway since it's confusing.)
Example
let a:Foo = b.into();
Could be written as:
let a:Foo = Foo::from(b);
The text was updated successfully, but these errors were encountered:
This should probably be a restriction lint if it's added. The advantage is definitely nice but it makes code a lot more verbose.
Clippy isn't type-aware
This isn't true, there are may queries clippy has access to that can determine if it's calling a method or a trait method; if that trait method is a diagnostic item, check if it's that, otherwise get the called method's path and check that
What it does
It would flag method calls from the standard library that actually only call another method, e.g:
.into()
-> useType::from(..)
instead.try_into()
-> useType::try_from(..)
instead.parse()
-> useType::from_str(..)
insteadAdvantage
You can easily jump to the definition of the conversion code with rust-analyzer. Because if you go to definition of e.g.
.into()
with rust-analyzer it will just take you to the definition of the blanket implementation:which is technically correct but not very helpful since you're probably trying to get to the definition of the actual conversion code (ref rust-lang/rust-analyzer#15315) . Especially in large code bases it can be hard to manually find where a trait is implemented.
Drawbacks
It makes code more verbose
and can easily lead to false positives since Clippy isn't type-aware (however implementing methods calledinto
ortry_into
outside of the std traits is probably an anti-pattern anyway since it's confusing.)Example
Could be written as:
The text was updated successfully, but these errors were encountered: