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:
parent
0b6c98a1f0
commit
5def9014ed
@ -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"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
26
rules/S6913/rust/metadata.json
Normal file
26
rules/S6913/rust/metadata.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
36
rules/S6913/rust/rule.adoc
Normal file
36
rules/S6913/rust/rule.adoc
Normal 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
|
Loading…
x
Reference in New Issue
Block a user