Skip to content
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

TooOwned #13697

Closed
kornelski opened this issue Nov 17, 2024 · 1 comment · Fixed by #13948
Closed

TooOwned #13697

kornelski opened this issue Nov 17, 2024 · 1 comment · Fixed by #13948
Assignees
Labels
A-lint Area: New lints

Comments

@kornelski
Copy link
Contributor

kornelski commented Nov 17, 2024

What it does

Warn against use of Cow<String> or Cow<Vec<u8>>, instead of Cow<str> or Cow<[u8]>.

[u8] is an unusual type, and it's not obvious that Cow<[u8]> can hold a Vec<u8>, so users can mistakenly use Cow<Vec<u8>> instead. However, Cow<Vec<u8>> wasn't meant to be used. It creates an extra level of indirection, and Cow::Borrowed is limited to holding a less-general &Vec instead of a slice. Both Cow<[u8]> and Cow<Vec<u8>> have the same size, so the other form doesn't even benefit from holding a thin pointer.

Same for CStr, OsStr, Path. From implementation perspective, I think it could warn when Cow<X> is used, and there exists Cow<Y, Owned = X>, and X != Y.

Advantage

It helps users discover the intended types for use in Cow, where the type argument is for a non-owned type, not the owned one.

Drawbacks

None?

Example

Cow<'_, Vec<u8>>

Could be written as:

Cow<'_, [u8]>
@kornelski kornelski added the A-lint Area: New lints label Nov 17, 2024
@llogiq
Copy link
Contributor

llogiq commented Jan 3, 2025

@rustbot claim

github-merge-queue bot pushed a commit that referenced this issue Feb 20, 2025
Closes #13697.

---

changelog: add [`owned_cow`] lint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants