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

Suggest using Vec::extend() in same_item_push #13987

Merged
merged 1 commit into from
Jan 22, 2025

Conversation

samueltardieu
Copy link
Contributor

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.

Inspired by #6156 (which predates repeat_n()).

changelog: [same_item_push]: recommend using Vec::extend() to extend a vector

@rustbot
Copy link
Collaborator

rustbot commented Jan 11, 2025

r? @xFrednet

rustbot has assigned @xFrednet.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 11, 2025
Copy link
Member

@xFrednet xFrednet left a 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

clippy_lints/src/loops/same_item_push.rs Outdated Show resolved Hide resolved
@@ -50,6 +50,16 @@ fn main() {
//~^ ERROR: it looks like the same item is being pushed into this Vec
}

#[clippy::msrv = "1.81"]
Copy link
Member

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:

use_self,
)]
msrv: Msrv = Msrv::empty(),

Copy link
Contributor Author

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!

Copy link
Member

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.
@xFrednet
Copy link
Member

xFrednet commented Jan 22, 2025

LGTM, thanks for the addition. (And sorry for the delay with my review)

error: looks like @xFrednet is bad add rhyming
  --> tests/poems/special-pr.rs:30:9
   |
LL |         Roses are red,
LL |         Violets are blue,
LL |         Use extend instead,
LL |         Of a for loop,
   |                  ^^^^
   |
   = help: consider using getting good
   = note: My creativity is a bit drained rn xD

@xFrednet xFrednet added this pull request to the merge queue Jan 22, 2025
Merged via the queue into rust-lang:master with commit 67e6bf3 Jan 22, 2025
11 checks passed
@samueltardieu samueltardieu deleted the push-rwtrylsmrlyt branch January 22, 2025 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants