From 5ea6185da4e8b40da40033023a2577552c57cb09 Mon Sep 17 00:00:00 2001 From: Gyula Sallai Date: Wed, 26 Mar 2025 13:32:44 +0100 Subject: [PATCH] Update rule.adoc --- rules/S7455/rust/rule.adoc | 44 +++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/rules/S7455/rust/rule.adoc b/rules/S7455/rust/rule.adoc index 368fb2954e..edc8fe0b6f 100644 --- a/rules/S7455/rust/rule.adoc +++ b/rules/S7455/rust/rule.adoc @@ -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