]> git.lizzy.rs Git - rust.git/blob - tests/ui/explicit_write.rs
Stabilize tool lints
[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
11
12
13 #![warn(clippy::explicit_write)]
14
15
16 fn stdout() -> String {
17     String::new()
18 }
19
20 fn stderr() -> String {
21     String::new()
22 }
23
24 fn main() {
25     // these should warn
26     {
27         use std::io::Write;
28         write!(std::io::stdout(), "test").unwrap();
29         write!(std::io::stderr(), "test").unwrap();
30         writeln!(std::io::stdout(), "test").unwrap();
31         writeln!(std::io::stderr(), "test").unwrap();
32         std::io::stdout().write_fmt(format_args!("test")).unwrap();
33         std::io::stderr().write_fmt(format_args!("test")).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 }