Update RSPEC

This commit is contained in:
yassin-kammoun-sonarsource 2025-03-27 10:12:59 +01:00
parent 4be3c6eb3a
commit 65157f31ee
2 changed files with 15 additions and 32 deletions

View File

@ -1,12 +1,13 @@
{
"title": "FIXME",
"type": "CODE_SMELL",
"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",
@ -16,10 +17,8 @@
"quickfix": "unknown",
"code": {
"impacts": {
"MAINTAINABILITY": "HIGH",
"RELIABILITY": "MEDIUM",
"SECURITY": "LOW"
"RELIABILITY": "MEDIUM"
},
"attribute": "CONVENTIONAL"
"attribute": "LOGICAL"
}
}

View File

@ -1,16 +1,6 @@
FIXME: add a description
// If you want to factorize the description uncomment the following line and create the file.
//include::../description.adoc[]
== Why is this an issue?
FIXME: remove the unused optional headers (that are commented out)
//=== What is the potential impact?
== How to fix it
//== How to fix it in FRAMEWORK NAME
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
@ -18,27 +8,21 @@ FIXME: remove the unused optional headers (that are commented out)
[source,rust,diff-id=1,diff-type=noncompliant]
----
FIXME
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]
----
FIXME
let a = 12u32;
let b = 13u32;
let result = a.saturating_sub(b); // Compliant: Safe subtraction using saturating_sub.
----
//=== How does this work?
== Resources
=== Documentation
//=== Pitfalls
//=== Going the extra mile
//== Resources
//=== Documentation
//=== Articles & blog posts
//=== Conference presentations
//=== Standards
//=== External coding guidelines
//=== Benchmarks
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#inverted_saturating_sub