]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/write_literal.rs
Merge commit '7248d06384c6a90de58c04c1f46be88821278d8b' into sync-from-clippy
[rust.git] / src / tools / clippy / tests / ui / write_literal.rs
1 #![allow(unused_must_use)]
2 #![warn(clippy::write_literal)]
3
4 use std::io::Write;
5
6 fn main() {
7     let mut v = Vec::new();
8
9     // these should be fine
10     write!(v, "Hello");
11     writeln!(v, "Hello");
12     let world = "world";
13     writeln!(v, "Hello {}", world);
14     writeln!(v, "Hello {world}", world = world);
15     writeln!(v, "3 in hex is {:X}", 3);
16     writeln!(v, "2 + 1 = {:.4}", 3);
17     writeln!(v, "2 + 1 = {:5.4}", 3);
18     writeln!(v, "Debug test {:?}", "hello, world");
19     writeln!(v, "{0:8} {1:>8}", "hello", "world");
20     writeln!(v, "{1:8} {0:>8}", "hello", "world");
21     writeln!(v, "{foo:8} {bar:>8}", foo = "hello", bar = "world");
22     writeln!(v, "{bar:8} {foo:>8}", foo = "hello", bar = "world");
23     writeln!(v, "{number:>width$}", number = 1, width = 6);
24     writeln!(v, "{number:>0width$}", number = 1, width = 6);
25     writeln!(v, "{} of {:b} people know binary, the other half doesn't", 1, 2);
26     writeln!(v, "10 / 4 is {}", 2.5);
27     writeln!(v, "2 + 1 = {}", 3);
28     writeln!(v, "From expansion {}", stringify!(not a string literal));
29
30     // these should throw warnings
31     write!(v, "Hello {}", "world");
32     writeln!(v, "Hello {} {}", world, "world");
33     writeln!(v, "Hello {}", "world");
34     writeln!(v, "{} {:.4}", "a literal", 5);
35
36     // positional args don't change the fact
37     // that we're using a literal -- this should
38     // throw a warning
39     writeln!(v, "{0} {1}", "hello", "world");
40     writeln!(v, "{1} {0}", "hello", "world");
41
42     // named args shouldn't change anything either
43     writeln!(v, "{foo} {bar}", foo = "hello", bar = "world");
44     writeln!(v, "{bar} {foo}", foo = "hello", bar = "world");
45 }