]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/src/docs/recursive_format_impl.txt
Auto merge of #99099 - Stargateur:phantomdata_debug, r=joshtriplett
[rust.git] / src / tools / clippy / src / docs / recursive_format_impl.txt
1 ### What it does
2 Checks for format trait implementations (e.g. `Display`) with a recursive call to itself
3 which uses `self` as a parameter.
4 This is typically done indirectly with the `write!` macro or with `to_string()`.
5
6 ### Why is this bad?
7 This will lead to infinite recursion and a stack overflow.
8
9 ### Example
10
11 ```
12 use std::fmt;
13
14 struct Structure(i32);
15 impl fmt::Display for Structure {
16     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
17         write!(f, "{}", self.to_string())
18     }
19 }
20
21 ```
22 Use instead:
23 ```
24 use std::fmt;
25
26 struct Structure(i32);
27 impl fmt::Display for Structure {
28     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29         write!(f, "{}", self.0)
30     }
31 }
32 ```