Skip to content

Commit 550ff84

Browse files
committed
Allow print/write with multiple newlines
1 parent 7702555 commit 550ff84

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
@@ -38,7 +38,7 @@ declare_clippy_lint! {
3838
declare_clippy_lint! {
3939
pub PRINT_WITH_NEWLINE,
4040
style,
41-
"using `print!()` with a format string that ends in a newline"
41+
"using `print!()` with a format string that ends in a single newline"
4242
}
4343

4444
/// **What it does:** Checks for printing on *stdout*. The purpose of this lint
@@ -127,7 +127,7 @@ declare_clippy_lint! {
127127
declare_clippy_lint! {
128128
pub WRITE_WITH_NEWLINE,
129129
style,
130-
"using `write!()` with a format string that ends in a newline"
130+
"using `write!()` with a format string that ends in a single newline"
131131
}
132132

133133
/// **What it does:** This lint warns about the use of literals as `write!`/`writeln!` args.
@@ -186,18 +186,18 @@ impl EarlyLintPass for Pass {
186186
} else if mac.node.path == "print" {
187187
span_lint(cx, PRINT_STDOUT, mac.span, "use of `print!`");
188188
if let Some(fmtstr) = check_tts(cx, &mac.node.tts, false) {
189-
if fmtstr.ends_with("\\n") {
189+
if fmtstr.ends_with("\\n") && !fmtstr.ends_with("\\n\\n") {
190190
span_lint(cx, PRINT_WITH_NEWLINE, mac.span,
191191
"using `print!()` with a format string that ends in a \
192-
newline, consider using `println!()` instead");
192+
single newline, consider using `println!()` instead");
193193
}
194194
}
195195
} else if mac.node.path == "write" {
196196
if let Some(fmtstr) = check_tts(cx, &mac.node.tts, true) {
197-
if fmtstr.ends_with("\\n") {
197+
if fmtstr.ends_with("\\n") && !fmtstr.ends_with("\\n\\n") {
198198
span_lint(cx, WRITE_WITH_NEWLINE, mac.span,
199199
"using `write!()` with a format string that ends in a \
200-
newline, consider using `writeln!()` instead");
200+
single newline, consider using `writeln!()` instead");
201201
}
202202
}
203203
} 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)