rust-lang / rust-lang/rust-clippy

`redundant_guards` on match guard of an expression rebinding

Open
#11,841 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

When matching an expression with a guard that is true only if the expression's rebinding does not have a certain value, clippy will warn the guard as redundant_guards and its help message will change the code's original meaning.

Reproducer

I tried this code:

fn main() {
    match 1 + 1 {
        b if match b {
            1 => false,
            _ => true
        } => println!("branch 1"),
        _ => println!("branch 2")
    }
}

I expected to see this happen:

No warning of redundant guard

Instead, this happened:

warning: redundant guard
 --> src/main.rs:3:14
  |
3 |           b if match b {
  |  ______________^
4 | |             1 => false,
5 | |             _ => true
6 | |         } => println!("branch 1"),
  | |_________^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_guards
  = note: `#[warn(clippy::redundant_guards)]` on by default
help: try
  |
3 -         b if match b {
3 +         1 => println!("branch 1"),
  |
Version
rustc 1.76.0-nightly (283170175 2023-11-17)
binary: rustc
commit-hash: 2831701757eb7b3105eda26a306c2f3a97e2664b
commit-date: 2023-11-17
host: x86_64-unknown-linux-gnu
release: 1.76.0-nightly
LLVM version: 17.0.5
Additional Labels

No response

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 reproducing the Rust snippet and inspect the implementation and existing tests for Clippy's redundant_guards lint. Verify that the lint does not warn on this expression-rebinding guard and that any suggested replacement preserves the program's original 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.