print_with_newline / write_with_newline: don't warn about string with several \ns in them.

Fixes #3126
This commit is contained in:
Matthias Krüger 2018-09-06 12:55:04 +02:00
parent 0a8ceaf8b0
commit a0f56edfc3
3 changed files with 12 additions and 2 deletions

View File

@ -195,7 +195,10 @@ fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &Mac) {
} else if mac.node.path == "print" { } else if mac.node.path == "print" {
span_lint(cx, PRINT_STDOUT, mac.span, "use of `print!`"); span_lint(cx, PRINT_STDOUT, mac.span, "use of `print!`");
if let Some(fmtstr) = check_tts(cx, &mac.node.tts, false).0 { 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( span_lint(
cx, cx,
PRINT_WITH_NEWLINE, PRINT_WITH_NEWLINE,
@ -207,7 +210,10 @@ fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &Mac) {
} }
} else if mac.node.path == "write" { } else if mac.node.path == "write" {
if let Some(fmtstr) = check_tts(cx, &mac.node.tts, true).0 { 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( span_lint(
cx, cx,
WRITE_WITH_NEWLINE, WRITE_WITH_NEWLINE,

View File

@ -21,4 +21,6 @@ fn main() {
print!("\n\n"); print!("\n\n");
print!("like eof\n\n"); print!("like eof\n\n");
print!("Hello {} {}\n\n", "world", "#2"); print!("Hello {} {}\n\n", "world", "#2");
println!("\ndon't\nwarn\nfor\nmultiple\nnewlines\n"); // #3126
println!("\nbla\n\n"); // #3126
} }

View File

@ -26,4 +26,6 @@ fn main() {
write!(&mut v, "\n\n"); write!(&mut v, "\n\n");
write!(&mut v, "like eof\n\n"); write!(&mut v, "like eof\n\n");
write!(&mut v, "Hello {} {}\n\n", "world", "#2"); 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
} }