]> git.lizzy.rs Git - rust.git/blob - tests/ui/explicit_write.rs
Auto merge of #3450 - phansch:structured_sugg_for_explicit_write, r=flip1995
[rust.git] / tests / ui / explicit_write.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![warn(clippy::explicit_write)]
11
12 fn stdout() -> String {
13     String::new()
14 }
15
16 fn stderr() -> String {
17     String::new()
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     // these should not warn, different destination
36     {
37         use std::fmt::Write;
38         let mut s = String::new();
39         write!(s, "test").unwrap();
40         write!(s, "test").unwrap();
41         writeln!(s, "test").unwrap();
42         writeln!(s, "test").unwrap();
43         s.write_fmt(format_args!("test")).unwrap();
44         s.write_fmt(format_args!("test")).unwrap();
45         write!(stdout(), "test").unwrap();
46         write!(stderr(), "test").unwrap();
47         writeln!(stdout(), "test").unwrap();
48         writeln!(stderr(), "test").unwrap();
49         stdout().write_fmt(format_args!("test")).unwrap();
50         stderr().write_fmt(format_args!("test")).unwrap();
51     }
52     // these should not warn, no unwrap
53     {
54         use std::io::Write;
55         std::io::stdout().write_fmt(format_args!("test")).expect("no stdout");
56         std::io::stderr().write_fmt(format_args!("test")).expect("no stderr");
57     }
58 }