]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
1 | // run-rustfix |
2 | ||
3 | #![allow(clippy::print_literal, clippy::redundant_clone)] | |
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 | "foo".to_string(); | |
14 | "{}".to_string(); | |
15 | "{} abc {}".to_string(); | |
16 | r##"foo {} | |
17 | " bar"##.to_string(); | |
18 | ||
19 | "foo".to_string(); | |
20 | format!("{:?}", "foo"); // Don't warn about `Debug`. | |
21 | format!("{:8}", "foo"); | |
22 | format!("{:width$}", "foo", width = 8); | |
23 | "foo".to_string(); // Warn when the format makes no difference. | |
24 | "foo".to_string(); // Warn when the format makes no difference. | |
25 | format!("foo {}", "bar"); | |
26 | format!("{} bar", "foo"); | |
27 | ||
28 | let arg: String = "".to_owned(); | |
29 | arg.to_string(); | |
30 | format!("{:?}", arg); // Don't warn about debug. | |
31 | format!("{:8}", arg); | |
32 | format!("{:width$}", arg, width = 8); | |
33 | arg.to_string(); // Warn when the format makes no difference. | |
34 | arg.to_string(); // Warn when the format makes no difference. | |
35 | format!("foo {}", arg); | |
36 | format!("{} bar", arg); | |
37 | ||
38 | // We don’t want to warn for non-string args; see issue #697. | |
39 | format!("{}", 42); | |
40 | format!("{:?}", 42); | |
41 | format!("{:+}", 42); | |
42 | format!("foo {}", 42); | |
43 | format!("{} bar", 42); | |
44 | ||
45 | // We only want to warn about `format!` itself. | |
46 | println!("foo"); | |
47 | println!("{}", "foo"); | |
48 | println!("foo {}", "foo"); | |
49 | println!("{}", 42); | |
50 | println!("foo {}", 42); | |
51 | ||
52 | // A `format!` inside a macro should not trigger a warning. | |
53 | foo!("should not warn"); | |
54 | ||
55 | // Precision on string means slicing without panicking on size. | |
56 | format!("{:.1}", "foo"); // Could be `"foo"[..1]` | |
57 | format!("{:.10}", "foo"); // Could not be `"foo"[..10]` | |
58 | format!("{:.prec$}", "foo", prec = 1); | |
59 | format!("{:.prec$}", "foo", prec = 10); | |
60 | ||
61 | 42.to_string(); | |
62 | let x = std::path::PathBuf::from("/bar/foo/qux"); | |
63 | x.display().to_string(); | |
64 | ||
65 | // False positive | |
66 | let a = "foo".to_string(); | |
67 | let _ = Some(a + "bar"); | |
68 | } |