]> git.lizzy.rs Git - rust.git/blob - src/docs/print_in_format_impl.txt
Auto merge of #9421 - xphoniex:fix-#9420, r=giraffate
[rust.git] / src / docs / print_in_format_impl.txt
1 ### What it does
2 Checks for use of `println`, `print`, `eprintln` or `eprint` in an
3 implementation of a formatting trait.
4
5 ### Why is this bad?
6 Using a print macro is likely unintentional since formatting traits
7 should write to the `Formatter`, not stdout/stderr.
8
9 ### Example
10 ```
11 use std::fmt::{Display, Error, Formatter};
12
13 struct S;
14 impl Display for S {
15     fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
16         println!("S");
17
18         Ok(())
19     }
20 }
21 ```
22 Use instead:
23 ```
24 use std::fmt::{Display, Error, Formatter};
25
26 struct S;
27 impl Display for S {
28     fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
29         writeln!(f, "S");
30
31         Ok(())
32     }
33 }
34 ```