3 #![allow(clippy::print_literal, clippy::redundant_clone, clippy::to_string_in_format_args)]
4 #![warn(clippy::useless_format)]
6 struct Foo(pub String);
9 ($($t:tt)*) => (Foo(format!($($t)*)))
15 format!("{{}} abc {{}}");
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");
32 let arg: String = "".to_owned();
34 format!("{:?}", arg); // Don't warn about debug.
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);
42 // We don’t want to warn for non-string args; see issue #697.
46 format!("foo {}", 42);
47 format!("{} bar", 42);
49 // We only want to warn about `format!` itself.
51 println!("{}", "foo");
52 println!("foo {}", "foo");
54 println!("foo {}", 42);
56 // A `format!` inside a macro should not trigger a warning.
57 foo!("should not warn");
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);
65 format!("{}", 42.to_string());
66 let x = std::path::PathBuf::from("/bar/foo/qux");
67 format!("{}", x.display().to_string());
70 let a = "foo".to_string();
71 let _ = Some(format!("{}", a + "bar"));
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"));
77 format!("prepend {:+}", "s");