Update rule.adoc
This commit is contained in:
parent
bd3afae6b8
commit
5ea6185da4
@ -1,44 +1,40 @@
|
||||
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?
|
||||
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.
|
||||
|
||||
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
|
||||
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
[source,rust,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
FIXME
|
||||
for value in iterator.next() {
|
||||
// Noncompliant: Looping directly over `iterator.next()`
|
||||
println!("{value}");
|
||||
}
|
||||
----
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
[source,rust,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
FIXME
|
||||
for value in iterator {
|
||||
// Compliant: Looping directly over `iterator`
|
||||
println!("{value}");
|
||||
}
|
||||
----
|
||||
|
||||
//=== How does this work?
|
||||
Alternatively, handling the Option:
|
||||
|
||||
//=== Pitfalls
|
||||
[source,rust,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
if let Some(value) = iterator.next() {
|
||||
// Process the single value
|
||||
println!("{value}");
|
||||
}
|
||||
----
|
||||
|
||||
//=== Going the extra mile
|
||||
== Resources
|
||||
=== Documentation
|
||||
|
||||
|
||||
//== Resources
|
||||
//=== Documentation
|
||||
//=== Articles & blog posts
|
||||
//=== Conference presentations
|
||||
//=== Standards
|
||||
//=== External coding guidelines
|
||||
//=== Benchmarks
|
||||
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#iter_next_loop
|
||||
|
Loading…
x
Reference in New Issue
Block a user