![github-actions[bot]](/assets/img/avatar_default.png)
* Create rule S7440 * Update RSPEC --------- Co-authored-by: yassin-kammoun-sonarsource <yassin-kammoun-sonarsource@users.noreply.github.com> Co-authored-by: yassin-kammoun-sonarsource <yassin.kammoun@sonarsource.com>
39 lines
850 B
Plaintext
39 lines
850 B
Plaintext
== 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
|