rust/tests/ui/explicit_write.rs

52 lines
1.6 KiB
Rust
Raw Normal View History

2019-01-13 12:22:59 +01:00
// run-rustfix
#![allow(unused_imports)]
2018-07-28 17:34:52 +02:00
#![warn(clippy::explicit_write)]
2017-10-12 08:18:43 +02:00
fn stdout() -> String {
String::new()
}
fn stderr() -> String {
String::new()
}
fn main() {
// these should warn
{
use std::io::Write;
write!(std::io::stdout(), "test").unwrap();
write!(std::io::stderr(), "test").unwrap();
writeln!(std::io::stdout(), "test").unwrap();
writeln!(std::io::stderr(), "test").unwrap();
std::io::stdout().write_fmt(format_args!("test")).unwrap();
std::io::stderr().write_fmt(format_args!("test")).unwrap();
2018-11-23 08:18:23 +01:00
// including newlines
writeln!(std::io::stdout(), "test\ntest").unwrap();
writeln!(std::io::stderr(), "test\ntest").unwrap();
2017-10-12 08:18:43 +02:00
}
// these should not warn, different destination
{
use std::fmt::Write;
let mut s = String::new();
write!(s, "test").unwrap();
write!(s, "test").unwrap();
writeln!(s, "test").unwrap();
writeln!(s, "test").unwrap();
s.write_fmt(format_args!("test")).unwrap();
s.write_fmt(format_args!("test")).unwrap();
write!(stdout(), "test").unwrap();
write!(stderr(), "test").unwrap();
writeln!(stdout(), "test").unwrap();
writeln!(stderr(), "test").unwrap();
stdout().write_fmt(format_args!("test")).unwrap();
stderr().write_fmt(format_args!("test")).unwrap();
}
// these should not warn, no unwrap
{
use std::io::Write;
std::io::stdout().write_fmt(format_args!("test")).expect("no stdout");
std::io::stderr().write_fmt(format_args!("test")).expect("no stderr");
}
}