]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/format.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / format.rs
CommitLineData
f20569fa
XL
1// run-rustfix
2
3#![allow(clippy::print_literal, clippy::redundant_clone)]
4#![warn(clippy::useless_format)]
5
6struct Foo(pub String);
7
8macro_rules! foo {
9 ($($t:tt)*) => (Foo(format!($($t)*)))
10}
11
12fn main() {
13 format!("foo");
14 format!("{{}}");
15 format!("{{}} abc {{}}");
16 format!(
17 r##"foo {{}}
18" bar"##
19 );
20
21 format!("{}", "foo");
22 format!("{:?}", "foo"); // Don't warn about `Debug`.
23 format!("{:8}", "foo");
24 format!("{:width$}", "foo", width = 8);
25 format!("{:+}", "foo"); // Warn when the format makes no difference.
26 format!("{:<}", "foo"); // Warn when the format makes no difference.
27 format!("foo {}", "bar");
28 format!("{} bar", "foo");
29
30 let arg: String = "".to_owned();
31 format!("{}", arg);
32 format!("{:?}", arg); // Don't warn about debug.
33 format!("{:8}", arg);
34 format!("{:width$}", arg, width = 8);
35 format!("{:+}", arg); // Warn when the format makes no difference.
36 format!("{:<}", arg); // Warn when the format makes no difference.
37 format!("foo {}", arg);
38 format!("{} bar", arg);
39
40 // We don’t want to warn for non-string args; see issue #697.
41 format!("{}", 42);
42 format!("{:?}", 42);
43 format!("{:+}", 42);
44 format!("foo {}", 42);
45 format!("{} bar", 42);
46
47 // We only want to warn about `format!` itself.
48 println!("foo");
49 println!("{}", "foo");
50 println!("foo {}", "foo");
51 println!("{}", 42);
52 println!("foo {}", 42);
53
54 // A `format!` inside a macro should not trigger a warning.
55 foo!("should not warn");
56
57 // Precision on string means slicing without panicking on size.
58 format!("{:.1}", "foo"); // Could be `"foo"[..1]`
59 format!("{:.10}", "foo"); // Could not be `"foo"[..10]`
60 format!("{:.prec$}", "foo", prec = 1);
61 format!("{:.prec$}", "foo", prec = 10);
62
63 format!("{}", 42.to_string());
64 let x = std::path::PathBuf::from("/bar/foo/qux");
65 format!("{}", x.display().to_string());
66
67 // False positive
68 let a = "foo".to_string();
69 let _ = Some(format!("{}", a + "bar"));
cdc7bbd5
XL
70
71 // Wrap it with braces
72 let v: Vec<String> = vec!["foo".to_string(), "bar".to_string()];
73 let _s: String = format!("{}", &*v.join("\n"));
f20569fa 74}