1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
11 #![allow(clippy::print_literal)]
12 #![warn(clippy::useless_format)]
14 struct Foo(pub String);
17 ($($t:tt)*) => (Foo(format!($($t)*)))
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 #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());