Skip to content

match_as_ref suggestion is not equivalent to the matching code #15691

@mmtftr

Description

@mmtftr

Summary

let dbtx_ref = match db_tx {
    Some(ref mut db_tx) => Some(db_tx),
    None => dbtx.as_deref_mut(),
};

The suggestion says let dbtx_ref = db_tx.as_mut(); which is not accurate. It should've been let dbtx_ref = db_tx.as_mut().or_else(|| /* whatever's in the None branch if not None */)

Lint Name

match_as_ref

Reproducer

I tried this code:

<code>
let dbtx_ref = match db_tx {
    Some(ref mut db_tx) => Some(db_tx),
    None => dbtx.as_deref_mut(),
};
</code>

I saw this happen:

<output>
let dbtx_ref = db_tx.as_mut();
</output>

I expected to see this happen:

<output>
let dbtx_ref = db_tx.as_mut().or(dbtx.as_deref_mut());
</output>

Version

rustc 1.85.1 (4eb161250 2025-03-15)
binary: rustc
commit-hash: 4eb161250e340c8f48f66e2b929ef4a5bed7c181
commit-date: 2025-03-15
host: aarch64-apple-darwin
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