![github-actions[bot]](/assets/img/avatar_default.png)
* 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>
37 lines
997 B
Plaintext
37 lines
997 B
Plaintext
== 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
|