rspec/rules/S7464/rust/rule.adoc
yassin-kammoun-sonarsource 9f6baeaede Update RSPEC
2025-03-27 11:04:51 +01:00

29 lines
859 B
Plaintext

== Why is this an issue?
Infinite iteration is usually an error in most cases, leading to programs that never terminate. While there are some acceptable use cases such as event streams, developers often unintentionally create infinite loops, causing resource exhaustion or unresponsive behavior.
=== Code examples
==== Noncompliant code example
[source,rust,diff-id=1,diff-type=noncompliant]
----
use std::iter;
iter::repeat(1_u8).collect::<Vec<_>>(); // Noncompliant: This creates an infinite iterator.
----
==== Compliant solution
[source,rust,diff-id=1,diff-type=compliant]
----
use std::iter;
iter::repeat(1_u8).take(5).collect::<Vec<_>>(); // Compliant: This creates a finite iterator by taking only the first 5 items.
----
== Resources
=== Documentation
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#infinite_iter