2019-02-24 05:30:08 +00:00
|
|
|
|
// run-rustfix
|
|
|
|
|
|
2022-01-27 14:12:45 +00:00
|
|
|
|
#![allow(
|
|
|
|
|
clippy::print_literal,
|
|
|
|
|
clippy::redundant_clone,
|
|
|
|
|
clippy::to_string_in_format_args,
|
|
|
|
|
clippy::needless_borrow
|
|
|
|
|
)]
|
2018-07-28 15:34:52 +00:00
|
|
|
|
#![warn(clippy::useless_format)]
|
2016-02-20 16:35:07 +00:00
|
|
|
|
|
2018-04-05 05:52:26 +00:00
|
|
|
|
struct Foo(pub String);
|
|
|
|
|
|
|
|
|
|
macro_rules! foo {
|
2019-03-10 17:19:47 +00:00
|
|
|
|
($($t:tt)*) => (Foo(format!($($t)*)))
|
2018-04-05 05:52:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-20 16:35:07 +00:00
|
|
|
|
fn main() {
|
2017-02-08 13:58:07 +00:00
|
|
|
|
format!("foo");
|
2019-04-09 14:15:48 +00:00
|
|
|
|
format!("{{}}");
|
|
|
|
|
format!("{{}} abc {{}}");
|
2019-08-23 08:01:41 +00:00
|
|
|
|
format!(
|
|
|
|
|
r##"foo {{}}
|
|
|
|
|
" bar"##
|
|
|
|
|
);
|
2016-02-22 16:54:46 +00:00
|
|
|
|
|
2021-11-04 12:52:36 +00:00
|
|
|
|
let _ = format!("");
|
|
|
|
|
|
2017-02-08 13:58:07 +00:00
|
|
|
|
format!("{}", "foo");
|
2019-01-31 01:15:29 +00:00
|
|
|
|
format!("{:?}", "foo"); // Don't warn about `Debug`.
|
2018-04-12 06:21:03 +00:00
|
|
|
|
format!("{:8}", "foo");
|
2018-10-02 21:54:50 +00:00
|
|
|
|
format!("{:width$}", "foo", width = 8);
|
2019-01-31 01:15:29 +00:00
|
|
|
|
format!("{:+}", "foo"); // Warn when the format makes no difference.
|
|
|
|
|
format!("{:<}", "foo"); // Warn when the format makes no difference.
|
2016-02-22 16:54:46 +00:00
|
|
|
|
format!("foo {}", "bar");
|
|
|
|
|
format!("{} bar", "foo");
|
|
|
|
|
|
|
|
|
|
let arg: String = "".to_owned();
|
2017-02-08 13:58:07 +00:00
|
|
|
|
format!("{}", arg);
|
2019-01-31 01:15:29 +00:00
|
|
|
|
format!("{:?}", arg); // Don't warn about debug.
|
2018-04-12 06:21:03 +00:00
|
|
|
|
format!("{:8}", arg);
|
2018-10-02 21:54:50 +00:00
|
|
|
|
format!("{:width$}", arg, width = 8);
|
2019-01-31 01:15:29 +00:00
|
|
|
|
format!("{:+}", arg); // Warn when the format makes no difference.
|
|
|
|
|
format!("{:<}", arg); // Warn when the format makes no difference.
|
2016-02-22 16:54:46 +00:00
|
|
|
|
format!("foo {}", arg);
|
|
|
|
|
format!("{} bar", arg);
|
|
|
|
|
|
2019-01-31 01:15:29 +00:00
|
|
|
|
// We don’t want to warn for non-string args; see issue #697.
|
2016-02-22 16:54:46 +00:00
|
|
|
|
format!("{}", 42);
|
|
|
|
|
format!("{:?}", 42);
|
|
|
|
|
format!("{:+}", 42);
|
2016-02-20 16:35:07 +00:00
|
|
|
|
format!("foo {}", 42);
|
2016-02-20 20:15:05 +00:00
|
|
|
|
format!("{} bar", 42);
|
2016-02-20 16:35:07 +00:00
|
|
|
|
|
2019-01-31 01:15:29 +00:00
|
|
|
|
// We only want to warn about `format!` itself.
|
2016-02-20 16:35:07 +00:00
|
|
|
|
println!("foo");
|
2016-02-22 16:54:46 +00:00
|
|
|
|
println!("{}", "foo");
|
|
|
|
|
println!("foo {}", "foo");
|
|
|
|
|
println!("{}", 42);
|
2016-02-20 16:35:07 +00:00
|
|
|
|
println!("foo {}", 42);
|
2018-04-05 05:52:26 +00:00
|
|
|
|
|
2019-01-31 01:15:29 +00:00
|
|
|
|
// A `format!` inside a macro should not trigger a warning.
|
2018-04-05 05:52:26 +00:00
|
|
|
|
foo!("should not warn");
|
2018-10-02 21:55:25 +00:00
|
|
|
|
|
2019-01-31 01:15:29 +00:00
|
|
|
|
// Precision on string means slicing without panicking on size.
|
2019-03-10 17:19:47 +00:00
|
|
|
|
format!("{:.1}", "foo"); // Could be `"foo"[..1]`
|
|
|
|
|
format!("{:.10}", "foo"); // Could not be `"foo"[..10]`
|
2018-10-02 21:55:25 +00:00
|
|
|
|
format!("{:.prec$}", "foo", prec = 1);
|
|
|
|
|
format!("{:.prec$}", "foo", prec = 10);
|
2018-10-03 18:59:59 +00:00
|
|
|
|
|
|
|
|
|
format!("{}", 42.to_string());
|
|
|
|
|
let x = std::path::PathBuf::from("/bar/foo/qux");
|
|
|
|
|
format!("{}", x.display().to_string());
|
2019-08-23 08:46:23 +00:00
|
|
|
|
|
|
|
|
|
// False positive
|
|
|
|
|
let a = "foo".to_string();
|
|
|
|
|
let _ = Some(format!("{}", a + "bar"));
|
2021-04-22 09:31:13 +00:00
|
|
|
|
|
|
|
|
|
// Wrap it with braces
|
|
|
|
|
let v: Vec<String> = vec!["foo".to_string(), "bar".to_string()];
|
|
|
|
|
let _s: String = format!("{}", &*v.join("\n"));
|
2021-07-15 08:44:10 +00:00
|
|
|
|
|
|
|
|
|
format!("prepend {:+}", "s");
|
2022-01-17 12:29:07 +00:00
|
|
|
|
|
|
|
|
|
// Issue #8290
|
|
|
|
|
let x = "foo";
|
|
|
|
|
let _ = format!("{x}");
|
|
|
|
|
let _ = format!("{x:?}"); // Don't lint on debug
|
|
|
|
|
let _ = format!("{y}", y = x);
|
2022-07-28 17:08:22 +00:00
|
|
|
|
|
|
|
|
|
// Issue #9234
|
|
|
|
|
let abc = "abc";
|
|
|
|
|
let _ = format!("{abc}");
|
|
|
|
|
let xx = "xx";
|
|
|
|
|
let _ = format!("{xx}");
|
2016-02-20 16:35:07 +00:00
|
|
|
|
}
|