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