Skip to content

New lint against indirect method calls #11193

Open
@not-my-profile

Description

@not-my-profile

What it does

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> const Into<U> for T
where
    U: ~const From<T>,
{
    /// Calls `U::from(self)`.
    ///
    /// That is, this conversion is whatever the implementation of
    /// <code>[From]&lt;T&gt; for U</code> chooses to do.
    #[inline]
    fn into(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);

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions