RS.CLIPPY.RECURSIVE_FORMAT_IMPL
Format trait method called while implementing the same Format trait
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: recursive_format_impl. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Checks for format trait implementations (e.g. Display) with a recursive call to itself
which uses self as a parameter.
This is typically done indirectly with the write! macro or with to_string().
Why is this bad?
This will lead to infinite recursion and a stack overflow.
Example
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())
}
}
Use instead:
use std::fmt;
struct Structure(i32);
impl fmt::Display for Structure {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
Past names
- to_string_in_display