Compare commits
2 Commits
master
...
rule/add-R
Author | SHA1 | Date | |
---|---|---|---|
![]() |
65157f31ee | ||
![]() |
4be3c6eb3a |
2
rules/S7463/metadata.json
Normal file
2
rules/S7463/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
24
rules/S7463/rust/metadata.json
Normal file
24
rules/S7463/rust/metadata.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"title": "`saturating_sub` should be used to avoid subtraction underflow",
|
||||
"type": "BUG",
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "5min"
|
||||
},
|
||||
"tags": [
|
||||
"clippy"
|
||||
],
|
||||
"defaultSeverity": "Major",
|
||||
"ruleSpecification": "RSPEC-7463",
|
||||
"sqKey": "S7463",
|
||||
"scope": "All",
|
||||
"defaultQualityProfiles": ["Sonar way"],
|
||||
"quickfix": "unknown",
|
||||
"code": {
|
||||
"impacts": {
|
||||
"RELIABILITY": "MEDIUM"
|
||||
},
|
||||
"attribute": "LOGICAL"
|
||||
}
|
||||
}
|
28
rules/S7463/rust/rule.adoc
Normal file
28
rules/S7463/rust/rule.adoc
Normal file
@ -0,0 +1,28 @@
|
||||
== Why is this an issue?
|
||||
|
||||
Using conditional subtraction ``++if a > b { b - a } else { 0 }++`` can lead to an unintended underflow, which can cause bugs or unexpected behaviors. The ``++saturating_sub++`` method ensures that the subtraction does not underflow by returning zero if the result would have been negative.
|
||||
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
[source,rust,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
let a = 12u32;
|
||||
let b = 13u32;
|
||||
let result = if a > b { b - a } else { 0 }; // Noncompliant: Potential underflow condition.
|
||||
----
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
[source,rust,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
let a = 12u32;
|
||||
let b = 13u32;
|
||||
let result = a.saturating_sub(b); // Compliant: Safe subtraction using saturating_sub.
|
||||
----
|
||||
|
||||
== Resources
|
||||
=== Documentation
|
||||
|
||||
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#inverted_saturating_sub
|
Loading…
x
Reference in New Issue
Block a user