-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Suggest using Vec::extend()
in same_item_push
#13987
Conversation
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.
Small comment and a suggestion. The rest looks really good :D
@@ -50,6 +50,16 @@ fn main() { | |||
//~^ ERROR: it looks like the same item is being pushed into this Vec | |||
} | |||
|
|||
#[clippy::msrv = "1.81"] |
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.
Please make sure to also add the lint to the configuration:
rust-clippy/clippy_config/src/conf.rs
Lines 649 to 651 in 25d319d
use_self, | |
)] | |
msrv: Msrv = Msrv::empty(), |
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.
I always forget that users may configure the msrv in Clippy as well, thanks for the reminder!
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.
If you want a project, you can try to find a way to do this automatically. This has been something like a holly grail for some time. ^^
Using `Vec::extend(std::iter::repeat_n(item, N))` allows to use the more natural number of elements to add `N`, as is probably done in the original loop, instead of computing the difference between the existing number of elements and the wanted one. Before MSRV 1.82, the older suggestion to use `Vec::resize()` is still issued.
b90338f
to
ded9354
Compare
LGTM, thanks for the addition. (And sorry for the delay with my review)
|
Using
Vec::extend(std::iter::repeat_n(item, N))
allows to use the more natural number of elements to addN
, as is probably done in the original loop, instead of computing the difference between the existing number of elements and the wanted one.Before MSRV 1.82, the older suggestion to use
Vec::resize()
is still issued.Inspired by #6156 (which predates
repeat_n()
).changelog: [
same_item_push
]: recommend usingVec::extend()
to extend a vector