You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What is the advantage of the recommended code over the original code
The original code will produce a dangling pointer on a Cow::Owned if created as a function argument. This is the same situation as for CString which will lead to a use-after-free.
Drawbacks
None.
Example
use std::borrow::Cow;fntake_ptr(_p:*consti32){}fnmain(){take_ptr(Cow::<[i32]>::Owned(vec![0]).as_ptr());take_ptr(Cow::<[i32]>::Borrowed(&[1]).as_ptr());}
Could be written as:
use std::borrow::Cow;fntake_ptr(_p:*consti32){}fnmain(){let c = Cow::<[i32]>::Owned(vec![0]);take_ptr(c.as_ptr());let c = Cow::<[i32]>::Borrowed(&[1]);take_ptr(c.as_ptr());}
Edit: I see that
take_ptr(vec![2])
also does not have a lint and exposes the same problem
The text was updated successfully, but these errors were encountered:
What it does
Checks for temporary
Cow
immediately dereferencedCategories (optional)
clippy::correctness
What is the advantage of the recommended code over the original code
The original code will produce a dangling pointer on a
Cow::Owned
if created as a function argument. This is the same situation as forCString
which will lead to a use-after-free.Drawbacks
None.
Example
Could be written as:
Edit: I see that
also does not have a lint and exposes the same problem
The text was updated successfully, but these errors were encountered: