-
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 to change std::iter::repeat(s).take(n).collect::<String>()
to s.repeat(n)
#7260
Labels
A-lint
Area: New lints
Comments
https://doc.rust-lang.org/std/primitive.str.html#method.repeat |
Then I suppose Clippy should check if the version is >=1.16.0 if that's a thing. |
Yes. My last comment was supposed to be a note for the implementer to add that config. |
bors
added a commit
to rust-lang-ci/rust
that referenced
this issue
Jun 20, 2021
Replace some `std::iter::repeat` with `str::repeat` I noticed that there were some instances where `std::iter::repeat` would be used to repeat a string or a char to take a specific count of it and then collect it into a `String` when `str::repeat` is actually much faster and better for that. See also: rust-lang/rust-clippy#7260.
flip1995
pushed a commit
to flip1995/rust-clippy
that referenced
this issue
Jul 1, 2021
Replace some `std::iter::repeat` with `str::repeat` I noticed that there were some instances where `std::iter::repeat` would be used to repeat a string or a char to take a specific count of it and then collect it into a `String` when `str::repeat` is actually much faster and better for that. See also: rust-lang#7260.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What it does
It suggests the user to change
std::iter::repeat(string).take(n).collect::<String>()
tostring.repeat(n)
. It should work without theString
type argument tocollect
too and clippy has to find out based on context whether it does collect to aString
.Additionally, when the argument to
std::iter::repeat
is a constant char like'a'
it should suggest to change that to"a".repeat(n)
. I said only constant chars because I'm not sure how well it will work if it does that for variable chars as well.Categories (optional)
clippy::perf
It's faster and generates a lot less instructions, in the
char
case too.It's also easier to read.
Drawbacks
None.
Example
Could be written as:
See also: rust-lang/rust#85538.
The text was updated successfully, but these errors were encountered: