]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/format.fixed
Rollup merge of #100928 - CleanCut:rustc_metadata_diagnostics, r=davidtwco
[rust.git] / src / tools / clippy / tests / ui / format.fixed
1 // run-rustfix
2
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 )]
10 #![warn(clippy::useless_format)]
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     "foo".to_string(); // Warn when the format makes no difference.
32     "foo".to_string(); // Warn when the format makes no difference.
33     format!("foo {}", "bar");
34     format!("{} bar", "foo");
35
36     let arg = String::new();
37     arg.to_string();
38     format!("{:?}", arg); // Don't warn about debug.
39     format!("{:8}", arg);
40     format!("{:width$}", arg, width = 8);
41     arg.to_string(); // Warn when the format makes no difference.
42     arg.to_string(); // Warn when the format makes no difference.
43     format!("foo {}", arg);
44     format!("{} bar", arg);
45
46     // We don’t want to warn for non-string args; see issue #697.
47     format!("{}", 42);
48     format!("{:?}", 42);
49     format!("{:+}", 42);
50     format!("foo {}", 42);
51     format!("{} bar", 42);
52
53     // We only want to warn about `format!` itself.
54     println!("foo");
55     println!("{}", "foo");
56     println!("foo {}", "foo");
57     println!("{}", 42);
58     println!("foo {}", 42);
59
60     // A `format!` inside a macro should not trigger a warning.
61     foo!("should not warn");
62
63     // Precision on string means slicing without panicking on size.
64     format!("{:.1}", "foo"); // Could be `"foo"[..1]`
65     format!("{:.10}", "foo"); // Could not be `"foo"[..10]`
66     format!("{:.prec$}", "foo", prec = 1);
67     format!("{:.prec$}", "foo", prec = 10);
68
69     42.to_string();
70     let x = std::path::PathBuf::from("/bar/foo/qux");
71     x.display().to_string();
72
73     // False positive
74     let a = "foo".to_string();
75     let _ = Some(a + "bar");
76
77     // Wrap it with braces
78     let v: Vec<String> = vec!["foo".to_string(), "bar".to_string()];
79     let _s: String = (&*v.join("\n")).to_string();
80
81     format!("prepend {:+}", "s");
82
83     // Issue #8290
84     let x = "foo";
85     let _ = x.to_string();
86     let _ = format!("{x:?}"); // Don't lint on debug
87     let _ = x.to_string();
88
89     // Issue #9234
90     let abc = "abc";
91     let _ = abc.to_string();
92     let xx = "xx";
93     let _ = xx.to_string();
94 }