summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/tests/ui/explicit_write.rs
blob: e7a698d3e012d35049febcde9222b519904e06e8 (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
59
60
61
62
63
// run-rustfix
#![allow(unused_imports)]
#![warn(clippy::explicit_write)]

fn stdout() -> String {
    String::new()
}

fn stderr() -> String {
    String::new()
}

macro_rules! one {
    () => {
        1
    };
}

fn main() {
    // these should warn
    {
        use std::io::Write;
        write!(std::io::stdout(), "test").unwrap();
        write!(std::io::stderr(), "test").unwrap();
        writeln!(std::io::stdout(), "test").unwrap();
        writeln!(std::io::stderr(), "test").unwrap();
        std::io::stdout().write_fmt(format_args!("test")).unwrap();
        std::io::stderr().write_fmt(format_args!("test")).unwrap();

        // including newlines
        writeln!(std::io::stdout(), "test\ntest").unwrap();
        writeln!(std::io::stderr(), "test\ntest").unwrap();

        let value = 1;
        writeln!(std::io::stderr(), "with {}", value).unwrap();
        writeln!(std::io::stderr(), "with {} {}", 2, value).unwrap();
        writeln!(std::io::stderr(), "with {value}").unwrap();
        writeln!(std::io::stderr(), "macro arg {}", one!()).unwrap();
    }
    // these should not warn, different destination
    {
        use std::fmt::Write;
        let mut s = String::new();
        write!(s, "test").unwrap();
        write!(s, "test").unwrap();
        writeln!(s, "test").unwrap();
        writeln!(s, "test").unwrap();
        s.write_fmt(format_args!("test")).unwrap();
        s.write_fmt(format_args!("test")).unwrap();
        write!(stdout(), "test").unwrap();
        write!(stderr(), "test").unwrap();
        writeln!(stdout(), "test").unwrap();
        writeln!(stderr(), "test").unwrap();
        stdout().write_fmt(format_args!("test")).unwrap();
        stderr().write_fmt(format_args!("test")).unwrap();
    }
    // these should not warn, no unwrap
    {
        use std::io::Write;
        std::io::stdout().write_fmt(format_args!("test")).expect("no stdout");
        std::io::stderr().write_fmt(format_args!("test")).expect("no stderr");
    }
}