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