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.
12 #![allow(clippy::print_literal)]
13 #![warn(clippy::useless_format)]
15 struct Foo(pub String);
18 ($($t:tt)*) => (Foo(format!($($t)*)))
25 format!("{:?}", "foo"); // don't warn about debug
26 format!("{:8}", "foo");
27 format!("{:width$}", "foo", width = 8);
28 format!("{:+}", "foo"); // warn when the format makes no difference
29 format!("{:<}", "foo"); // warn when the format makes no difference
30 format!("foo {}", "bar");
31 format!("{} bar", "foo");
33 let arg: String = "".to_owned();
35 format!("{:?}", arg); // don't warn about debug
37 format!("{:width$}", arg, width = 8);
38 format!("{:+}", arg); // warn when the format makes no difference
39 format!("{:<}", arg); // warn when the format makes no difference
40 format!("foo {}", arg);
41 format!("{} bar", arg);
43 // we don’t want to warn for non-string args, see #697
47 format!("foo {}", 42);
48 format!("{} bar", 42);
50 // we only want to warn about `format!` itself
52 println!("{}", "foo");
53 println!("foo {}", "foo");
55 println!("foo {}", 42);
57 // A format! inside a macro should not trigger a warning
58 foo!("should not warn");
60 // precision on string means slicing without panicking on size:
61 format!("{:.1}", "foo"); // could be "foo"[..1]
62 format!("{:.10}", "foo"); // could not be "foo"[..10]
63 format!("{:.prec$}", "foo", prec = 1);
64 format!("{:.prec$}", "foo", prec = 10);
66 format!("{}", 42.to_string());
67 let x = std::path::PathBuf::from("/bar/foo/qux");
68 format!("{}", x.display().to_string());