Clippy lints / Style
Clippy lint · Style · checked with Clippy 0.1.99
clippy::redundant_pattern_matching: Lint for redundant pattern matching over Result, Option, std::task::Poll, std::net::IpAddr or bools
What it meansLint for redundant pattern matching over Result, Option, std::task::Poll, std::net::IpAddr or bools It is in the style group and warns by default.
Does it matter?
It’s more concise and clear to just use the proper utility function or using the condition directly
Code that works but is not idiomatic Rust. 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
if let Ok(_) = Ok::<i32, i32>(42) {}
if let Err(_) = Err::<i32, i32>(42) {}
if let None = None::<()> {}
if let Some(_) = Some(42) {}
if let Poll::Pending = Poll::Pending::<()> {}
if let Poll::Ready(_) = Poll::Ready(42) {}
if let IpAddr::V4(_) = IpAddr::V4(Ipv4Addr::LOCALHOST) {}
if let IpAddr::V6(_) = IpAddr::V6(Ipv6Addr::LOCALHOST) {}
match Ok::<i32, i32>(42) {
Ok(_) => true,
Err(_) => false,
};
let cond = true;
if let true = cond {}
matches!(cond, true);
Write this instead
if Ok::<i32, i32>(42).is_ok() {}
if Err::<i32, i32>(42).is_err() {}
if None::<()>.is_none() {}
if Some(42).is_some() {}
if Poll::Pending::<()>.is_pending() {}
if Poll::Ready(42).is_ready() {}
if IpAddr::V4(Ipv4Addr::LOCALHOST).is_ipv4() {}
if IpAddr::V6(Ipv6Addr::LOCALHOST).is_ipv6() {}
Ok::<i32, i32>(42).is_ok();
let cond = true;
if cond {}
cond;
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::redundant_pattern_matching)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
redundant_pattern_matching = "allow"One run
cargo clippy -- -A clippy::redundant-pattern-matchingThe [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
Lint for redundant pattern matching over Result, Option,
std::task::Poll, std::net::IpAddr or bools
Why is this bad?
It’s more concise and clear to just use the proper utility function or using the condition directly
Known problems
For suggestions involving bindings in patterns, this will change the drop order for the matched type.
Both if let and while let will drop the value at the end of the block, both if and while will drop the
value before entering the block. For most types this change will not matter, but for a few
types this will not be an acceptable change (e.g. locks). See the
reference for more about
drop order.
Example
if let Ok(_) = Ok::<i32, i32>(42) {}
if let Err(_) = Err::<i32, i32>(42) {}
if let None = None::<()> {}
if let Some(_) = Some(42) {}
if let Poll::Pending = Poll::Pending::<()> {}
if let Poll::Ready(_) = Poll::Ready(42) {}
if let IpAddr::V4(_) = IpAddr::V4(Ipv4Addr::LOCALHOST) {}
if let IpAddr::V6(_) = IpAddr::V6(Ipv6Addr::LOCALHOST) {}
match Ok::<i32, i32>(42) {
Ok(_) => true,
Err(_) => false,
};
let cond = true;
if let true = cond {}
matches!(cond, true);
The more idiomatic use would be:
if Ok::<i32, i32>(42).is_ok() {}
if Err::<i32, i32>(42).is_err() {}
if None::<()>.is_none() {}
if Some(42).is_some() {}
if Poll::Pending::<()>.is_pending() {}
if Poll::Ready(42).is_ready() {}
if IpAddr::V4(Ipv4Addr::LOCALHOST).is_ipv4() {}
if IpAddr::V6(Ipv6Addr::LOCALHOST).is_ipv6() {}
Ok::<i32, i32>(42).is_ok();
let cond = true;
if cond {}
cond;
Past names
- if_let_redundant_pattern_matching
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- needless_returnunneeded `return` statement
- new_without_defaultChecks for public types with a pub fn new() -> Self method and no implementation of Default
- len_zeroChecks for getting the length of something via .len() just to compare to zero, and suggests using .is_empty() where applicable
- redundant_closureChecks for closures which just call another function where the function can be called directly
- redundant_field_namesredundant field names in struct initialization
- redundant_patternthe `y @ _` pattern can be written as just `y`
- redundant_static_lifetimesconstants have by default a `'static` lifetime
- result_map_or_into_optionChecks for usage of _.map_or(None, Some)
- result_unit_errthis returns a `Result<_, ()>`
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::needless_returnunneeded `return` statementRead it next →Did this answer your question?