Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{print,write}-with-newline: do not suggest empty format string #6042

Merged
merged 1 commit into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion clippy_lints/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,15 @@ impl EarlyLintPass for Write {
}

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

if *contents == r"\n" {
return sp;
}

let newline_sp_hi = sp.hi()
- match fmtstr.style {
StrStyle::Cooked => BytePos(1),
Expand Down
1 change: 1 addition & 0 deletions tests/ui/print_with_newline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn main() {
print!("Hello {}\n", "world");
print!("Hello {} {}\n", "world", "#2");
print!("{}\n", 1265);
print!("\n");

// these are all fine
print!("");
Expand Down
23 changes: 17 additions & 6 deletions tests/ui/print_with_newline.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,18 @@ LL | println!("{}", 1265);
| ^^^^^^^ --

error: using `print!()` with a format string that ends in a single newline
--> $DIR/print_with_newline.rs:30:5
--> $DIR/print_with_newline.rs:12:5
|
LL | print!("/n");
| ^^^^^^^^^^^^
|
help: use `println!` instead
|
LL | println!();
| ^^^^^^^ --

error: using `print!()` with a format string that ends in a single newline
--> $DIR/print_with_newline.rs:31:5
|
LL | print!("//n"); // should fail
| ^^^^^^^^^^^^^^
Expand All @@ -55,7 +66,7 @@ LL | println!("/"); // should fail
| ^^^^^^^ --

error: using `print!()` with a format string that ends in a single newline
--> $DIR/print_with_newline.rs:37:5
--> $DIR/print_with_newline.rs:38:5
|
LL | / print!(
LL | | "
Expand All @@ -70,7 +81,7 @@ LL | ""
|

error: using `print!()` with a format string that ends in a single newline
--> $DIR/print_with_newline.rs:41:5
--> $DIR/print_with_newline.rs:42:5
|
LL | / print!(
LL | | r"
Expand All @@ -85,7 +96,7 @@ LL | r""
|

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

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

error: aborting due to 9 previous errors
error: aborting due to 10 previous errors

1 change: 1 addition & 0 deletions tests/ui/write_with_newline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ fn main() {
write!(&mut v, "Hello {}\n", "world");
write!(&mut v, "Hello {} {}\n", "world", "#2");
write!(&mut v, "{}\n", 1265);
write!(&mut v, "\n");

// These should be fine
write!(&mut v, "");
Expand Down
23 changes: 17 additions & 6 deletions tests/ui/write_with_newline.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,18 @@ LL | writeln!(&mut v, "{}", 1265);
| ^^^^^^^ --

error: using `write!()` with a format string that ends in a single newline
--> $DIR/write_with_newline.rs:35:5
--> $DIR/write_with_newline.rs:17:5
|
LL | write!(&mut v, "/n");
| ^^^^^^^^^^^^^^^^^^^^
|
help: use `writeln!()` instead
|
LL | writeln!(&mut v, );
| ^^^^^^^ --

error: using `write!()` with a format string that ends in a single newline
--> $DIR/write_with_newline.rs:36:5
|
LL | write!(&mut v, "//n"); // should fail
| ^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -55,7 +66,7 @@ LL | writeln!(&mut v, "/"); // should fail
| ^^^^^^^ --

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

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

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

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

error: aborting due to 9 previous errors
error: aborting due to 10 previous errors