]> git.lizzy.rs Git - rust.git/blob - tests/ui/explicit_write.rs
Merge branch 'master' into rustfmt_tests
[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     // these should not warn, different destination
32     {
33         use std::fmt::Write;
34         let mut s = String::new();
35         write!(s, "test").unwrap();
36         write!(s, "test").unwrap();
37         writeln!(s, "test").unwrap();
38         writeln!(s, "test").unwrap();
39         s.write_fmt(format_args!("test")).unwrap();
40         s.write_fmt(format_args!("test")).unwrap();
41         write!(stdout(), "test").unwrap();
42         write!(stderr(), "test").unwrap();
43         writeln!(stdout(), "test").unwrap();
44         writeln!(stderr(), "test").unwrap();
45         stdout().write_fmt(format_args!("test")).unwrap();
46         stderr().write_fmt(format_args!("test")).unwrap();
47     }
48     // these should not warn, no unwrap
49     {
50         use std::io::Write;
51         std::io::stdout().write_fmt(format_args!("test")).expect("no stdout");
52         std::io::stderr().write_fmt(format_args!("test")).expect("no stderr");
53     }
54 }