Skip to content

Commit

Permalink
resolve rust-lang#4542 by removing machine applicable suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshMcguigan committed Sep 17, 2019
1 parent 535bc1d commit fcca562
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
3 changes: 2 additions & 1 deletion clippy_lints/src/explicit_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ fn write_output_string(write_args: &HirVec<Expr>) -> Option<String> {
if output_args.len() > 0;
if let ExprKind::AddrOf(_, ref output_string_expr) = output_args[0].node;
if let ExprKind::Array(ref string_exprs) = output_string_expr.node;
if string_exprs.len() > 0;
// we only want to provide an automatic suggestion for simple (non-format) strings
if string_exprs.len() == 1;
if let ExprKind::Lit(ref lit) = string_exprs[0].node;
if let LitKind::Str(ref write_output, _) = lit.node;
then {
Expand Down
6 changes: 5 additions & 1 deletion tests/ui/explicit_write.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// run-rustfix
#![allow(unused_imports)]
#![allow(unused_imports, clippy::blacklisted_name)]
#![warn(clippy::explicit_write)]

fn stdout() -> String {
Expand All @@ -24,6 +24,10 @@ fn main() {
// including newlines
println!("test\ntest");
eprintln!("test\ntest");

// including formatting
let bar = "bar";
writeln!(std::io::stderr(), "foo {}", bar).unwrap();
}
// these should not warn, different destination
{
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/explicit_write.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,11 @@ error: use of `writeln!(stderr(), ...).unwrap()`
LL | writeln!(std::io::stderr(), "test/ntest").unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `eprintln!("test/ntest")`

error: aborting due to 8 previous errors
error: use of `writeln!(stderr(), ...).unwrap()`. Consider using `eprintln!` instead
--> $DIR/explicit_write.rs:30:9
|
LL | writeln!(std::io::stderr(), "foo {}", bar).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 9 previous errors

7 changes: 7 additions & 0 deletions tests/ui/explicit_write_non_rustfix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![allow(unused_imports, clippy::blacklisted_name)]
#![warn(clippy::explicit_write)]

fn main() {
let bar = "bar";
writeln!(std::io::stderr(), "foo {}", bar).unwrap();
}

0 comments on commit fcca562

Please sign in to comment.