]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/explicit_write.rs
Rollup merge of #102577 - kornelski:non-code-visual-studio, r=wesleywiser
[rust.git] / src / tools / clippy / tests / ui / explicit_write.rs
1 // run-rustfix
2 #![warn(clippy::explicit_write)]
3 #![allow(unused_imports)]
4 #![allow(clippy::uninlined_format_args)]
5
6 fn stdout() -> String {
7     String::new()
8 }
9
10 fn stderr() -> String {
11     String::new()
12 }
13
14 macro_rules! one {
15     () => {
16         1
17     };
18 }
19
20 fn main() {
21     // these should warn
22     {
23         use std::io::Write;
24         write!(std::io::stdout(), "test").unwrap();
25         write!(std::io::stderr(), "test").unwrap();
26         writeln!(std::io::stdout(), "test").unwrap();
27         writeln!(std::io::stderr(), "test").unwrap();
28         std::io::stdout().write_fmt(format_args!("test")).unwrap();
29         std::io::stderr().write_fmt(format_args!("test")).unwrap();
30
31         // including newlines
32         writeln!(std::io::stdout(), "test\ntest").unwrap();
33         writeln!(std::io::stderr(), "test\ntest").unwrap();
34
35         let value = 1;
36         writeln!(std::io::stderr(), "with {}", value).unwrap();
37         writeln!(std::io::stderr(), "with {} {}", 2, value).unwrap();
38         writeln!(std::io::stderr(), "with {value}").unwrap();
39         writeln!(std::io::stderr(), "macro arg {}", one!()).unwrap();
40         let width = 2;
41         writeln!(std::io::stderr(), "{:w$}", value, w = width).unwrap();
42     }
43     // these should not warn, different destination
44     {
45         use std::fmt::Write;
46         let mut s = String::new();
47         write!(s, "test").unwrap();
48         write!(s, "test").unwrap();
49         writeln!(s, "test").unwrap();
50         writeln!(s, "test").unwrap();
51         s.write_fmt(format_args!("test")).unwrap();
52         s.write_fmt(format_args!("test")).unwrap();
53         write!(stdout(), "test").unwrap();
54         write!(stderr(), "test").unwrap();
55         writeln!(stdout(), "test").unwrap();
56         writeln!(stderr(), "test").unwrap();
57         stdout().write_fmt(format_args!("test")).unwrap();
58         stderr().write_fmt(format_args!("test")).unwrap();
59     }
60     // these should not warn, no unwrap
61     {
62         use std::io::Write;
63         std::io::stdout().write_fmt(format_args!("test")).expect("no stdout");
64         std::io::stderr().write_fmt(format_args!("test")).expect("no stderr");
65     }
66 }