Compare commits

...

3 Commits

Author SHA1 Message Date
Gyula Sallai
5ea6185da4
Update rule.adoc 2025-03-26 13:32:44 +01:00
Gyula Sallai
bd3afae6b8
Update metadata.json 2025-03-26 13:26:46 +01:00
sallaigy
e3e3696a37 Create rule S7455 2025-03-26 12:21:38 +00:00
3 changed files with 66 additions and 0 deletions

View File

@ -0,0 +1,2 @@
{
}

View File

@ -0,0 +1,24 @@
{
"title": "The return value of `next` should not be looped over",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"clippy"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7455",
"sqKey": "S7455",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"RELIABILITY": "MEDIUM"
},
"attribute": "LOGICAL"
}
}

View File

@ -0,0 +1,40 @@
== Why is this an issue?
Looping over `x.next()` is misleading and can introduce subtle bugs. `x.next()` returns an `Option` which should be handled properly. When used in a loop, `Option` implements `IntoIterator`, resulting in unexpected behavior where only a single element or `None` might be processed, leading to difficult-to-diagnose errors.
=== Code examples
==== Noncompliant code example
[source,rust,diff-id=1,diff-type=noncompliant]
----
for value in iterator.next() {
// Noncompliant: Looping directly over `iterator.next()`
println!("{value}");
}
----
==== Compliant solution
[source,rust,diff-id=1,diff-type=compliant]
----
for value in iterator {
// Compliant: Looping directly over `iterator`
println!("{value}");
}
----
Alternatively, handling the Option:
[source,rust,diff-id=1,diff-type=compliant]
----
if let Some(value) = iterator.next() {
// Process the single value
println!("{value}");
}
----
== Resources
=== Documentation
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#iter_next_loop