Skip to content

Incorrect behaviour change suggestion for manual_ok_err #14660

Closed
@jtroo

Description

@jtroo

Summary

This code snippet gets incorrectly caught by the manual_ok_err check when it is incorrect to apply it. The expected behaviour is that the clippy lint does not fire in this function.

fn parse_non_zero_u16(expr: &SExpr, s: &ParserState, label: &str) -> Result<u16> {
    expr.atom(s.vars())
        .map(str::parse::<u16>)
        .and_then(|u| match u {
            Ok(u @ 1..) => Some(u),
            _ => None,
        })
        .ok_or_else(|| anyhow_expr!(expr, "{label} must be 1-65535"))
}

Clippy suggests:

2068 |           .and_then(|u| match u {
     |  _______________________^
2069 | |             Ok(u @ 1..) => Some(u),
2070 | |             _ => None,
2071 | |         })
     | |_________^ help: replace with: `u.ok()`

I reviewed issue #14533 and #14239 as well which looked related to manual_ok_err but they don't immediately seem to be the same issue to me.

Reproducer

I tried this code:

        match u {
            Ok(u @ 1..) => Some(u),
            _ => None,
        }

I expected to see this happen:

No clippy lint fires.

Instead, this happened:

Clippy lint fires and suggests a behaviour change.

Version

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-pc-windows-msvc
release: 1.86.0
LLVM version: 19.1.7

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions