]> git.lizzy.rs Git - rust.git/blob - tests/ui/format.fixed
Fix `useless_format` suggestions
[rust.git] / tests / ui / format.fixed
1 // run-rustfix
2
3 #![allow(clippy::print_literal)]
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     "foo".to_string();
14
15     "foo".to_string();
16     format!("{:?}", "foo"); // don't warn about debug
17     format!("{:8}", "foo");
18     format!("{:width$}", "foo", width = 8);
19     "foo".to_string(); // warn when the format makes no difference
20     "foo".to_string(); // warn when the format makes no difference
21     format!("foo {}", "bar");
22     format!("{} bar", "foo");
23
24     let arg: String = "".to_owned();
25     arg.to_string();
26     format!("{:?}", arg); // don't warn about debug
27     format!("{:8}", arg);
28     format!("{:width$}", arg, width = 8);
29     arg.to_string(); // warn when the format makes no difference
30     arg.to_string(); // warn when the format makes no difference
31     format!("foo {}", arg);
32     format!("{} bar", arg);
33
34     // we don’t want to warn for non-string args, see #697
35     format!("{}", 42);
36     format!("{:?}", 42);
37     format!("{:+}", 42);
38     format!("foo {}", 42);
39     format!("{} bar", 42);
40
41     // we only want to warn about `format!` itself
42     println!("foo");
43     println!("{}", "foo");
44     println!("foo {}", "foo");
45     println!("{}", 42);
46     println!("foo {}", 42);
47
48     // A format! inside a macro should not trigger a warning
49     foo!("should not warn");
50
51     // precision on string means slicing without panicking on size:
52     format!("{:.1}", "foo"); // could be "foo"[..1]
53     format!("{:.10}", "foo"); // could not be "foo"[..10]
54     format!("{:.prec$}", "foo", prec = 1);
55     format!("{:.prec$}", "foo", prec = 10);
56
57     42.to_string();
58     let x = std::path::PathBuf::from("/bar/foo/qux");
59     x.display().to_string();
60 }