Clippy lints / Pedantic
Clippy lint · Pedantic · checked with Clippy 0.1.99
clippy::option_as_ref_cloned: cloning an `Option<_>` using `.as_ref().cloned()`
What it meansChecks for usage of .as_ref().cloned() and .as_mut().cloned() on Options It is in the pedantic group and off by default.
What you see
warning: cloning an `Option<_>` using `.as_ref().cloned()` --> main.rs:4:9 | 4 | bar.as_ref().cloned() | ^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#option_as_ref_cloned = note: `-W clippy::option-as-ref-cloned` implied by `-W clippy::pedantic` help: this can be written more concisely by cloning the `Option<_>` directly | 4 - bar.as_ref().cloned() 4 + bar.clone() |
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?
This can be written more concisely by cloning the Option directly.
Stricter lints with some false positives. Off by default; opt in with clippy::pedantic.
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
fn foo(bar: &Option<Vec<u8>>) -> Option<Vec<u8>> {
bar.as_ref().cloned()
}
Write this instead
fn foo(bar: &Option<Vec<u8>>) -> Option<Vec<u8>> {
bar.clone()
}
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
option_as_ref_cloned = "warn"In code
#![warn(clippy::option_as_ref_cloned)]One run
cargo clippy -- -W clippy::option-as-ref-clonedThe [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 usage of .as_ref().cloned() and .as_mut().cloned() on Options
Why is this bad?
This can be written more concisely by cloning the Option directly.
Example
fn foo(bar: &Option<Vec<u8>>) -> Option<Vec<u8>> {
bar.as_ref().cloned()
}
Use instead:
fn foo(bar: &Option<Vec<u8>>) -> Option<Vec<u8>> {
bar.clone()
}
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- too_many_linesChecks for functions with a large amount of lines
- missing_errors_docChecks the doc comments of publicly visible functions that return a Result type and warns if there is no # Errors section
- must_use_candidateChecks for public functions that have no #[must_use] attribute, but return something not already marked must-use, have no mutable arg and mutate no statics
- no_mangle_with_rust_abi`#[no_mangle]` set on a function with the default (`Rust`) ABI
- non_std_lazy_staticsLints when once_cell::sync::Lazy or lazy_static! are used to define a static variable, and suggests replacing such cases with std::sync::LazyLock instead. Note: This lint will not trigger in crate with no_std context, or with MSRV < 1.80.0
- nonminimal_boolChecks for boolean expressions that can be written more concisely
- option_optionuse of `Option<Option<T>>`
- overly_complex_bool_exprChecks for boolean expressions that contain terminals that can be eliminated
- ptr_as_ptr`as` casting between raw pointers without changing their constness
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::too_many_linesChecks for functions with a large amount of linesRead it next →Did this answer your question?