]> git.lizzy.rs Git - rust.git/commit
Auto merge of #8984 - xanathar:pr/suspicious_to_owned, r=llogiq
authorbors <bors@rust-lang.org>
Sat, 27 Aug 2022 17:38:40 +0000 (17:38 +0000)
committerbors <bors@rust-lang.org>
Sat, 27 Aug 2022 17:38:40 +0000 (17:38 +0000)
commit2d4d8e16cd390a6a4fd2f249e2e15116f363f681
tree09cb736777a71ebd7aae33ccc642617123cbb0a9
parentbe8bd600009dd087f6c2cd4b354cf016b2072fd3
parentde028e2fb9d81d58d62dfc7bc8fd2335a9885641
Auto merge of #8984 - xanathar:pr/suspicious_to_owned, r=llogiq

Implemented `suspicious_to_owned` lint to check if `to_owned` is called on a `Cow`

changelog: Add lint ``[`suspicious_to_owned`]``

-----------------
Hi,
posting this unsolicited PR as I've been burned by this issue :)
Being unsolicited, feel free to reject it or reassign a different lint level etc.

This lint checks whether `to_owned` is called on `Cow<'_, _>`. This is done because `to_owned` is very similarly named to `into_owned`, but the effect of calling those two methods is completely different (one makes the `Cow::Borrowed` into a `Cow::Owned`, the other just clones the `Cow`). If the cow is then passed to code for which the type is not checked (e.g. generics, closures, etc.) it might slip through and if the cow data is coming from an unsafe context there is the potential for accidentally cause undefined behavior.
Even if not falling into this painful case, there's really no reason to call `to_owned` on a `Cow` other than confusing people reading the code: either `into_owned` or `clone` should be called.

Note that this overlaps perfectly with `implicit_clone` as a warning, but `implicit_clone` is classified pedantic (while the consequences for `Cow` might be of a wider blast radius than just pedantry); given the overlap, I set-up the lint so that if `suspicious_to_owned` triggers `implicit_clone` will not trigger. I'm not 100% sure this is done in the correct way (I tried to copy what other lints were doing) so please provide feedback on it if it isn't.

### Checklist

- \[x] Followed [lint naming conventions][lint_naming]
- \[x] Added passing UI tests (including committed `.stderr` file)
- \[x] `cargo test` passes locally
- \[x] Executed `cargo dev update_lints`
- \[x] Added lint documentation
- \[x] Run `cargo dev fmt`