Skip to content

irrefutable_let_patterns should not lint at the start of an else if let-chain #128661

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Jarcho opened this issue Aug 4, 2024 · 2 comments · Fixed by #129394
Closed

irrefutable_let_patterns should not lint at the start of an else if let-chain #128661

Jarcho opened this issue Aug 4, 2024 · 2 comments · Fixed by #129394
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. F-let_chains `#![feature(let_chains)]` L-irrefutable_let_patterns Lint: irrefutable_let_patterns T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@Jarcho
Copy link
Contributor

Jarcho commented Aug 4, 2024

The following triggers irrefutable_let_patterns:

fn expensive() -> u32 {
    0
}

fn option(x: u32) -> Option<u32> {
    Some(x)
}

fn f(x: bool) {
    if x {
        // something
    } else if let foo = expensive()
        && let Some(bar) = option(foo)
    {
        // use `foo` and `bar`
    } else {
        // something
    }
}

Though let foo = expensive() is irrefutable, it would add an extra layer of nesting to move it into a regular let statement. e.g.

if x {
    // something
} else {
    let foo = expensive()
    if let Some(bar) = option(foo) {
        // use `foo` and `bar`
    } else {
        // something
    }
}
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 4, 2024
@workingjubilee workingjubilee added F-let_chains `#![feature(let_chains)]` L-irrefutable_let_patterns Lint: irrefutable_let_patterns labels Aug 4, 2024
@workingjubilee workingjubilee changed the title irrefutable_let_patterns shouldn't trigger on else if irrefutable_let_patterns should track arms in if let chains Aug 4, 2024
@workingjubilee workingjubilee changed the title irrefutable_let_patterns should track arms in if let chains irrefutable_let_patterns needs to work with let-chains Aug 4, 2024
@workingjubilee
Copy link
Member

Sorry for the sudden retitling, I noticed your example uses #![feature(let_chains)] and I'm not sure if this combination of events can occur without it.

@Jarcho
Copy link
Contributor Author

Jarcho commented Aug 4, 2024

I was actually about to change that. I realized after submitting this that the lint applies to all let expression, not just let chains.

@Jarcho Jarcho changed the title irrefutable_let_patterns needs to work with let-chains irrefutable_let_patterns should not lint at the start of an else if let-chain Aug 4, 2024
@jieyouxu jieyouxu added A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 5, 2024
@jieyouxu jieyouxu added T-lang Relevant to the language team, which will review and decide on the PR/issue. C-bug Category: This is a bug. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Aug 13, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 29, 2024
…=Nadrieril

Don't lint `irrefutable_let_patterns` on leading patterns if `else if` let-chains

fixes rust-lang#128661

Is there any preference where the test goes? There looks to be several places it could fit.
workingjubilee added a commit to workingjubilee/rustc that referenced this issue Oct 30, 2024
…=Nadrieril

Don't lint `irrefutable_let_patterns` on leading patterns if `else if` let-chains

fixes rust-lang#128661

Is there any preference where the test goes? There looks to be several places it could fit.
@bors bors closed this as completed in 87d348b Oct 30, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 30, 2024
Rollup merge of rust-lang#129394 - Jarcho:irrefutable_let_patterns, r=Nadrieril

Don't lint `irrefutable_let_patterns` on leading patterns if `else if` let-chains

fixes rust-lang#128661

Is there any preference where the test goes? There looks to be several places it could fit.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-bug Category: This is a bug. F-let_chains `#![feature(let_chains)]` L-irrefutable_let_patterns Lint: irrefutable_let_patterns T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants