Skip to content

Commit a0f56ed

Browse files
committed
print_with_newline / write_with_newline: don't warn about string with several \ns in them.
Fixes #3126
1 parent 0a8ceaf commit a0f56ed

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

clippy_lints/src/write.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,10 @@ impl EarlyLintPass for Pass {
195195
} else if mac.node.path == "print" {
196196
span_lint(cx, PRINT_STDOUT, mac.span, "use of `print!`");
197197
if let Some(fmtstr) = check_tts(cx, &mac.node.tts, false).0 {
198-
if fmtstr.ends_with("\\n") && !fmtstr.ends_with("\\n\\n") {
198+
if fmtstr.ends_with("\\n") &&
199+
// don't warn about strings with several `\n`s (#3126)
200+
fmtstr.matches("\\n").count() == 1
201+
{
199202
span_lint(
200203
cx,
201204
PRINT_WITH_NEWLINE,
@@ -207,7 +210,10 @@ impl EarlyLintPass for Pass {
207210
}
208211
} else if mac.node.path == "write" {
209212
if let Some(fmtstr) = check_tts(cx, &mac.node.tts, true).0 {
210-
if fmtstr.ends_with("\\n") && !fmtstr.ends_with("\\n\\n") {
213+
if fmtstr.ends_with("\\n") &&
214+
// don't warn about strings with several `\n`s (#3126)
215+
fmtstr.matches("\\n").count() == 1
216+
{
211217
span_lint(
212218
cx,
213219
WRITE_WITH_NEWLINE,

tests/ui/print_with_newline.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@ fn main() {
2121
print!("\n\n");
2222
print!("like eof\n\n");
2323
print!("Hello {} {}\n\n", "world", "#2");
24+
println!("\ndon't\nwarn\nfor\nmultiple\nnewlines\n"); // #3126
25+
println!("\nbla\n\n"); // #3126
2426
}

tests/ui/write_with_newline.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ fn main() {
2626
write!(&mut v, "\n\n");
2727
write!(&mut v, "like eof\n\n");
2828
write!(&mut v, "Hello {} {}\n\n", "world", "#2");
29+
writeln!(&mut v, "\ndon't\nwarn\nfor\nmultiple\nnewlines\n"); // #3126
30+
writeln!(&mut v, "\nbla\n\n"); // #3126
2931
}

0 commit comments

Comments
 (0)