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