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