Add run-rustfix to explicit_write test

This commit is contained in:
Wilco Kusee 2019-01-13 12:22:59 +01:00
parent 9f8fb8007c
commit 40d9f1d9f4
3 changed files with 61 additions and 8 deletions

View file

@ -0,0 +1,51 @@
// run-rustfix
#![allow(unused_imports)]
#![warn(clippy::explicit_write)]
fn stdout() -> String {
String::new()
}
fn stderr() -> String {
String::new()
}
fn main() {
// these should warn
{
use std::io::Write;
print!("test");
eprint!("test");
println!("test");
eprintln!("test");
print!("test");
eprint!("test");
// including newlines
println!("test\ntest");
eprintln!("test\ntest");
}
// 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");
}
}

View file

@ -1,3 +1,5 @@
// run-rustfix
#![allow(unused_imports)]
#![warn(clippy::explicit_write)] #![warn(clippy::explicit_write)]
fn stdout() -> String { fn stdout() -> String {

View file

@ -1,5 +1,5 @@
error: use of `write!(stdout(), ...).unwrap()` error: use of `write!(stdout(), ...).unwrap()`
--> $DIR/explicit_write.rs:15:9 --> $DIR/explicit_write.rs:17:9
| |
LL | write!(std::io::stdout(), "test").unwrap(); LL | write!(std::io::stdout(), "test").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `print!("test")` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `print!("test")`
@ -7,43 +7,43 @@ LL | write!(std::io::stdout(), "test").unwrap();
= note: `-D clippy::explicit-write` implied by `-D warnings` = note: `-D clippy::explicit-write` implied by `-D warnings`
error: use of `write!(stderr(), ...).unwrap()` error: use of `write!(stderr(), ...).unwrap()`
--> $DIR/explicit_write.rs:16:9 --> $DIR/explicit_write.rs:18:9
| |
LL | write!(std::io::stderr(), "test").unwrap(); LL | write!(std::io::stderr(), "test").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `eprint!("test")` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `eprint!("test")`
error: use of `writeln!(stdout(), ...).unwrap()` error: use of `writeln!(stdout(), ...).unwrap()`
--> $DIR/explicit_write.rs:17:9 --> $DIR/explicit_write.rs:19:9
| |
LL | writeln!(std::io::stdout(), "test").unwrap(); LL | writeln!(std::io::stdout(), "test").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `println!("test")` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `println!("test")`
error: use of `writeln!(stderr(), ...).unwrap()` error: use of `writeln!(stderr(), ...).unwrap()`
--> $DIR/explicit_write.rs:18:9 --> $DIR/explicit_write.rs:20:9
| |
LL | writeln!(std::io::stderr(), "test").unwrap(); LL | writeln!(std::io::stderr(), "test").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `eprintln!("test")` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `eprintln!("test")`
error: use of `stdout().write_fmt(...).unwrap()` error: use of `stdout().write_fmt(...).unwrap()`
--> $DIR/explicit_write.rs:19:9 --> $DIR/explicit_write.rs:21:9
| |
LL | std::io::stdout().write_fmt(format_args!("test")).unwrap(); LL | std::io::stdout().write_fmt(format_args!("test")).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `print!("test")` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `print!("test")`
error: use of `stderr().write_fmt(...).unwrap()` error: use of `stderr().write_fmt(...).unwrap()`
--> $DIR/explicit_write.rs:20:9 --> $DIR/explicit_write.rs:22:9
| |
LL | std::io::stderr().write_fmt(format_args!("test")).unwrap(); LL | std::io::stderr().write_fmt(format_args!("test")).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `eprint!("test")` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `eprint!("test")`
error: use of `writeln!(stdout(), ...).unwrap()` error: use of `writeln!(stdout(), ...).unwrap()`
--> $DIR/explicit_write.rs:23:9 --> $DIR/explicit_write.rs:25:9
| |
LL | writeln!(std::io::stdout(), "test/ntest").unwrap(); LL | writeln!(std::io::stdout(), "test/ntest").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `println!("test/ntest")` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `println!("test/ntest")`
error: use of `writeln!(stderr(), ...).unwrap()` error: use of `writeln!(stderr(), ...).unwrap()`
--> $DIR/explicit_write.rs:24:9 --> $DIR/explicit_write.rs:26:9
| |
LL | writeln!(std::io::stderr(), "test/ntest").unwrap(); LL | writeln!(std::io::stderr(), "test/ntest").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `eprintln!("test/ntest")` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `eprintln!("test/ntest")`