-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Lint for using the same expression for both length and capacity when constructing a vec/string #5955
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
Comments
Can I give this one a try? |
Sure, go ahead! |
Oh, I just saw that @sgrif offered themself to implement it. It would be better to hear from them first if they started to work on it already. |
Not a problem, I'll wait for them to reply. By the way, is there a bug in the repo currently? I just forked it and the build won't compile due to a dependency struct being private.
I have 187 cases of this in my terminal. |
Thanks for the heads-up, a fix is in progress in #6007. This happens when rustc breaks Clippy, we're working on trying to avoid this hassle to contributors :) After that PR passes the tests and is merged, the errors should be gone. |
@rustbot claim |
Hi 👋🏻, I'm an experienced software engineer but am new to Rust. I picked this as a first issue since it seems like it doesn't exactly need to be done in a rush 😋. I'll get to work in my spare time. |
Uh oh!
There was an error while loading. Please reload this page.
What it does
This lint checks for calls to
String::from_raw_parts
andVec::from_raw_parts
, and triggers when the same expression is used for both the length and capacity. If folks are interested in a pull request for this lint, I am happy to do the work.Categories (optional)
What is the advantage of the recommended code over the original code
When code is written this way, the author likely intended to use a borrowed type instead. The idea for this lint came after noticing a friend of mine consistently reaching for
String
and not&str
, which requires inventing a capacity. The author will likely use the length in that case. Unless the string/vec was created usingwith_capacity
or hadshrink_to_fit
called on it, it's unlikely that the length and capacity are the same.Drawbacks
This is trying to catch a beginner mistake, and can potentially trigger on valid code. It's unclear how common this mistake actually is
Example
Could be written as:
The text was updated successfully, but these errors were encountered: