]> git.lizzy.rs Git - rust.git/blob - tests/ui/format.rs
Merge pull request #3265 from mikerite/fix-export
[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 #![feature(tool_lints)]
12 #![allow(clippy::print_literal)]
13 #![warn(clippy::useless_format)]
14
15 struct Foo(pub String);
16
17 macro_rules! foo {
18   ($($t:tt)*) => (Foo(format!($($t)*)))
19 }
20
21 fn main() {
22     format!("foo");
23
24     format!("{}", "foo");
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");
32
33     let arg: String = "".to_owned();
34     format!("{}", arg);
35     format!("{:?}", arg); // don't warn about debug
36     format!("{:8}", arg);
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);
42
43     // we don’t want to warn for non-string args, see #697
44     format!("{}", 42);
45     format!("{:?}", 42);
46     format!("{:+}", 42);
47     format!("foo {}", 42);
48     format!("{} bar", 42);
49
50     // we only want to warn about `format!` itself
51     println!("foo");
52     println!("{}", "foo");
53     println!("foo {}", "foo");
54     println!("{}", 42);
55     println!("foo {}", 42);
56
57     // A format! inside a macro should not trigger a warning
58     foo!("should not warn");
59
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);
65
66     format!("{}", 42.to_string());
67     let x = std::path::PathBuf::from("/bar/foo/qux");
68     format!("{}", x.display().to_string());
69 }