rust-lang / rust-lang/rust-clippy

`match_single_binding` suggestion potentially produces different behavior

Open
#8,815 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug C-enhancement L-suggestion
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

The suggestion currently puts the bindings outside of the block, which can extend their lifetimes and potentially shadow other values.

Reproducer

I tried this code:

let foo = 3;
match (1, 2) {
    (foo, bar) => {
        println!("{}", foo + bar); // 3
    }
}

println!("{}", foo + 3); // 6

I expected to see this happen:

let foo = 3;
{
    let (foo, bar) = (1, 2);
    println!("{}", foo + bar); // 3
}

println!("{}", foo + 3); // 6

Instead, this happened:

let foo = 3;
let (foo, bar) = (1, 2);
{
    println!("{}", foo + bar); // 3
}

println!("{}", foo + 3); // 4
Version
rustc 1.62.0-nightly (8f36334ca 2022-04-06)
binary: rustc
commit-hash: 8f36334ca939a67cce3f37f24953ff6f2d3f3d33
commit-date: 2022-04-06
host: x86_64-unknown-linux-gnu
release: 1.62.0-nightly
LLVM version: 14.0.0
Additional Labels

@rustbot label +C-enhancement

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 by locating the match_single_binding lint and reproduce the issue with the Rust example in the report. Compare the generated suggestion with the expected block-scoped form; done means the suggestion preserves the outer foo value and produces the expected behavior.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.