rust-lang / rust-lang/rust-clippy

`manual_str_repeat` should trigger for `repeat_n` with msrv 1.82

Open
#13,784 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-negative
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

Since Rust 1.82.0 users can replace std::iter::repeat(x).take(m).collect::<String>() by the (almost equivalent) std::iter::repeat_n(x,m).collect::<String>(). Both are manual versions of string repeat, but only the former emits a warning.

Playground link

I would be happy to try to contribute a PR for this if the maintainers agree that this is a false negative.

Thanks for clippy, it's the best!

Lint Name

manual_str_repeat

Reproducer

I tried this code:

fn main() {
    let text: String = std::iter::repeat_n('a',5).collect();
    println!("{text}");
}

I expected to see this happen:

    Checking playground v0.0.1 (/playground)
warning: manual implementation of `str::repeat` using iterators
 --> src/main.rs:2:24
  |
2 |     let text: String = std::iter::repeat_n('a',5).collect();
  |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `"a".repeat(5)`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_str_repeat
  = note: `#[warn(clippy::manual_str_repeat)]` on by default

Instead, this happened:
(no warning emitted)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the manual_str_repeat lint entry point and reproduce the issue with the Rust 1.82 repeat_n example from the report. Compare its behavior with the existing repeat(...).take(...) case. Done means the shown repeat_n pattern emits the manual_str_repeat warning and suggests "a".repeat(5).

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.