Skip to content

Commit 7063c36

Browse files
justjosiasebroto
authored andcommitted
Add eprint! to print_with_newline lint
1 parent b04bfbd commit 7063c36

File tree

3 files changed

+190
-0
lines changed

3 files changed

+190
-0
lines changed

clippy_lints/src/write.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,26 @@ impl EarlyLintPass for Write {
283283
span_lint(cx, PRINT_STDERR, mac.span(), "use of `eprintln!`");
284284
} else if mac.path == sym!(eprint) {
285285
span_lint(cx, PRINT_STDERR, mac.span(), "use of `eprint!`");
286+
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), false) {
287+
if check_newlines(&fmt_str) {
288+
span_lint_and_then(
289+
cx,
290+
PRINT_WITH_NEWLINE,
291+
mac.span(),
292+
"using `eprint!()` with a format string that ends in a single newline",
293+
|err| {
294+
err.multipart_suggestion(
295+
"use `eprintln!` instead",
296+
vec![
297+
(mac.path.span, String::from("eprintln")),
298+
(newline_span(&fmt_str), String::new()),
299+
],
300+
Applicability::MachineApplicable,
301+
);
302+
},
303+
);
304+
}
305+
}
286306
} else if mac.path == sym!(print) {
287307
if !is_build_script(cx) {
288308
span_lint(cx, PRINT_STDOUT, mac.span(), "use of `print!`");

tests/ui/eprint_with_newline.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#![allow(clippy::print_literal)]
2+
#![warn(clippy::print_with_newline)]
3+
4+
fn main() {
5+
eprint!("Hello\n");
6+
eprint!("Hello {}\n", "world");
7+
eprint!("Hello {} {}\n", "world", "#2");
8+
eprint!("{}\n", 1265);
9+
eprint!("\n");
10+
11+
// these are all fine
12+
eprint!("");
13+
eprint!("Hello");
14+
eprintln!("Hello");
15+
eprintln!("Hello\n");
16+
eprintln!("Hello {}\n", "world");
17+
eprint!("Issue\n{}", 1265);
18+
eprint!("{}", 1265);
19+
eprint!("\n{}", 1275);
20+
eprint!("\n\n");
21+
eprint!("like eof\n\n");
22+
eprint!("Hello {} {}\n\n", "world", "#2");
23+
eprintln!("\ndon't\nwarn\nfor\nmultiple\nnewlines\n"); // #3126
24+
eprintln!("\nbla\n\n"); // #3126
25+
26+
// Escaping
27+
eprint!("\\n"); // #3514
28+
eprint!("\\\n"); // should fail
29+
eprint!("\\\\n");
30+
31+
// Raw strings
32+
eprint!(r"\n"); // #3778
33+
34+
// Literal newlines should also fail
35+
eprint!(
36+
"
37+
"
38+
);
39+
eprint!(
40+
r"
41+
"
42+
);
43+
44+
// Don't warn on CRLF (#4208)
45+
eprint!("\r\n");
46+
eprint!("foo\r\n");
47+
eprint!("\\r\n"); //~ ERROR
48+
eprint!("foo\rbar\n") // ~ ERROR
49+
}

tests/ui/eprint_with_newline.stderr

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
error: using `eprint!()` with a format string that ends in a single newline
2+
--> $DIR/eprint_with_newline.rs:5:5
3+
|
4+
LL | eprint!("Hello/n");
5+
| ^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::print-with-newline` implied by `-D warnings`
8+
help: use `eprintln!` instead
9+
|
10+
LL | eprintln!("Hello");
11+
| ^^^^^^^^ --
12+
13+
error: using `eprint!()` with a format string that ends in a single newline
14+
--> $DIR/eprint_with_newline.rs:6:5
15+
|
16+
LL | eprint!("Hello {}/n", "world");
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
|
19+
help: use `eprintln!` instead
20+
|
21+
LL | eprintln!("Hello {}", "world");
22+
| ^^^^^^^^ --
23+
24+
error: using `eprint!()` with a format string that ends in a single newline
25+
--> $DIR/eprint_with_newline.rs:7:5
26+
|
27+
LL | eprint!("Hello {} {}/n", "world", "#2");
28+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
29+
|
30+
help: use `eprintln!` instead
31+
|
32+
LL | eprintln!("Hello {} {}", "world", "#2");
33+
| ^^^^^^^^ --
34+
35+
error: using `eprint!()` with a format string that ends in a single newline
36+
--> $DIR/eprint_with_newline.rs:8:5
37+
|
38+
LL | eprint!("{}/n", 1265);
39+
| ^^^^^^^^^^^^^^^^^^^^^
40+
|
41+
help: use `eprintln!` instead
42+
|
43+
LL | eprintln!("{}", 1265);
44+
| ^^^^^^^^ --
45+
46+
error: using `eprint!()` with a format string that ends in a single newline
47+
--> $DIR/eprint_with_newline.rs:9:5
48+
|
49+
LL | eprint!("/n");
50+
| ^^^^^^^^^^^^^
51+
|
52+
help: use `eprintln!` instead
53+
|
54+
LL | eprintln!();
55+
| ^^^^^^^^ --
56+
57+
error: using `eprint!()` with a format string that ends in a single newline
58+
--> $DIR/eprint_with_newline.rs:28:5
59+
|
60+
LL | eprint!("//n"); // should fail
61+
| ^^^^^^^^^^^^^^^
62+
|
63+
help: use `eprintln!` instead
64+
|
65+
LL | eprintln!("/"); // should fail
66+
| ^^^^^^^^ --
67+
68+
error: using `eprint!()` with a format string that ends in a single newline
69+
--> $DIR/eprint_with_newline.rs:35:5
70+
|
71+
LL | / eprint!(
72+
LL | | "
73+
LL | | "
74+
LL | | );
75+
| |_____^
76+
|
77+
help: use `eprintln!` instead
78+
|
79+
LL | eprintln!(
80+
LL | ""
81+
|
82+
83+
error: using `eprint!()` with a format string that ends in a single newline
84+
--> $DIR/eprint_with_newline.rs:39:5
85+
|
86+
LL | / eprint!(
87+
LL | | r"
88+
LL | | "
89+
LL | | );
90+
| |_____^
91+
|
92+
help: use `eprintln!` instead
93+
|
94+
LL | eprintln!(
95+
LL | r""
96+
|
97+
98+
error: using `eprint!()` with a format string that ends in a single newline
99+
--> $DIR/eprint_with_newline.rs:47:5
100+
|
101+
LL | eprint!("/r/n"); //~ ERROR
102+
| ^^^^^^^^^^^^^^^^
103+
|
104+
help: use `eprintln!` instead
105+
|
106+
LL | eprintln!("/r"); //~ ERROR
107+
| ^^^^^^^^ --
108+
109+
error: using `eprint!()` with a format string that ends in a single newline
110+
--> $DIR/eprint_with_newline.rs:48:5
111+
|
112+
LL | eprint!("foo/rbar/n") // ~ ERROR
113+
| ^^^^^^^^^^^^^^^^^^^^^
114+
|
115+
help: use `eprintln!` instead
116+
|
117+
LL | eprintln!("foo/rbar") // ~ ERROR
118+
| ^^^^^^^^ --
119+
120+
error: aborting due to 10 previous errors
121+

0 commit comments

Comments
 (0)