Merge pull request #2131 from devonhollowood/suggest-print

Suggest print
This commit is contained in:
Oliver Schneider 2017-10-17 15:26:41 +02:00 committed by GitHub
commit f01e45faa4
4 changed files with 188 additions and 0 deletions

View file

@ -0,0 +1,46 @@
#![warn(explicit_write)]
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();
}
// 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

@ -0,0 +1,38 @@
error: use of `write!(stdout(), ...).unwrap()`. Consider using `print!` instead
--> $DIR/explicit_write.rs:16:9
|
16 | write!(std::io::stdout(), "test").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D explicit-write` implied by `-D warnings`
error: use of `write!(stderr(), ...).unwrap()`. Consider using `eprint!` instead
--> $DIR/explicit_write.rs:17:9
|
17 | write!(std::io::stderr(), "test").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: use of `writeln!(stdout(), ...).unwrap()`. Consider using `println!` instead
--> $DIR/explicit_write.rs:18:9
|
18 | writeln!(std::io::stdout(), "test").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: use of `writeln!(stderr(), ...).unwrap()`. Consider using `eprintln!` instead
--> $DIR/explicit_write.rs:19:9
|
19 | writeln!(std::io::stderr(), "test").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: use of `stdout().write_fmt(...).unwrap()`. Consider using `print!` instead
--> $DIR/explicit_write.rs:20:9
|
20 | std::io::stdout().write_fmt(format_args!("test")).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: use of `stderr().write_fmt(...).unwrap()`. Consider using `eprint!` instead
--> $DIR/explicit_write.rs:21:9
|
21 | std::io::stderr().write_fmt(format_args!("test")).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^