5 clippy::redundant_clone,
6 clippy::to_string_in_format_args,
7 clippy::needless_borrow
9 #![warn(clippy::useless_format)]
11 struct Foo(pub String);
14 ($($t:tt)*) => (Foo(format!($($t)*)))
20 format!("{{}} abc {{}}");
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");
37 let arg: String = "".to_owned();
39 format!("{:?}", arg); // Don't warn about debug.
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);
47 // We don’t want to warn for non-string args; see issue #697.
51 format!("foo {}", 42);
52 format!("{} bar", 42);
54 // We only want to warn about `format!` itself.
56 println!("{}", "foo");
57 println!("foo {}", "foo");
59 println!("foo {}", 42);
61 // A `format!` inside a macro should not trigger a warning.
62 foo!("should not warn");
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);
70 format!("{}", 42.to_string());
71 let x = std::path::PathBuf::from("/bar/foo/qux");
72 format!("{}", x.display().to_string());
75 let a = "foo".to_string();
76 let _ = Some(format!("{}", a + "bar"));
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"));
82 format!("prepend {:+}", "s");
86 let _ = format!("{x}");
87 let _ = format!("{x:?}"); // Don't lint on debug
88 let _ = format!("{y}", y = x);
92 let _ = format!("{abc}");
94 let _ = format!("{xx}");