]> git.lizzy.rs Git - rust.git/blob - tests/ui/format.rs
removing unsafe from test fn's && renaming shrink to sugg_span
[rust.git] / tests / ui / format.rs
1 // run-rustfix
2
3 #![allow(clippy::print_literal, clippy::redundant_clone, clippy::to_string_in_format_args)]
4 #![warn(clippy::useless_format)]
5
6 struct Foo(pub String);
7
8 macro_rules! foo {
9     ($($t:tt)*) => (Foo(format!($($t)*)))
10 }
11
12 fn main() {
13     format!("foo");
14     format!("{{}}");
15     format!("{{}} abc {{}}");
16     format!(
17         r##"foo {{}}
18 " bar"##
19     );
20
21     let _ = format!("");
22
23     format!("{}", "foo");
24     format!("{:?}", "foo"); // Don't warn about `Debug`.
25     format!("{:8}", "foo");
26     format!("{:width$}", "foo", width = 8);
27     format!("{:+}", "foo"); // Warn when the format makes no difference.
28     format!("{:<}", "foo"); // Warn when the format makes no difference.
29     format!("foo {}", "bar");
30     format!("{} bar", "foo");
31
32     let arg: String = "".to_owned();
33     format!("{}", arg);
34     format!("{:?}", arg); // Don't warn about debug.
35     format!("{:8}", arg);
36     format!("{:width$}", arg, width = 8);
37     format!("{:+}", arg); // Warn when the format makes no difference.
38     format!("{:<}", arg); // Warn when the format makes no difference.
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     format!("{}", 42.to_string());
66     let x = std::path::PathBuf::from("/bar/foo/qux");
67     format!("{}", x.display().to_string());
68
69     // False positive
70     let a = "foo".to_string();
71     let _ = Some(format!("{}", a + "bar"));
72
73     // Wrap it with braces
74     let v: Vec<String> = vec!["foo".to_string(), "bar".to_string()];
75     let _s: String = format!("{}", &*v.join("\n"));
76
77     format!("prepend {:+}", "s");
78 }