For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/static_analysis/static_analysis_rules/rust-code-quality/redundant-pattern-matching.md. A documentation index is available at /llms.txt.

Redundant pattern match on Result or Option

This product is not supported for your selected Datadog site. ().

Metadata

ID: rust-code-quality/redundant-pattern-matching

Language: Rust

Severity: Warning

Category: Code Style

Description

Using if let or while let to match Ok, Err, Some, or None just to check presence is unnecessarily verbose. Use the corresponding method instead: is_ok(), is_err(), is_some(), or is_none(). This is shorter and more idiomatic Rust.

Non-Compliant Code Examples

if let Ok(_) = Ok::<i32, i32>(42) {}
if let Err(_) = Err::<i32, i32>(42) {}
if let None = None::<()> {}
while let Some(_) = some_option {}

if let Ok(_) = some_result {
    println!("ok");
} else {
    println!("err");
}

Compliant Code Examples

if Ok::<i32, i32>(42).is_ok() {}
if Err::<i32, i32>(42).is_err() {}
if None::<()>.is_none() {}
while some_option.is_some() {}

if let Ok(val) = some_result {
    println!("{}", val);
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Security