Skip to content

Commit 61dd007

Browse files
committed
Auto merge of rust-lang#6042 - euclio:println-empty, r=flip1995
{print,write}-with-newline: do not suggest empty format string changelog: do not suggest empty format strings in `print-with-newline` and `write-with-newline`
2 parents e7bff50 + 0261e34 commit 61dd007

File tree

5 files changed

+41
-13
lines changed

5 files changed

+41
-13
lines changed

clippy_lints/src/write.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,15 @@ impl EarlyLintPass for Write {
322322
}
323323

324324
/// Given a format string that ends in a newline and its span, calculates the span of the
325-
/// newline.
325+
/// newline, or the format string itself if the format string consists solely of a newline.
326326
fn newline_span(fmtstr: &StrLit) -> Span {
327327
let sp = fmtstr.span;
328328
let contents = &fmtstr.symbol.as_str();
329329

330+
if *contents == r"\n" {
331+
return sp;
332+
}
333+
330334
let newline_sp_hi = sp.hi()
331335
- match fmtstr.style {
332336
StrStyle::Cooked => BytePos(1),

tests/ui/print_with_newline.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ fn main() {
99
print!("Hello {}\n", "world");
1010
print!("Hello {} {}\n", "world", "#2");
1111
print!("{}\n", 1265);
12+
print!("\n");
1213

1314
// these are all fine
1415
print!("");

tests/ui/print_with_newline.stderr

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,18 @@ LL | println!("{}", 1265);
4444
| ^^^^^^^ --
4545

4646
error: using `print!()` with a format string that ends in a single newline
47-
--> $DIR/print_with_newline.rs:30:5
47+
--> $DIR/print_with_newline.rs:12:5
48+
|
49+
LL | print!("/n");
50+
| ^^^^^^^^^^^^
51+
|
52+
help: use `println!` instead
53+
|
54+
LL | println!();
55+
| ^^^^^^^ --
56+
57+
error: using `print!()` with a format string that ends in a single newline
58+
--> $DIR/print_with_newline.rs:31:5
4859
|
4960
LL | print!("//n"); // should fail
5061
| ^^^^^^^^^^^^^^
@@ -55,7 +66,7 @@ LL | println!("/"); // should fail
5566
| ^^^^^^^ --
5667

5768
error: using `print!()` with a format string that ends in a single newline
58-
--> $DIR/print_with_newline.rs:37:5
69+
--> $DIR/print_with_newline.rs:38:5
5970
|
6071
LL | / print!(
6172
LL | | "
@@ -70,7 +81,7 @@ LL | ""
7081
|
7182

7283
error: using `print!()` with a format string that ends in a single newline
73-
--> $DIR/print_with_newline.rs:41:5
84+
--> $DIR/print_with_newline.rs:42:5
7485
|
7586
LL | / print!(
7687
LL | | r"
@@ -85,7 +96,7 @@ LL | r""
8596
|
8697

8798
error: using `print!()` with a format string that ends in a single newline
88-
--> $DIR/print_with_newline.rs:49:5
99+
--> $DIR/print_with_newline.rs:50:5
89100
|
90101
LL | print!("/r/n"); //~ ERROR
91102
| ^^^^^^^^^^^^^^^
@@ -96,7 +107,7 @@ LL | println!("/r"); //~ ERROR
96107
| ^^^^^^^ --
97108

98109
error: using `print!()` with a format string that ends in a single newline
99-
--> $DIR/print_with_newline.rs:50:5
110+
--> $DIR/print_with_newline.rs:51:5
100111
|
101112
LL | print!("foo/rbar/n") // ~ ERROR
102113
| ^^^^^^^^^^^^^^^^^^^^
@@ -106,5 +117,5 @@ help: use `println!` instead
106117
LL | println!("foo/rbar") // ~ ERROR
107118
| ^^^^^^^ --
108119

109-
error: aborting due to 9 previous errors
120+
error: aborting due to 10 previous errors
110121

tests/ui/write_with_newline.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ fn main() {
1414
write!(&mut v, "Hello {}\n", "world");
1515
write!(&mut v, "Hello {} {}\n", "world", "#2");
1616
write!(&mut v, "{}\n", 1265);
17+
write!(&mut v, "\n");
1718

1819
// These should be fine
1920
write!(&mut v, "");

tests/ui/write_with_newline.stderr

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,18 @@ LL | writeln!(&mut v, "{}", 1265);
4444
| ^^^^^^^ --
4545

4646
error: using `write!()` with a format string that ends in a single newline
47-
--> $DIR/write_with_newline.rs:35:5
47+
--> $DIR/write_with_newline.rs:17:5
48+
|
49+
LL | write!(&mut v, "/n");
50+
| ^^^^^^^^^^^^^^^^^^^^
51+
|
52+
help: use `writeln!()` instead
53+
|
54+
LL | writeln!(&mut v, );
55+
| ^^^^^^^ --
56+
57+
error: using `write!()` with a format string that ends in a single newline
58+
--> $DIR/write_with_newline.rs:36:5
4859
|
4960
LL | write!(&mut v, "//n"); // should fail
5061
| ^^^^^^^^^^^^^^^^^^^^^^
@@ -55,7 +66,7 @@ LL | writeln!(&mut v, "/"); // should fail
5566
| ^^^^^^^ --
5667

5768
error: using `write!()` with a format string that ends in a single newline
58-
--> $DIR/write_with_newline.rs:42:5
69+
--> $DIR/write_with_newline.rs:43:5
5970
|
6071
LL | / write!(
6172
LL | | &mut v,
@@ -72,7 +83,7 @@ LL | ""
7283
|
7384

7485
error: using `write!()` with a format string that ends in a single newline
75-
--> $DIR/write_with_newline.rs:47:5
86+
--> $DIR/write_with_newline.rs:48:5
7687
|
7788
LL | / write!(
7889
LL | | &mut v,
@@ -89,7 +100,7 @@ LL | r""
89100
|
90101

91102
error: using `write!()` with a format string that ends in a single newline
92-
--> $DIR/write_with_newline.rs:56:5
103+
--> $DIR/write_with_newline.rs:57:5
93104
|
94105
LL | write!(&mut v, "/r/n"); //~ ERROR
95106
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -100,7 +111,7 @@ LL | writeln!(&mut v, "/r"); //~ ERROR
100111
| ^^^^^^^ --
101112

102113
error: using `write!()` with a format string that ends in a single newline
103-
--> $DIR/write_with_newline.rs:57:5
114+
--> $DIR/write_with_newline.rs:58:5
104115
|
105116
LL | write!(&mut v, "foo/rbar/n");
106117
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -110,5 +121,5 @@ help: use `writeln!()` instead
110121
LL | writeln!(&mut v, "foo/rbar");
111122
| ^^^^^^^ --
112123

113-
error: aborting due to 9 previous errors
124+
error: aborting due to 10 previous errors
114125

0 commit comments

Comments
 (0)