]> git.lizzy.rs Git - rust.git/blob - tests/ui/format.rs
Auto merge of #3645 - phansch:remove_copyright_headers, r=oli-obk
[rust.git] / tests / ui / format.rs
1 #![allow(clippy::print_literal)]
2 #![warn(clippy::useless_format)]
3
4 struct Foo(pub String);
5
6 macro_rules! foo {
7   ($($t:tt)*) => (Foo(format!($($t)*)))
8 }
9
10 fn main() {
11     format!("foo");
12
13     format!("{}", "foo");
14     format!("{:?}", "foo"); // don't warn about debug
15     format!("{:8}", "foo");
16     format!("{:width$}", "foo", width = 8);
17     format!("{:+}", "foo"); // warn when the format makes no difference
18     format!("{:<}", "foo"); // warn when the format makes no difference
19     format!("foo {}", "bar");
20     format!("{} bar", "foo");
21
22     let arg: String = "".to_owned();
23     format!("{}", arg);
24     format!("{:?}", arg); // don't warn about debug
25     format!("{:8}", arg);
26     format!("{:width$}", arg, width = 8);
27     format!("{:+}", arg); // warn when the format makes no difference
28     format!("{:<}", arg); // warn when the format makes no difference
29     format!("foo {}", arg);
30     format!("{} bar", arg);
31
32     // we don’t want to warn for non-string args, see #697
33     format!("{}", 42);
34     format!("{:?}", 42);
35     format!("{:+}", 42);
36     format!("foo {}", 42);
37     format!("{} bar", 42);
38
39     // we only want to warn about `format!` itself
40     println!("foo");
41     println!("{}", "foo");
42     println!("foo {}", "foo");
43     println!("{}", 42);
44     println!("foo {}", 42);
45
46     // A format! inside a macro should not trigger a warning
47     foo!("should not warn");
48
49     // precision on string means slicing without panicking on size:
50     format!("{:.1}", "foo"); // could be "foo"[..1]
51     format!("{:.10}", "foo"); // could not be "foo"[..10]
52     format!("{:.prec$}", "foo", prec = 1);
53     format!("{:.prec$}", "foo", prec = 10);
54
55     format!("{}", 42.to_string());
56     let x = std::path::PathBuf::from("/bar/foo/qux");
57     format!("{}", x.display().to_string());
58 }