Skip to content

Option::as_mut followed by Option::take is a foot-gun #13671

Closed
@thomaseizinger

Description

@thomaseizinger

What it does

Warns users that calling take on an Option created by as_mut does not clear the original Option.

It is useless to take an Option that is only holding a reference, one might as well pattern match on it directly.

Advantage

It makes it clearer that the original option is not modified.

Drawbacks

No response

Example

let mut option = Some("foo");
let maybe_foo = option.as_mut().take();

"Calling take on an Option with a reference does not clear the original Option. Remove as_mut or pattern-match on the option directly if you did not intend to clear it."

Could be written as:

let mut option = Some("foo");
let maybe_foo = option.take();
let mut option = Some("foo");
if let Some(a) = option.as_mut() {
    // ...
}

Metadata

Metadata

Assignees

Labels

A-lintArea: New lints

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions