Skip to content

while_let_loop suggests wrong name to use in pattern #7362

@domenicquirl

Description

@domenicquirl

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 

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions