Compare commits
3 Commits
master
...
rule/add-R
Author | SHA1 | Date | |
---|---|---|---|
![]() |
5ea6185da4 | ||
![]() |
bd3afae6b8 | ||
![]() |
e3e3696a37 |
2
rules/S7455/metadata.json
Normal file
2
rules/S7455/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
24
rules/S7455/rust/metadata.json
Normal file
24
rules/S7455/rust/metadata.json
Normal 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"
|
||||
}
|
||||
}
|
40
rules/S7455/rust/rule.adoc
Normal file
40
rules/S7455/rust/rule.adoc
Normal 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
|
Loading…
x
Reference in New Issue
Block a user