Skip to content

Commit

Permalink
print-with-newline: do not suggest println!("")
Browse files Browse the repository at this point in the history
  • Loading branch information
euclio committed Sep 14, 2020
1 parent 231444d commit ed245c9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
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

0 comments on commit ed245c9

Please sign in to comment.