1
Fork 0

Fix output for write macros

This commit is contained in:
Devon Hollowood 2017-10-12 05:53:20 -03:00
parent 4105593eee
commit e31a0941e2
2 changed files with 28 additions and 27 deletions

View file

@ -56,14 +56,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
None
},
], {
let dest_expr = &write_args[0];
let (span, calling_macro) =
if let Some(span) = is_expn_of(dest_expr.span, "write") {
(span, Some("write"))
} else if let Some(span) = is_expn_of(dest_expr.span, "writeln") {
(span, Some("writeln"))
let write_span = unwrap_args[0].span;
let calling_macro =
// ordering is important here, since `writeln!` uses `write!` internally
if is_expn_of(write_span, "writeln").is_some() {
Some("writeln")
} else if is_expn_of(write_span, "write").is_some() {
Some("write")
} else {
(dest_expr.span, None)
None
};
let prefix = if dest_name == "stderr" {
"e"
@ -74,9 +75,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
span_lint(
cx,
SUGGEST_PRINT,
span,
expr.span,
&format!(
"use of `{}!({}, ...).unwrap()`. Consider using `{}{}!` instead",
"use of `{}!({}(), ...).unwrap()`. Consider using `{}{}!` instead",
macro_name,
dest_name,
prefix,
@ -87,9 +88,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
span_lint(
cx,
SUGGEST_PRINT,
span,
expr.span,
&format!(
"use of `{}.write_fmt(...).unwrap()`. Consider using `{}print!` instead",
"use of `{}().write_fmt(...).unwrap()`. Consider using `{}print!` instead",
dest_name,
prefix,
)

View file

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