Skip to content

Commit b2a4013

Browse files
authored
Merge pull request #3014 from estk/allow-write-multi-newline
Allow print/write with multiple newlines
2 parents 826d998 + 550ff84 commit b2a4013

File tree

5 files changed

+26
-21
lines changed

5 files changed

+26
-21
lines changed

clippy_lints/src/write.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ declare_clippy_lint! {
4242
declare_clippy_lint! {
4343
pub PRINT_WITH_NEWLINE,
4444
style,
45-
"using `print!()` with a format string that ends in a newline"
45+
"using `print!()` with a format string that ends in a single newline"
4646
}
4747

4848
/// **What it does:** Checks for printing on *stdout*. The purpose of this lint
@@ -135,7 +135,7 @@ declare_clippy_lint! {
135135
declare_clippy_lint! {
136136
pub WRITE_WITH_NEWLINE,
137137
style,
138-
"using `write!()` with a format string that ends in a newline"
138+
"using `write!()` with a format string that ends in a single newline"
139139
}
140140

141141
/// **What it does:** This lint warns about the use of literals as `write!`/`writeln!` args.
@@ -194,18 +194,18 @@ impl EarlyLintPass for Pass {
194194
} else if mac.node.path == "print" {
195195
span_lint(cx, PRINT_STDOUT, mac.span, "use of `print!`");
196196
if let Some(fmtstr) = check_tts(cx, &mac.node.tts, false) {
197-
if fmtstr.ends_with("\\n") {
197+
if fmtstr.ends_with("\\n") && !fmtstr.ends_with("\\n\\n") {
198198
span_lint(cx, PRINT_WITH_NEWLINE, mac.span,
199199
"using `print!()` with a format string that ends in a \
200-
newline, consider using `println!()` instead");
200+
single newline, consider using `println!()` instead");
201201
}
202202
}
203203
} else if mac.node.path == "write" {
204204
if let Some(fmtstr) = check_tts(cx, &mac.node.tts, true) {
205-
if fmtstr.ends_with("\\n") {
205+
if fmtstr.ends_with("\\n") && !fmtstr.ends_with("\\n\\n") {
206206
span_lint(cx, WRITE_WITH_NEWLINE, mac.span,
207207
"using `write!()` with a format string that ends in a \
208-
newline, consider using `writeln!()` instead");
208+
single newline, consider using `writeln!()` instead");
209209
}
210210
}
211211
} else if mac.node.path == "writeln" {

tests/ui/print_with_newline.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
fn main() {
77
print!("Hello\n");
88
print!("Hello {}\n", "world");
9-
print!("Hello {} {}\n\n", "world", "#2");
9+
print!("Hello {} {}\n", "world", "#2");
1010
print!("{}\n", 1265);
1111

1212
// these are all fine
@@ -18,4 +18,7 @@ fn main() {
1818
print!("Issue\n{}", 1265);
1919
print!("{}", 1265);
2020
print!("\n{}", 1275);
21+
print!("\n\n");
22+
print!("like eof\n\n");
23+
print!("Hello {} {}\n\n", "world", "#2");
2124
}

tests/ui/print_with_newline.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
error: using `print!()` with a format string that ends in a newline, consider using `println!()` instead
1+
error: using `print!()` with a format string that ends in a single newline, consider using `println!()` instead
22
--> $DIR/print_with_newline.rs:7:5
33
|
44
7 | print!("Hello/n");
55
| ^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D print-with-newline` implied by `-D warnings`
88

9-
error: using `print!()` with a format string that ends in a newline, consider using `println!()` instead
9+
error: using `print!()` with a format string that ends in a single newline, consider using `println!()` instead
1010
--> $DIR/print_with_newline.rs:8:5
1111
|
1212
8 | print!("Hello {}/n", "world");
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

15-
error: using `print!()` with a format string that ends in a newline, consider using `println!()` instead
15+
error: using `print!()` with a format string that ends in a single newline, consider using `println!()` instead
1616
--> $DIR/print_with_newline.rs:9:5
1717
|
18-
9 | print!("Hello {} {}/n/n", "world", "#2");
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
9 | print!("Hello {} {}/n", "world", "#2");
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2020

21-
error: using `print!()` with a format string that ends in a newline, consider using `println!()` instead
21+
error: using `print!()` with a format string that ends in a single newline, consider using `println!()` instead
2222
--> $DIR/print_with_newline.rs:10:5
2323
|
2424
10 | print!("{}/n", 1265);

tests/ui/write_with_newline.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fn main() {
99
// These should fail
1010
write!(&mut v, "Hello\n");
1111
write!(&mut v, "Hello {}\n", "world");
12-
write!(&mut v, "Hello {} {}\n\n", "world", "#2");
12+
write!(&mut v, "Hello {} {}\n", "world", "#2");
1313
write!(&mut v, "{}\n", 1265);
1414

1515
// These should be fine
@@ -21,5 +21,7 @@ fn main() {
2121
write!(&mut v, "Issue\n{}", 1265);
2222
write!(&mut v, "{}", 1265);
2323
write!(&mut v, "\n{}", 1275);
24-
24+
write!(&mut v, "\n\n");
25+
write!(&mut v, "like eof\n\n");
26+
write!(&mut v, "Hello {} {}\n\n", "world", "#2");
2527
}

tests/ui/write_with_newline.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
error: using `write!()` with a format string that ends in a newline, consider using `writeln!()` instead
1+
error: using `write!()` with a format string that ends in a single newline, consider using `writeln!()` instead
22
--> $DIR/write_with_newline.rs:10:5
33
|
44
10 | write!(&mut v, "Hello/n");
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D write-with-newline` implied by `-D warnings`
88

9-
error: using `write!()` with a format string that ends in a newline, consider using `writeln!()` instead
9+
error: using `write!()` with a format string that ends in a single newline, consider using `writeln!()` instead
1010
--> $DIR/write_with_newline.rs:11:5
1111
|
1212
11 | write!(&mut v, "Hello {}/n", "world");
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1414

15-
error: using `write!()` with a format string that ends in a newline, consider using `writeln!()` instead
15+
error: using `write!()` with a format string that ends in a single newline, consider using `writeln!()` instead
1616
--> $DIR/write_with_newline.rs:12:5
1717
|
18-
12 | write!(&mut v, "Hello {} {}/n/n", "world", "#2");
19-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
12 | write!(&mut v, "Hello {} {}/n", "world", "#2");
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2020

21-
error: using `write!()` with a format string that ends in a newline, consider using `writeln!()` instead
21+
error: using `write!()` with a format string that ends in a single newline, consider using `writeln!()` instead
2222
--> $DIR/write_with_newline.rs:13:5
2323
|
2424
13 | write!(&mut v, "{}/n", 1265);

0 commit comments

Comments
 (0)