Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions rules/S6913/java/metadata.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
24 changes: 24 additions & 0 deletions rules/S6913/metadata.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
27 changes: 27 additions & 0 deletions rules/S6913/rust/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"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",
"min_max"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-6913",
"sqKey": "S6913",
"scope": "Main",
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "covered",
"code": {
"impacts": {
"RELIABILITY": "MEDIUM"
},
"attribute": "LOGICAL"
}
}
36 changes: 36 additions & 0 deletions rules/S6913/rust/rule.adoc
Original file line number Diff line number Diff line change
@@ -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