]> git.lizzy.rs Git - rust.git/blob - tests/ui/write_literal.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / write_literal.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![allow(unused_must_use)]
11 #![warn(clippy::write_literal)]
12
13 use std::io::Write;
14
15 fn main() {
16     let mut v = Vec::new();
17
18     // these should be fine
19     write!(&mut v, "Hello");
20     writeln!(&mut v, "Hello");
21     let world = "world";
22     writeln!(&mut v, "Hello {}", world);
23     writeln!(&mut v, "Hello {world}", world = world);
24     writeln!(&mut v, "3 in hex is {:X}", 3);
25     writeln!(&mut v, "2 + 1 = {:.4}", 3);
26     writeln!(&mut v, "2 + 1 = {:5.4}", 3);
27     writeln!(&mut v, "Debug test {:?}", "hello, world");
28     writeln!(&mut v, "{0:8} {1:>8}", "hello", "world");
29     writeln!(&mut v, "{1:8} {0:>8}", "hello", "world");
30     writeln!(&mut v, "{foo:8} {bar:>8}", foo = "hello", bar = "world");
31     writeln!(&mut v, "{bar:8} {foo:>8}", foo = "hello", bar = "world");
32     writeln!(&mut v, "{number:>width$}", number = 1, width = 6);
33     writeln!(&mut v, "{number:>0width$}", number = 1, width = 6);
34
35     // these should throw warnings
36     writeln!(&mut v, "{} of {:b} people know binary, the other half doesn't", 1, 2);
37     write!(&mut v, "Hello {}", "world");
38     writeln!(&mut v, "Hello {} {}", world, "world");
39     writeln!(&mut v, "Hello {}", "world");
40     writeln!(&mut v, "10 / 4 is {}", 2.5);
41     writeln!(&mut v, "2 + 1 = {}", 3);
42
43     // positional args don't change the fact
44     // that we're using a literal -- this should
45     // throw a warning
46     writeln!(&mut v, "{0} {1}", "hello", "world");
47     writeln!(&mut v, "{1} {0}", "hello", "world");
48
49     // named args shouldn't change anything either
50     writeln!(&mut v, "{foo} {bar}", foo = "hello", bar = "world");
51     writeln!(&mut v, "{bar} {foo}", foo = "hello", bar = "world");
52 }