-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implemented suspicious_to_owned
lint to check if to_owned
is called on a Cow
#8984
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @llogiq (or someone else) soon. Please see the contribution instructions for more information. |
Converted to draft to fix the test issue (that for some reason doesn't manifest locally :( ) |
e517864
to
d7f547e
Compare
CI is happy now, marking ready for review. |
to_owned
is called on a Cow
suspicious_to_owned
lint to check if to_owned
is called on a Cow
This looks OK but shouldn't redundant_clone warn about this? See also #2387. |
Here are the warnings from running this on something like the 1000 top crates.
It looks like that for most of them the advice of using |
Thanks, running this on the top 1000 crates makes sense. Quick-xml is certainly conscious of the caveats and in some way perpetrates the same Vpckg use of That said, I can see how these usages (especially the choice made in quick-xml, even if I honestly disagree with it) make a strong argument against this lint. The reason against relying on The issue I found was when using
The above is UB and very prone to all kinds of crash with This the reason why I was suggesting it for a more specific lint with a higher warning level, but I can see the reasons against this argument so feel free to close the PR in that case. |
☔ The latest upstream changes (presumably #9099) made this pull request unmergeable. Please resolve the merge conflicts. |
Should the suggestion not be to suggest using
|
You're probably right that suggesting to use (I'll also rebase the changes on top of the latest master head -- sorry for the delay, but was away) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I was absent for a while. I think the lint docs could use some minor rewording, otherwise I think we can merge this after a rebase.
d7f547e
to
7b79b88
Compare
I rebased and changed the wording so that it's less opinionated between Let me know if it's fine or other changes to the wording are required. |
☔ The latest upstream changes (presumably #9379) made this pull request unmergeable. Please resolve the merge conflicts. |
Thank you! r=me with another rebase. |
7b79b88
to
15031f0
Compare
Rebased on the latest master but forgot to update_lints 🤷♂️ Fixed the mistake and exercising CI again. |
… on a `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. This creates confusion (stemming from the ambiguity of the 'owned' term in the context of `Cow`s) and might not be what the writer intended.
15031f0
to
de028e2
Compare
Thank you! @bors r+ |
Implemented `suspicious_to_owned` lint to check if `to_owned` is called on a `Cow` changelog: ``[`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`
💔 Test failed - checks-action_test |
Not sure about the merge failure here. I'd be happy to help this landed but... I need a clue :) (*) I did the rebase locally and didn't push so not to break the review validity, but can push it if it helps of course. |
The @bors retry |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Hello, I don't understand the premise. Can you help me understand? If I am currently borrowing a |
|
The 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 onCow<'_, _>
. This is done becauseto_owned
is very similarly named tointo_owned
, but the effect of calling those two methods is completely different (one makes theCow::Borrowed
into aCow::Owned
, the other just clones theCow
). 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 aCow
other than confusing people reading the code: eitherinto_owned
orclone
should be called.Note that this overlaps perfectly with
implicit_clone
as a warning, butimplicit_clone
is classified pedantic (while the consequences forCow
might be of a wider blast radius than just pedantry); given the overlap, I set-up the lint so that ifsuspicious_to_owned
triggersimplicit_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
.stderr
file)cargo test
passes locallycargo dev update_lints
cargo dev fmt