Clippy lints / Restriction
Clippy lint · Restriction · checked with Clippy 0.1.99
clippy::ref_patterns: usage of ref pattern
What it meansChecks for usages of the ref keyword. It is in the restriction group and off by default.
What you see
warning: usage of ref pattern --> main.rs:4:13 | 4 | if let Some(ref foo) = opt {} | ^^^^^^^ | = help: consider using `&` for clarity instead = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#ref_patterns = note: requested on the command line with `-W clippy::ref-patterns`
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?
The ref keyword can be confusing for people unfamiliar with it, and often it is more concise to use & instead.
Lints that forbid language features for specific codebases (no unwrap, no indexing...). Off by default; pick them one by one, never the whole group.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
let opt = Some(5);
if let Some(ref foo) = opt {}
Write this instead
let opt = Some(5);
if let Some(foo) = &opt {}
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_patterns = "warn"In code
#![warn(clippy::ref_patterns)]One run
cargo clippy -- -W clippy::ref-patternsThe [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 usages of the ref keyword.
Why restrict this?
The ref keyword can be confusing for people unfamiliar with it, and often
it is more concise to use & instead.
Example
let opt = Some(5);
if let Some(ref foo) = opt {}
Use instead:
let opt = Some(5);
if let Some(foo) = &opt {}
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on Options
- module_name_repetitionsDetects public item names that are prefixed or suffixed by the containing public module’s name
- expect_usedChecks for .expect() or .expect_err() calls on Results and .expect() call on Options
- rc_mutexChecks for Rc<Mutex<T>>
- redundant_test_prefixChecks for test functions (functions annotated with #[test]) that are prefixed with test_ which is redundant
- redundant_type_annotationsredundant type annotation
- renamed_function_paramsrenamed function parameter of trait impl
- rest_pat_in_fully_bound_structsChecks for unnecessary ‘..’ pattern binding on struct when all fields are explicitly matched
- rest_pattern_accessible_fieldstruct destructuring with rest (`..`)
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on OptionsRead it next →Did this answer your question?