From e31a0941e20851933eb80b14da8d2a11b6f614b1 Mon Sep 17 00:00:00 2001 From: Devon Hollowood Date: Thu, 12 Oct 2017 05:53:20 -0300 Subject: [PATCH] Fix output for write macros --- clippy_lints/src/suggest_print.rs | 23 +++++++++++----------- tests/ui/suggest_print.stderr | 32 +++++++++++++++---------------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/clippy_lints/src/suggest_print.rs b/clippy_lints/src/suggest_print.rs index 1bc58297476f..bf202b20057f 100644 --- a/clippy_lints/src/suggest_print.rs +++ b/clippy_lints/src/suggest_print.rs @@ -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, ) diff --git a/tests/ui/suggest_print.stderr b/tests/ui/suggest_print.stderr index f83f6cf7ad35..bae9f6f26795 100644 --- a/tests/ui/suggest_print.stderr +++ b/tests/ui/suggest_print.stderr @@ -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(); - | ^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^