fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
2.2 KiB58 linesraw
1#![warn(clippy::write_literal)]
2#![allow(clippy::uninlined_format_args, unused_must_use)]
3
4use std::io::Write;
5
6fn 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    //~^ ERROR: literal with an empty format string
33    //~| NOTE: `-D clippy::write-literal` implied by `-D warnings`
34    writeln!(v, "Hello {} world", world);
35    //~^ ERROR: literal with an empty format string
36    writeln!(v, "Hello world");
37    //~^ ERROR: literal with an empty format string
38    writeln!(v, "a literal {:.4}", 5);
39    //~^ ERROR: literal with an empty format string
40
41    // positional args don't change the fact
42    // that we're using a literal -- this should
43    // throw a warning
44    writeln!(v, "hello world");
45    //~^ ERROR: literal with an empty format string
46    //~| ERROR: literal with an empty format string
47    writeln!(v, "world hello");
48    //~^ ERROR: literal with an empty format string
49    //~| ERROR: literal with an empty format string
50
51    // named args shouldn't change anything either
52    writeln!(v, "hello world");
53    //~^ ERROR: literal with an empty format string
54    //~| ERROR: literal with an empty format string
55    writeln!(v, "world hello");
56    //~^ ERROR: literal with an empty format string
57    //~| ERROR: literal with an empty format string
58}