Create rule S7432: Reversed ranges and slices should not be empty (#4773)
* Create rule S7432 * Update RSPEC --------- 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
26f042cc83
commit
6edd31ee99
2
rules/S7432/metadata.json
Normal file
2
rules/S7432/metadata.json
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
{
|
||||||
|
}
|
24
rules/S7432/rust/metadata.json
Normal file
24
rules/S7432/rust/metadata.json
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"title": "Reversed ranges and slices should not be empty",
|
||||||
|
"type": "BUG",
|
||||||
|
"status": "ready",
|
||||||
|
"remediation": {
|
||||||
|
"func": "Constant\/Issue",
|
||||||
|
"constantCost": "5min"
|
||||||
|
},
|
||||||
|
"tags": [
|
||||||
|
"clippy"
|
||||||
|
],
|
||||||
|
"defaultSeverity": "Major",
|
||||||
|
"ruleSpecification": "RSPEC-7432",
|
||||||
|
"sqKey": "S7432",
|
||||||
|
"scope": "All",
|
||||||
|
"defaultQualityProfiles": ["Sonar way"],
|
||||||
|
"quickfix": "unknown",
|
||||||
|
"code": {
|
||||||
|
"impacts": {
|
||||||
|
"RELIABILITY": "MEDIUM"
|
||||||
|
},
|
||||||
|
"attribute": "LOGICAL"
|
||||||
|
}
|
||||||
|
}
|
32
rules/S7432/rust/rule.adoc
Normal file
32
rules/S7432/rust/rule.adoc
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
== Why is this an issue?
|
||||||
|
|
||||||
|
Empty ranges result in no iterations, making any loop a no-op. Trying to index slices using reversed ranges will result in runtime panics. Both scenarios lead to bugs or logical errors in the code.
|
||||||
|
|
||||||
|
=== Code examples
|
||||||
|
|
||||||
|
==== Noncompliant code example
|
||||||
|
|
||||||
|
[source,rust,diff-id=1,diff-type=noncompliant]
|
||||||
|
----
|
||||||
|
fn main() {
|
||||||
|
(10..=0).for_each(|x| println!("{}", x)); // Noncompliant: Empty range
|
||||||
|
let arr = [1, 2, 3, 4, 5];
|
||||||
|
let sub = &arr[3..1]; // Noncompliant: Reversed slice indexing
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
==== Compliant solution
|
||||||
|
|
||||||
|
[source,rust,diff-id=1,diff-type=compliant]
|
||||||
|
----
|
||||||
|
fn main() {
|
||||||
|
(0..=10).rev().for_each(|x| println!("{}", x)); // Compliant: Properly reversed range for iteration
|
||||||
|
let arr = [1, 2, 3, 4, 5];
|
||||||
|
let sub = &arr[1..3]; // Compliant: Valid slice indexing
|
||||||
|
}
|
||||||
|
----
|
||||||
|
|
||||||
|
== Resources
|
||||||
|
=== Documentation
|
||||||
|
|
||||||
|
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#reversed_empty_ranges
|
Loading…
x
Reference in New Issue
Block a user