summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/print_in_format_impl.rs
blob: 64e8868660980fb1c62a20456e7f438d2d9e6225 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#![allow(unused, clippy::print_literal, clippy::write_literal)]
#![warn(clippy::print_in_format_impl)]
use std::fmt::{Debug, Display, Error, Formatter};

macro_rules! indirect {
    () => {{ println!() }};
}

macro_rules! nested {
    ($($tt:tt)*) => {
        $($tt)*
    };
}

struct Foo;
impl Debug for Foo {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        static WORKS_WITH_NESTED_ITEMS: bool = true;

        print!("{}", 1);
        println!("{}", 2);
        eprint!("{}", 3);
        eprintln!("{}", 4);
        nested! {
            println!("nested");
        };

        write!(f, "{}", 5);
        writeln!(f, "{}", 6);
        indirect!();

        Ok(())
    }
}

impl Display for Foo {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        print!("Display");
        write!(f, "Display");

        Ok(())
    }
}

struct UnnamedFormatter;
impl Debug for UnnamedFormatter {
    fn fmt(&self, _: &mut Formatter) -> Result<(), Error> {
        println!("UnnamedFormatter");
        Ok(())
    }
}

fn main() {
    print!("outside fmt");
    println!("outside fmt");
    eprint!("outside fmt");
    eprintln!("outside fmt");
}