rspec/rules/S920/rust/rule.adoc
github-actions[bot] 6cf7f45131
Create rule S920: Match expression conditions should not have boolean type (#4733)
* Add rust to rule S920

* Update RSPEC

---------

Co-authored-by: yassin-kammoun-sonarsource <yassin-kammoun-sonarsource@users.noreply.github.com>
Co-authored-by: yassin-kammoun-sonarsource <yassin.kammoun@sonarsource.com>
2025-03-19 13:37:58 +00:00

35 lines
1.0 KiB
Plaintext

== 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