Skip to content

False positive needless_match for even simple example #14754

Open
@yezhizhen

Description

@yezhizhen

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

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-bugIssue: The suggestion compiles but changes the code to behave in an unintended way

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions