8

Add `unnecessary_to_owned` lint by smoelius · Pull Request #7978 · rust-lang/rus...

 2 years ago
source link: https://github.com/rust-lang/rust-clippy/pull/7978
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Copy link

Contributor

smoelius commented on Nov 16, 2021

This PR adds a lint to check for unnecessary calls to ToOwned::to_owned and other similar functions (e.g., Cow::into_owned, ToString::to_string, etc.).

The lint checks for expressions of the form &receiver.to_owned_like() used in a position requiring type &T where one of the following is true:

  • receiver's type is T exactly
  • receiver's type implements Deref<Target = T>
  • receiver's type implements AsRef<T>

The lint additionally checks for expressions of the form receiver.to_owned_like() used as arguments of type impl AsRef<T>.

It would be nice if the lint could also check for expressions used as arguments to functions like the following:

fn foo<T: AsRef<str>>(x: T) { ... }

However, I couldn't figure out how to determine whether a function input type was instantiated from a parameter with a trait bound.

If someone could offer me some guidance, I would be happy to add such functionality.

Closes #7933

changelog: Add [unnecessary_to_owned] lint


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK