]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/explicit_write.rs
New upstream version 1.60.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / explicit_write.rs
CommitLineData
f20569fa
XL
1// run-rustfix
2#![allow(unused_imports)]
3#![warn(clippy::explicit_write)]
4
5fn stdout() -> String {
6 String::new()
7}
8
9fn stderr() -> String {
10 String::new()
11}
12
5099ac24
FG
13macro_rules! one {
14 () => {
15 1
16 };
17}
18
f20569fa
XL
19fn main() {
20 // these should warn
21 {
22 use std::io::Write;
23 write!(std::io::stdout(), "test").unwrap();
24 write!(std::io::stderr(), "test").unwrap();
25 writeln!(std::io::stdout(), "test").unwrap();
26 writeln!(std::io::stderr(), "test").unwrap();
27 std::io::stdout().write_fmt(format_args!("test")).unwrap();
28 std::io::stderr().write_fmt(format_args!("test")).unwrap();
29
30 // including newlines
31 writeln!(std::io::stdout(), "test\ntest").unwrap();
32 writeln!(std::io::stderr(), "test\ntest").unwrap();
5099ac24
FG
33
34 let value = 1;
35 writeln!(std::io::stderr(), "with {}", value).unwrap();
36 writeln!(std::io::stderr(), "with {} {}", 2, value).unwrap();
37 writeln!(std::io::stderr(), "with {value}").unwrap();
38 writeln!(std::io::stderr(), "macro arg {}", one!()).unwrap();
f20569fa
XL
39 }
40 // these should not warn, different destination
41 {
42 use std::fmt::Write;
43 let mut s = String::new();
44 write!(s, "test").unwrap();
45 write!(s, "test").unwrap();
46 writeln!(s, "test").unwrap();
47 writeln!(s, "test").unwrap();
48 s.write_fmt(format_args!("test")).unwrap();
49 s.write_fmt(format_args!("test")).unwrap();
50 write!(stdout(), "test").unwrap();
51 write!(stderr(), "test").unwrap();
52 writeln!(stdout(), "test").unwrap();
53 writeln!(stderr(), "test").unwrap();
54 stdout().write_fmt(format_args!("test")).unwrap();
55 stderr().write_fmt(format_args!("test")).unwrap();
56 }
57 // these should not warn, no unwrap
58 {
59 use std::io::Write;
60 std::io::stdout().write_fmt(format_args!("test")).expect("no stdout");
61 std::io::stderr().write_fmt(format_args!("test")).expect("no stderr");
62 }
63}