Clippy lints / Pedantic
Clippy lint · Pedantic · checked with Clippy 0.1.99
clippy::ref_option_ref: since `&` implements the `Copy` trait, `&Option<&T>` can be simplified to `Option<&T>`
What it meansChecks for usage of &Option<&T>. It is in the pedantic group and off by default.
What you see
warning: since `&` implements the `Copy` trait, `&Option<&T>` can be simplified to `Option<&T>` --> main.rs:3:8 | 3 | let x: &Option<&u32> = &Some(&0u32); | ^^^^^^^^^^^^^ help: try: `Option<&u32>` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#ref_option_ref = note: `-W clippy::ref-option-ref` implied by `-W clippy::pedantic`
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?
Since & is Copy, it’s useless to have a reference on Option<&T>.
Stricter lints with some false positives. Off by default; opt in with clippy::pedantic.
How to fix it
Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:
Flagged
let x: &Option<&u32> = &Some(&0u32);
Write this instead
let x: Option<&u32> = Some(&0u32);
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]
ref_option_ref = "warn"In code
#![warn(clippy::ref_option_ref)]One run
cargo clippy -- -W clippy::ref-option-refThe [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 &Option<&T>.
Why is this bad?
Since & is Copy, it’s useless to have a
reference on Option<&T>.
Known problems
It may be irrelevant to use this lint on public API code as it will make a breaking change to apply it.
Example
let x: &Option<&u32> = &Some(&0u32);
Use instead:
let x: Option<&u32> = Some(&0u32);
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
- ref_as_ptrreference as raw pointer
- ref_binding_to_referenceChecks for ref bindings which create a reference to a reference
- ref_optionWarns when a function signature uses &Option<T> instead of Option<&T>
- return_self_not_must_usemissing `#[must_use]` attribute on a method returning `Self`
- same_functions_in_if_conditionChecks for consecutive ifs with the same function call
- same_length_and_capacityChecks for usages of Vec::from_raw_parts and String::from_raw_parts where the same expression is used for the length and the capacity
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?