Skip to content

False positive needless_match for even simple example #14754

New issue

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

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

Already on GitHub? Sign in to your account

Open
yezhizhen opened this issue May 8, 2025 · 2 comments · May be fixed by #14757
Open

False positive needless_match for even simple example #14754

yezhizhen opened this issue May 8, 2025 · 2 comments · May be fixed by #14757
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-bug Issue: The suggestion compiles but changes the code to behave in an unintended way

Comments

@yezhizhen
Copy link

yezhizhen commented May 8, 2025

Summary

needless_match is warned, while the suggestion would really change the execution logic.

Discovered in servo/servo#36908 (comment)

Lint Name

needless_match

Reproducer

I tried this code:

fn side_effect() {
    println!("Side effect!");
}

fn t() -> Result<i32, &'static str> {
    if 5 > 3 { Ok(3) } else { Err("Error") }
}

fn logic() -> Result<i32, &'static str> {
    let x = match t() {
        Ok(v) => Ok(v),
        err @ Err(_) => return err,
    };
    // Only when Ok
    side_effect();
    x
}

fn main() {
    let _ = logic();
}

I saw this happen:

warning: this match expression is unnecessary
  --> src\main.rs:10:13
   |
10 |       let x = match t() {
   |  _____________^
11 | |         Ok(v) => Ok(v),
12 | |         err @ Err(_) => return err,
13 | |     };
   | |_____^ help: replace it with: `t()`

I expected to see this happen:
No Warning. As the suggestion would change the execution logic.

Version

rustc 1.85.1 (4eb161250 2025-03-15)
binary: rustc
commit-hash: 4eb161250e340c8f48f66e2b929ef4a5bed7c181
commit-date: 2025-03-15
host: x86_64-pc-windows-msvc
release: 1.85.1
LLVM version: 19.1.7

Additional Labels

No response

@yezhizhen yezhizhen added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels May 8, 2025
@samueltardieu
Copy link
Contributor

@rustbot claim
@rustbot label +I-suggestion-causes-error

@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label May 8, 2025
@samueltardieu
Copy link
Contributor

@rustbot label -I-suggestion-causes-error +I-suggestion-causes-bug

@rustbot rustbot added I-suggestion-causes-bug Issue: The suggestion compiles but changes the code to behave in an unintended way and removed I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels May 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-bug Issue: The suggestion compiles but changes the code to behave in an unintended way
Projects
None yet
3 participants