Skip to content

Commit 05800cb

Browse files
github-actions[bot]sallaigyghislainpiot
authored
Create rule S7457: Calls to step_by that always panic should not be made (#4842)
* Create rule S7457 * Update metadata.json * Update rule.adoc * Update metadata.json --------- Co-authored-by: sallaigy <sallaigy@users.noreply.github.com> Co-authored-by: Gyula Sallai <gyula.sallai@sonarsource.com> Co-authored-by: Ghislain Piot <ghislain.piot@sonarsource.com>
1 parent 82356e2 commit 05800cb

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

rules/S7457/metadata.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{
2+
}

rules/S7457/rust/metadata.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"title": "Calls to `step_by` that always panic should not be made",
3+
"type": "BUG",
4+
"status": "ready",
5+
"remediation": {
6+
"func": "Constant\/Issue",
7+
"constantCost": "5min"
8+
},
9+
"tags": [
10+
"clippy"
11+
],
12+
"defaultSeverity": "Blocker",
13+
"ruleSpecification": "RSPEC-7457",
14+
"sqKey": "S7457",
15+
"scope": "All",
16+
"defaultQualityProfiles": ["Sonar way"],
17+
"quickfix": "unknown",
18+
"code": {
19+
"impacts": {
20+
"RELIABILITY": "HIGH"
21+
},
22+
"attribute": "LOGICAL"
23+
}
24+
}

rules/S7457/rust/rule.adoc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
== Why is this an issue?
3+
Calling `.step_by(0)` on an iterator will cause the program to panic. This is likely an oversight and should be corrected to ensure program stability. If the intent is to cause a panic, it is clearer to use `panic!()` directly.
4+
5+
6+
=== Code examples
7+
8+
==== Noncompliant code example
9+
[source,rust,diff-id=1,diff-type=noncompliant]
10+
----
11+
for x in (0..100).step_by(0) { // Noncompliant: This will cause a panic.
12+
// ...
13+
}
14+
----
15+
16+
==== Compliant solution
17+
18+
[source,rust,diff-id=1,diff-type=compliant]
19+
----
20+
for x in (0..100).step_by(1) { // Compliant: Step by a valid positive integer.
21+
// ...
22+
}
23+
----
24+
25+
== Resources
26+
=== Documentation
27+
28+
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#iterator_step_by_zero

0 commit comments

Comments
 (0)