rspec/rules/S7440/rust/rule.adoc

39 lines
850 B
Plaintext
Raw Permalink Normal View History

== Why is this an issue?
Recursive calls in formatting trait implementations (e.g., `Display`) will lead to infinite recursion and a stack overflow.
=== Code examples
==== Noncompliant code example
[source,rust,diff-id=1,diff-type=noncompliant]
----
use std::fmt;
struct Structure(i32);
impl fmt::Display for Structure {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.to_string()) // Noncompliant
}
}
----
==== Compliant solution
[source,rust,diff-id=1,diff-type=compliant]
----
use std::fmt;
struct Structure(i32);
impl fmt::Display for Structure {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0) // Compliant
}
}
----
== Resources
=== Documentation
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#recursive_format_impl