]> git.lizzy.rs Git - rust.git/blob - tests/ui/write_literal.rs
Merge pull request #3291 from JoshMcguigan/cmp_owned-3289
[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
11
12
13 #![allow(unused_must_use)]
14 #![warn(clippy::write_literal)]
15
16 use std::io::Write;
17
18 fn main() {
19     let mut v = Vec::new();
20
21     // these should be fine
22     write!(&mut v, "Hello");
23     writeln!(&mut v, "Hello");
24     let world = "world";
25     writeln!(&mut v, "Hello {}", world);
26     writeln!(&mut v, "Hello {world}", world=world);
27     writeln!(&mut v, "3 in hex is {:X}", 3);
28     writeln!(&mut v, "2 + 1 = {:.4}", 3);
29     writeln!(&mut v, "2 + 1 = {:5.4}", 3);
30     writeln!(&mut v, "Debug test {:?}", "hello, world");
31     writeln!(&mut v, "{0:8} {1:>8}", "hello", "world");
32     writeln!(&mut v, "{1:8} {0:>8}", "hello", "world");
33     writeln!(&mut v, "{foo:8} {bar:>8}", foo="hello", bar="world");
34     writeln!(&mut v, "{bar:8} {foo:>8}", foo="hello", bar="world");
35     writeln!(&mut v, "{number:>width$}", number=1, width=6);
36     writeln!(&mut v, "{number:>0width$}", number=1, width=6);
37
38     // these should throw warnings
39     writeln!(&mut v, "{} of {:b} people know binary, the other half doesn't", 1, 2);
40     write!(&mut v, "Hello {}", "world");
41     writeln!(&mut v, "Hello {} {}", world, "world");
42     writeln!(&mut v, "Hello {}", "world");
43     writeln!(&mut v, "10 / 4 is {}", 2.5);
44     writeln!(&mut v, "2 + 1 = {}", 3);
45
46     // positional args don't change the fact
47     // that we're using a literal -- this should
48     // throw a warning
49     writeln!(&mut v, "{0} {1}", "hello", "world");
50     writeln!(&mut v, "{1} {0}", "hello", "world");
51
52     // named args shouldn't change anything either
53     writeln!(&mut v, "{foo} {bar}", foo="hello", bar="world");
54     writeln!(&mut v, "{bar} {foo}", foo="hello", bar="world");
55 }