]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/explicit_write.fixed
Rollup merge of #102581 - jyn514:src-detection, r=Mark-Simulacrum
[rust.git] / src / tools / clippy / tests / ui / explicit_write.fixed
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         print!("test");
25         eprint!("test");
26         println!("test");
27         eprintln!("test");
28         print!("test");
29         eprint!("test");
30
31         // including newlines
32         println!("test\ntest");
33         eprintln!("test\ntest");
34
35         let value = 1;
36         eprintln!("with {}", value);
37         eprintln!("with {} {}", 2, value);
38         eprintln!("with {value}");
39         eprintln!("macro arg {}", one!());
40         let width = 2;
41         eprintln!("{:w$}", value, w = width);
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 }