Skip to content

Commit c4c9d9f

Browse files
committed
Add suggestion for explicit_write lint
1 parent e2608fc commit c4c9d9f

File tree

4 files changed

+68
-39
lines changed

4 files changed

+68
-39
lines changed

clippy_lints/src/explicit_write.rs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
use crate::rustc::hir::*;
1111
use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
1212
use crate::rustc::{declare_tool_lint, lint_array};
13-
use crate::utils::opt_def_id;
14-
use crate::utils::{is_expn_of, match_def_path, resolve_node, span_lint};
13+
use crate::syntax::ast::LitKind;
14+
use crate::utils::{is_expn_of, match_def_path, opt_def_id, resolve_node, span_lint, span_lint_and_sugg};
1515
use if_chain::if_chain;
1616

1717
/// **What it does:** Checks for usage of `write!()` / `writeln()!` which can be
@@ -51,6 +51,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
5151
if unwrap_args.len() > 0;
5252
if let ExprKind::MethodCall(ref write_fun, _, ref write_args) =
5353
unwrap_args[0].node;
54+
// Obtain the string that should be printed
55+
if let ExprKind::Call(_, ref output_args) = write_args[1].node;
56+
if let ExprKind::AddrOf(_, ref output_string_expr) = output_args[0].node;
57+
if let ExprKind::Array(ref string_exprs) = output_string_expr.node;
58+
if let ExprKind::Lit(ref lit) = string_exprs[0].node;
59+
if let LitKind::Str(ref write_output, _) = lit.node;
5460
if write_fun.ident.name == "write_fmt";
5561
// match calls to std::io::stdout() / std::io::stderr ()
5662
if write_args.len() > 0;
@@ -81,29 +87,35 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
8187
} else {
8288
""
8389
};
90+
91+
// We need to remove the last trailing newline from the string because the
92+
// underlying `fmt::write` function doesn't know wether `println!` or `print!` was
93+
// used.
94+
let mut write_output: String = write_output.to_string();
95+
if write_output.ends_with('\n') {
96+
write_output.truncate(write_output.len() - 1)
97+
}
8498
if let Some(macro_name) = calling_macro {
85-
span_lint(
99+
span_lint_and_sugg(
86100
cx,
87101
EXPLICIT_WRITE,
88102
expr.span,
89103
&format!(
90-
"use of `{}!({}(), ...).unwrap()`. Consider using `{}{}!` instead",
104+
"use of `{}!({}(), ...).unwrap()`",
91105
macro_name,
92-
dest_name,
93-
prefix,
94-
macro_name.replace("write", "print")
95-
)
106+
dest_name
107+
),
108+
"try this",
109+
format!("{}{}!(\"{}\")", prefix, macro_name.replace("write", "print"), write_output.escape_default())
96110
);
97111
} else {
98-
span_lint(
112+
span_lint_and_sugg(
99113
cx,
100114
EXPLICIT_WRITE,
101115
expr.span,
102-
&format!(
103-
"use of `{}().write_fmt(...).unwrap()`. Consider using `{}print!` instead",
104-
dest_name,
105-
prefix,
106-
)
116+
&format!("use of `{}().write_fmt(...).unwrap()`", dest_name),
117+
"try this",
118+
format!("{}print!(\"{}\")", prefix, write_output.escape_default())
107119
);
108120
}
109121
}

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#![feature(slice_patterns)]
1515
#![feature(stmt_expr_attributes)]
1616
#![feature(range_contains)]
17+
#![feature(str_escape)]
1718
#![allow(clippy::missing_docs_in_private_items)]
1819
#![recursion_limit = "256"]
1920
#![warn(rust_2018_idioms, trivial_casts, trivial_numeric_casts)]

tests/ui/explicit_write.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ fn main() {
2727
writeln!(std::io::stderr(), "test").unwrap();
2828
std::io::stdout().write_fmt(format_args!("test")).unwrap();
2929
std::io::stderr().write_fmt(format_args!("test")).unwrap();
30+
31+
// including newlines
32+
writeln!(std::io::stdout(), "test\ntest").unwrap();
33+
writeln!(std::io::stderr(), "test\ntest").unwrap();
3034
}
3135
// these should not warn, different destination
3236
{

tests/ui/explicit_write.stderr

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,52 @@
1-
error: use of `write!(stdout(), ...).unwrap()`. Consider using `print!` instead
2-
--> $DIR/explicit_write.rs:24:9
1+
error: use of `write!(stdout(), ...).unwrap()`
2+
--> $DIR/explicit_write.rs:28:9
33
|
4-
24 | write!(std::io::stdout(), "test").unwrap();
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
28 | write!(std::io::stdout(), "test").unwrap();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `print!("test")`
66
|
77
= note: `-D clippy::explicit-write` implied by `-D warnings`
88

9-
error: use of `write!(stderr(), ...).unwrap()`. Consider using `eprint!` instead
10-
--> $DIR/explicit_write.rs:25:9
9+
error: use of `write!(stderr(), ...).unwrap()`
10+
--> $DIR/explicit_write.rs:29:9
1111
|
12-
25 | write!(std::io::stderr(), "test").unwrap();
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
29 | write!(std::io::stderr(), "test").unwrap();
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `eprint!("test")`
1414

15-
error: use of `writeln!(stdout(), ...).unwrap()`. Consider using `println!` instead
16-
--> $DIR/explicit_write.rs:26:9
15+
error: use of `writeln!(stdout(), ...).unwrap()`
16+
--> $DIR/explicit_write.rs:30:9
1717
|
18-
26 | writeln!(std::io::stdout(), "test").unwrap();
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
30 | writeln!(std::io::stdout(), "test").unwrap();
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `println!("test")`
2020

21-
error: use of `writeln!(stderr(), ...).unwrap()`. Consider using `eprintln!` instead
22-
--> $DIR/explicit_write.rs:27:9
21+
error: use of `writeln!(stderr(), ...).unwrap()`
22+
--> $DIR/explicit_write.rs:31:9
2323
|
24-
27 | writeln!(std::io::stderr(), "test").unwrap();
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
31 | writeln!(std::io::stderr(), "test").unwrap();
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `eprintln!("test")`
2626

27-
error: use of `stdout().write_fmt(...).unwrap()`. Consider using `print!` instead
28-
--> $DIR/explicit_write.rs:28:9
27+
error: use of `stdout().write_fmt(...).unwrap()`
28+
--> $DIR/explicit_write.rs:32:9
2929
|
30-
28 | std::io::stdout().write_fmt(format_args!("test")).unwrap();
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
32 | std::io::stdout().write_fmt(format_args!("test")).unwrap();
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `print!("test")`
3232

33-
error: use of `stderr().write_fmt(...).unwrap()`. Consider using `eprint!` instead
34-
--> $DIR/explicit_write.rs:29:9
33+
error: use of `stderr().write_fmt(...).unwrap()`
34+
--> $DIR/explicit_write.rs:33:9
35+
|
36+
33 | std::io::stderr().write_fmt(format_args!("test")).unwrap();
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `eprint!("test")`
38+
39+
error: use of `writeln!(stdout(), ...).unwrap()`
40+
--> $DIR/explicit_write.rs:36:9
41+
|
42+
36 | writeln!(std::io::stdout(), "test/ntest").unwrap();
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `println!("test/ntest")`
44+
45+
error: use of `writeln!(stderr(), ...).unwrap()`
46+
--> $DIR/explicit_write.rs:37:9
3547
|
36-
29 | std::io::stderr().write_fmt(format_args!("test")).unwrap();
37-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
37 | writeln!(std::io::stderr(), "test/ntest").unwrap();
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `eprintln!("test/ntest")`
3850

39-
error: aborting due to 6 previous errors
51+
error: aborting due to 8 previous errors
4052

0 commit comments

Comments
 (0)