Skip to content

manual_let_else should reuse the identifier from the original let statement #9939

@Serial-ATA

Description

@Serial-ATA

Summary

The lint will emit an invalid suggestion when the binding name in the pattern differs from the name used in the let statement.

Reproducer

I tried this code:

#![warn(clippy::manual_let_else)]

fn main() {
    let foo = Some(1);

    let value = match foo {
        Some(v) => v,
        _ => return,
    };
    
    println!("We have a value of: {value}");
}

I expected to see this happen:

#![warn(clippy::manual_let_else)]

fn main() {
    let foo = Some(1);

    let Some(value) = foo else { return };
    
    println!("We have a value of: {value}");
}

This is the suggestion it should emit, reusing the name value.

Instead, this happened:

#![warn(clippy::manual_let_else)]

fn main() {
    let foo = Some(1);

	// It reuses the name `v`, which is not expected later on
    let Some(v) = foo else { return };
    
    println!("We have a value of: {value}");
}

This suggestion causes an error:

error[[E0425]](https://doc.rust-lang.org/nightly/error-index.html#E0425): cannot find value `value` in this scope
 --> src/main.rs:8:36
  |
8 |     println!("We have a value of: {value}");
  |                                    ^^^^^ not found in this scope

Version

rustc 1.67.0-nightly (70f8737b2 2022-11-23)
binary: rustc
commit-hash: 70f8737b2f5d3bf7d6b784fad00b663b7ff9feda
commit-date: 2022-11-23
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions