Create rule S6913: Clamping values with cmp::min and cmp::max should use correct ranges (#4689)

* Add rust to rule S6913

* Update RSPEC

* Remove tag

---------

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:38:53 +00:00 committed by GitHub
parent 0b6c98a1f0
commit 5def9014ed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 86 additions and 24 deletions

View File

@ -1,26 +1,2 @@
{
"title": "\"Math.clamp\" should be used with correct ranges",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"java21"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6913",
"sqKey": "S6913",
"scope": "Main",
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "covered",
"code": {
"impacts": {
"RELIABILITY": "MEDIUM"
},
"attribute": "LOGICAL"
}
}

View File

@ -1,2 +1,26 @@
{
"title": "\"Math.clamp\" should be used with correct ranges",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"java21"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6913",
"sqKey": "S6913",
"scope": "Main",
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "covered",
"code": {
"impacts": {
"RELIABILITY": "MEDIUM"
},
"attribute": "LOGICAL"
}
}

View File

@ -0,0 +1,26 @@
{
"title": "Clamping values with `cmp::min` and `cmp::max` should use correct ranges",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"clippy"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6913",
"sqKey": "S6913",
"scope": "Main",
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "covered",
"code": {
"impacts": {
"RELIABILITY": "MEDIUM"
},
"attribute": "LOGICAL"
}
}

View File

@ -0,0 +1,36 @@
== Why is this an issue?
The `std::cmp::min` and `std::cmp::max` functions in Rust are useful for clamping values within a specified range. However, if these functions are mistakenly swapped, the result will not behave as intended. Instead of clamping the value within the desired range, the outcome will be a constant value, which is likely not the intended behavior.
== How to fix it
To fix this issue, ensure that `min` and `max` are used correctly to clamp the value between the desired range. The correct usage should ensure that the value is clamped between the minimum and maximum bounds.
=== Code examples
==== Noncompliant code example
[source,rust,diff-id=1,diff-type=noncompliant]
----
min(0, max(100, x))
// or
x.max(100).min(0)
----
==== Compliant solution
[source,rust,diff-id=1,diff-type=compliant]
----
max(0, min(100, x))
// or
x.min(100).max(0)
----
== Resources
=== Documentation
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#min_max