rspec/rules/S920/rust/rule.adoc

35 lines
1.0 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
Using a ``++match++`` expression with a boolean condition is considered a bad practice because it adds unnecessary complexity to the code. The ``++match++`` expression is designed for pattern matching and is more powerful and flexible than an ``++if-else++`` construct. However, when used with a simple boolean condition, it introduces extra verbosity and reduces code readability. Instead, it is generally better to use an ``++if-else++`` construct.
=== Noncompliant code example
[source,rust,diff-id=1,diff-type=noncompliant]
----
fn check_value(value: bool) -> &'static str {
match value {
true => "Value is true",
false => "Value is false",
}
}
----
=== Compliant solution
[source,rust,diff-id=1,diff-type=compliant]
----
fn check_value(value: bool) -> &'static str {
if value {
"Value is true"
} else {
"Value is false"
}
}
----
== Resources
=== Documentation
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#match_bool