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>
This commit is contained in:
github-actions[bot] 2025-03-19 13:37:58 +00:00 committed by GitHub
parent 982f059788
commit 6cf7f45131
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 101 additions and 33 deletions

View File

@ -1,35 +1,2 @@
{
"title": "Switch statement conditions should not have essentially boolean type",
"type": "CODE_SMELL",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW"
},
"attribute": "CLEAR"
},
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "10min"
},
"tags": [
"misra-c++2008",
"misra-c2004",
"misra-c2012"
],
"extra": {
"replacementRules": [
],
"legacyKeys": [
]
},
"defaultSeverity": "Minor",
"ruleSpecification": "RSPEC-920",
"sqKey": "S920",
"scope": "Main",
"defaultQualityProfiles": [
],
"quickfix": "unknown"
}

View File

@ -1,2 +1,35 @@
{
"title": "Switch statement conditions should not have essentially boolean type",
"type": "CODE_SMELL",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW"
},
"attribute": "CLEAR"
},
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "10min"
},
"tags": [
"misra-c++2008",
"misra-c2004",
"misra-c2012"
],
"extra": {
"replacementRules": [
],
"legacyKeys": [
]
},
"defaultSeverity": "Minor",
"ruleSpecification": "RSPEC-920",
"sqKey": "S920",
"scope": "Main",
"defaultQualityProfiles": [
],
"quickfix": "unknown"
}

View File

@ -0,0 +1,34 @@
{
"title": "Match expression conditions should not have boolean type",
"type": "CODE_SMELL",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW"
},
"attribute": "CLEAR"
},
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "10min"
},
"tags": [
"clippy"
],
"extra": {
"replacementRules": [
],
"legacyKeys": [
]
},
"defaultSeverity": "Minor",
"ruleSpecification": "RSPEC-920",
"sqKey": "S920",
"scope": "Main",
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "unknown"
}

34
rules/S920/rust/rule.adoc Normal file
View File

@ -0,0 +1,34 @@
== 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