rust-lang / rust-lang/rust-clippy
`while_let_loop` suggests wrong name to use in pattern
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
When running clippy on this code
fn foo() -> Option<usize> {
todo!()
}
fn main() {
loop {
let x = match foo() {
Some(n) => n,
None => break,
};
println!("{}", x);
}
}
it (rightly) suggests that while let could be used instead. However, the suggested fix is try: 'while let Some(n) = foo() { .. }' (playground link), taking the n from the pattern inside the match instead of the x from let x. Carrying out this replacement introduces compilation errors, as x is now undefined.
Expected behaviour:
Suggest trying while let Some(x), taking the variable name that binds the result of the match expression.
Meta
cargo clippy -V:clippy 0.1.52 (9bc8c42b 2021-05-09)rustc -Vv:rustc 1.52.1 (9bc8c42bb 2021-05-09) binary: rustc commit-hash: 9bc8c42bb2f19e745a63f3445f1ac248fb015e53 commit-date: 2021-05-09 host: x86_64-pc-windows-msvc release: 1.52.1 LLVM version: 12.0.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the report with the supplied Rust snippet and run cargo clippy to observe the while_let_loop suggestion. Trace the lint implementation and its tests from the Clippy source, then verify that the suggested pattern preserves the variable used by println! and no longer causes compilation errors.
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
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100