From a0f56edfc316d642785efc5ccaf0d1c6c457b057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Thu, 6 Sep 2018 12:55:04 +0200 Subject: [PATCH] print_with_newline / write_with_newline: don't warn about string with several `\n`s in them. Fixes #3126 --- clippy_lints/src/write.rs | 10 ++++++++-- tests/ui/print_with_newline.rs | 2 ++ tests/ui/write_with_newline.rs | 2 ++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/write.rs b/clippy_lints/src/write.rs index 06a4f6cb39e..7ddae1c81c7 100644 --- a/clippy_lints/src/write.rs +++ b/clippy_lints/src/write.rs @@ -195,7 +195,10 @@ fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &Mac) { } else if mac.node.path == "print" { span_lint(cx, PRINT_STDOUT, mac.span, "use of `print!`"); if let Some(fmtstr) = check_tts(cx, &mac.node.tts, false).0 { - if fmtstr.ends_with("\\n") && !fmtstr.ends_with("\\n\\n") { + if fmtstr.ends_with("\\n") && + // don't warn about strings with several `\n`s (#3126) + fmtstr.matches("\\n").count() == 1 + { span_lint( cx, PRINT_WITH_NEWLINE, @@ -207,7 +210,10 @@ fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &Mac) { } } else if mac.node.path == "write" { if let Some(fmtstr) = check_tts(cx, &mac.node.tts, true).0 { - if fmtstr.ends_with("\\n") && !fmtstr.ends_with("\\n\\n") { + if fmtstr.ends_with("\\n") && + // don't warn about strings with several `\n`s (#3126) + fmtstr.matches("\\n").count() == 1 + { span_lint( cx, WRITE_WITH_NEWLINE, diff --git a/tests/ui/print_with_newline.rs b/tests/ui/print_with_newline.rs index 5efee5abfc8..c2c79c726e8 100644 --- a/tests/ui/print_with_newline.rs +++ b/tests/ui/print_with_newline.rs @@ -21,4 +21,6 @@ fn main() { print!("\n\n"); print!("like eof\n\n"); print!("Hello {} {}\n\n", "world", "#2"); + println!("\ndon't\nwarn\nfor\nmultiple\nnewlines\n"); // #3126 + println!("\nbla\n\n"); // #3126 } diff --git a/tests/ui/write_with_newline.rs b/tests/ui/write_with_newline.rs index e060459a411..58e6002fa6a 100644 --- a/tests/ui/write_with_newline.rs +++ b/tests/ui/write_with_newline.rs @@ -26,4 +26,6 @@ fn main() { write!(&mut v, "\n\n"); write!(&mut v, "like eof\n\n"); write!(&mut v, "Hello {} {}\n\n", "world", "#2"); + writeln!(&mut v, "\ndon't\nwarn\nfor\nmultiple\nnewlines\n"); // #3126 + writeln!(&mut v, "\nbla\n\n"); // #3126 }