rust-clippy/tests/ui/write_with_newline.fixed

77 lines
2.2 KiB
Rust
Raw Normal View History

2023-04-20 15:19:36 +00:00
// FIXME: Ideally these suggestions would be fixed via rustfix. Blocked by rust-lang/rust#53934
#![allow(clippy::write_literal)]
#![warn(clippy::write_with_newline)]
use std::io::Write;
fn main() {
let mut v = Vec::new();
// These should fail
writeln!(v, "Hello");
//~^ ERROR: using `write!()` with a format string that ends in a single newline
//~| NOTE: `-D clippy::write-with-newline` implied by `-D warnings`
2023-04-20 15:19:36 +00:00
writeln!(v, "Hello {}", "world");
//~^ ERROR: using `write!()` with a format string that ends in a single newline
2023-04-20 15:19:36 +00:00
writeln!(v, "Hello {} {}", "world", "#2");
//~^ ERROR: using `write!()` with a format string that ends in a single newline
2023-04-20 15:19:36 +00:00
writeln!(v, "{}", 1265);
//~^ ERROR: using `write!()` with a format string that ends in a single newline
2023-04-20 15:19:36 +00:00
writeln!(v);
//~^ ERROR: using `write!()` with a format string that ends in a single newline
2023-04-20 15:19:36 +00:00
// These should be fine
write!(v, "");
write!(v, "Hello");
writeln!(v, "Hello");
writeln!(v, "Hello\n");
writeln!(v, "Hello {}\n", "world");
write!(v, "Issue\n{}", 1265);
write!(v, "{}", 1265);
write!(v, "\n{}", 1275);
write!(v, "\n\n");
write!(v, "like eof\n\n");
write!(v, "Hello {} {}\n\n", "world", "#2");
// #3126
writeln!(v, "\ndon't\nwarn\nfor\nmultiple\nnewlines\n");
// #3126
writeln!(v, "\nbla\n\n");
2023-04-20 15:19:36 +00:00
// Escaping
// #3514
write!(v, "\\n");
writeln!(v, "\\");
//~^ ERROR: using `write!()` with a format string that ends in a single newline
2023-04-20 15:19:36 +00:00
write!(v, "\\\\n");
// Raw strings
// #3778
write!(v, r"\n");
2023-04-20 15:19:36 +00:00
// Literal newlines should also fail
writeln!(
//~^ ERROR: using `write!()` with a format string that ends in a single newline
2023-04-20 15:19:36 +00:00
v
);
writeln!(
//~^ ERROR: using `write!()` with a format string that ends in a single newline
2023-04-20 15:19:36 +00:00
v
);
// Don't warn on CRLF (#4208)
write!(v, "\r\n");
write!(v, "foo\r\n");
2023-07-27 11:40:22 +00:00
writeln!(v, "\\r");
//~^ ERROR: using `write!()` with a format string that ends in a single newline
2023-04-20 15:19:36 +00:00
write!(v, "foo\rbar\n");
// Ignore expanded format strings
macro_rules! newline {
() => {
"\n"
};
}
write!(v, newline!());
}