Clippy lints / Complexity
Clippy lint · Complexity · checked with Clippy 0.1.99
clippy::needless_option_take: called `Option::take()` on a temporary value
What it meansChecks for calling take function after as_ref. It is in the complexity group and warns by default.
What you see
warning: called `Option::take()` on a temporary value --> main.rs:4:1 | 4 | x.as_ref().take(); | ^^^^^^^^^^------- | | | help: remove | = note: `as_ref` creates a temporary value, so calling take() has no effect = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#needless_option_take = note: `#[warn(clippy::needless_option_take)]` on by default
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
Redundant code. take writes None to its argument. In this case the modification is useless as it’s a temporary that cannot be read from afterwards.
Code that does something simple in a complicated way. Warns by default.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
let x = Some(3);
x.as_ref().take();
Write this instead
let x = Some(3);
x.as_ref();
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::needless_option_take)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
needless_option_take = "allow"One run
cargo clippy -- -A clippy::needless-option-takeThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Checks for calling take function after as_ref.
Why is this bad?
Redundant code. take writes None to its argument.
In this case the modification is useless as it’s a temporary that cannot be read from afterwards.
Example
let x = Some(3);
x.as_ref().take();
Use instead:
let x = Some(3);
x.as_ref();
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- too_many_argumentsChecks for functions with too many parameters
- clone_on_copyusing `clone` on type `u64` which implements the `Copy` trait
- type_complexityChecks for types used in structs, parameters and let declarations above a certain complexity threshold
- needless_lifetimesthe following explicit lifetimes could be elided: 'a
- needless_matchChecks for unnecessary match or match-like if let returns for Option and Result when function signatures are the same
- needless_option_as_derefderefed type is same as origin
- needless_question_markSuggests replacing Ok(x?) or Some(x?) with x in return positions where the ? operator is not needed to convert the type of x
- needless_splitnunnecessary use of `splitn`
- needless_updateChecks for needlessly including a base struct on update when all fields are changed anyway. This lint is not applied to structs marked with non_exhaustive
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::too_many_argumentsChecks for functions with too many parametersRead it next →Did this answer your question?