Add `unnecessary_to_owned` lint by smoelius · Pull Request #7978 · rust-lang/rus...
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.
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 isT
exactlyreceiver
's type implementsDeref<Target = T>
receiver
's type implementsAsRef<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
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK