2019-05-23 18:11:38 +00:00
|
|
|
// FIXME: Ideally these suggestions would be fixed via rustfix. Blocked by rust-lang/rust#53934
|
|
|
|
|
2018-07-28 15:34:52 +00:00
|
|
|
#![allow(clippy::write_literal)]
|
|
|
|
#![warn(clippy::write_with_newline)]
|
2018-04-05 03:46:39 +00:00
|
|
|
|
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut v = Vec::new();
|
|
|
|
|
|
|
|
// These should fail
|
2022-01-11 19:31:35 +00:00
|
|
|
write!(v, "Hello\n");
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: using `write!()` with a format string that ends in a single newline
|
|
|
|
//~| NOTE: `-D clippy::write-with-newline` implied by `-D warnings`
|
2022-01-11 19:31:35 +00:00
|
|
|
write!(v, "Hello {}\n", "world");
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: using `write!()` with a format string that ends in a single newline
|
2022-01-11 19:31:35 +00:00
|
|
|
write!(v, "Hello {} {}\n", "world", "#2");
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: using `write!()` with a format string that ends in a single newline
|
2022-01-11 19:31:35 +00:00
|
|
|
write!(v, "{}\n", 1265);
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: using `write!()` with a format string that ends in a single newline
|
2022-01-11 19:31:35 +00:00
|
|
|
write!(v, "\n");
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: using `write!()` with a format string that ends in a single newline
|
2018-04-05 03:46:39 +00:00
|
|
|
|
|
|
|
// These should be fine
|
2022-01-11 19:31:35 +00:00
|
|
|
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");
|
2023-07-28 18:40:44 +00:00
|
|
|
// #3126
|
|
|
|
writeln!(v, "\ndon't\nwarn\nfor\nmultiple\nnewlines\n");
|
|
|
|
// #3126
|
|
|
|
writeln!(v, "\nbla\n\n");
|
2018-12-11 06:28:25 +00:00
|
|
|
|
|
|
|
// Escaping
|
2023-07-28 18:40:44 +00:00
|
|
|
// #3514
|
|
|
|
write!(v, "\\n");
|
|
|
|
write!(v, "\\\n");
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: using `write!()` with a format string that ends in a single newline
|
2022-01-11 19:31:35 +00:00
|
|
|
write!(v, "\\\\n");
|
2019-02-18 03:32:58 +00:00
|
|
|
|
|
|
|
// Raw strings
|
2023-07-28 18:40:44 +00:00
|
|
|
// #3778
|
|
|
|
write!(v, r"\n");
|
2019-02-18 16:39:23 +00:00
|
|
|
|
|
|
|
// Literal newlines should also fail
|
|
|
|
write!(
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: using `write!()` with a format string that ends in a single newline
|
2022-01-11 19:31:35 +00:00
|
|
|
v,
|
2019-02-18 16:39:23 +00:00
|
|
|
"
|
|
|
|
"
|
|
|
|
);
|
|
|
|
write!(
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: using `write!()` with a format string that ends in a single newline
|
2022-01-11 19:31:35 +00:00
|
|
|
v,
|
2019-02-18 16:39:23 +00:00
|
|
|
r"
|
|
|
|
"
|
|
|
|
);
|
2019-11-03 04:41:22 +00:00
|
|
|
|
|
|
|
// Don't warn on CRLF (#4208)
|
2022-01-11 19:31:35 +00:00
|
|
|
write!(v, "\r\n");
|
|
|
|
write!(v, "foo\r\n");
|
2023-04-20 14:37:15 +00:00
|
|
|
write!(v, "\\r\n");
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: using `write!()` with a format string that ends in a single newline
|
2022-01-11 19:31:35 +00:00
|
|
|
write!(v, "foo\rbar\n");
|
2022-08-30 12:20:49 +00:00
|
|
|
|
|
|
|
// Ignore expanded format strings
|
|
|
|
macro_rules! newline {
|
|
|
|
() => {
|
|
|
|
"\n"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
write!(v, newline!());
|
2018-04-05 03:46:39 +00:00
|
|
|
}
|