]> git.lizzy.rs Git - rust.git/blob - tests/ui/format.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / format.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
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.
9
10
11 #![allow(clippy::print_literal)]
12 #![warn(clippy::useless_format)]
13
14 struct Foo(pub String);
15
16 macro_rules! foo {
17   ($($t:tt)*) => (Foo(format!($($t)*)))
18 }
19
20 fn main() {
21     format!("foo");
22
23     format!("{}", "foo");
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");
31
32     let arg: String = "".to_owned();
33     format!("{}", arg);
34     format!("{:?}", arg); // don't warn about debug
35     format!("{:8}", arg);
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);
41
42     // we don’t want to warn for non-string args, see #697
43     format!("{}", 42);
44     format!("{:?}", 42);
45     format!("{:+}", 42);
46     format!("foo {}", 42);
47     format!("{} bar", 42);
48
49     // we only want to warn about `format!` itself
50     println!("foo");
51     println!("{}", "foo");
52     println!("foo {}", "foo");
53     println!("{}", 42);
54     println!("foo {}", 42);
55
56     // A format! inside a macro should not trigger a warning
57     foo!("should not warn");
58
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);
64
65     format!("{}", 42.to_string());
66     let x = std::path::PathBuf::from("/bar/foo/qux");
67     format!("{}", x.display().to_string());
68 }