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