rust-clippy/tests/ui/write_literal.fixed

65 lines
2.4 KiB
Rust
Raw Normal View History

2023-07-27 11:40:22 +00:00
#![warn(clippy::write_literal)]
#![allow(clippy::uninlined_format_args, unused_must_use)]
use std::io::Write;
fn main() {
let mut v = Vec::new();
// these should be fine
write!(v, "Hello");
writeln!(v, "Hello");
let world = "world";
writeln!(v, "Hello {}", world);
writeln!(v, "Hello {world}", world = world);
writeln!(v, "3 in hex is {:X}", 3);
writeln!(v, "2 + 1 = {:.4}", 3);
writeln!(v, "2 + 1 = {:5.4}", 3);
writeln!(v, "Debug test {:?}", "hello, world");
writeln!(v, "{0:8} {1:>8}", "hello", "world");
writeln!(v, "{1:8} {0:>8}", "hello", "world");
writeln!(v, "{foo:8} {bar:>8}", foo = "hello", bar = "world");
writeln!(v, "{bar:8} {foo:>8}", foo = "hello", bar = "world");
writeln!(v, "{number:>width$}", number = 1, width = 6);
writeln!(v, "{number:>0width$}", number = 1, width = 6);
writeln!(v, "{} of {:b} people know binary, the other half doesn't", 1, 2);
writeln!(v, "10 / 4 is {}", 2.5);
writeln!(v, "2 + 1 = {}", 3);
writeln!(v, "From expansion {}", stringify!(not a string literal));
// these should throw warnings
write!(v, "Hello world");
//~^ ERROR: literal with an empty format string
//~| NOTE: `-D clippy::write-literal` implied by `-D warnings`
2023-07-27 11:40:22 +00:00
writeln!(v, "Hello {} world", world);
//~^ ERROR: literal with an empty format string
2023-07-27 11:40:22 +00:00
writeln!(v, "Hello world");
//~^ ERROR: literal with an empty format string
2023-07-27 11:40:22 +00:00
writeln!(v, "a literal {:.4}", 5);
//~^ ERROR: literal with an empty format string
2023-07-27 11:40:22 +00:00
// positional args don't change the fact
// that we're using a literal -- this should
// throw a warning
writeln!(v, "hello world");
//~^ ERROR: literal with an empty format string
2023-07-27 11:40:22 +00:00
writeln!(v, "world hello");
//~^ ERROR: literal with an empty format string
2023-07-27 11:40:22 +00:00
// named args shouldn't change anything either
writeln!(v, "hello world");
//~^ ERROR: literal with an empty format string
2023-07-27 11:40:22 +00:00
writeln!(v, "world hello");
//~^ ERROR: literal with an empty format string
// #10128
writeln!(v, "hello {0} world", 2);
//~^ ERROR: literal with an empty format string
writeln!(v, "world {0} hello", 2);
//~^ ERROR: literal with an empty format string
writeln!(v, "hello {0} {1}, {bar}", 2, 3, bar = 4);
//~^ ERROR: literal with an empty format string
writeln!(v, "hello {0} {1}, world {2}", 2, 3, 4);
//~^ ERROR: literal with an empty format string
2023-07-27 11:40:22 +00:00
}