]> git.lizzy.rs Git - rust.git/blob - tests/ui/format.fixed
Re-add false positive check
[rust.git] / tests / ui / format.fixed
1 // run-rustfix
2
3 #![allow(clippy::print_literal)]
4 #![warn(clippy::useless_format)]
5
6 struct Foo(pub String);
7
8 macro_rules! foo {
9     ($($t:tt)*) => (Foo(format!($($t)*)))
10 }
11
12 fn main() {
13     "foo".to_string();
14     "{}".to_string();
15     "{} abc {}".to_string();
16     "foo {}\n\" bar".to_string();
17
18     "foo".to_string();
19     format!("{:?}", "foo"); // Don't warn about `Debug`.
20     format!("{:8}", "foo");
21     format!("{:width$}", "foo", width = 8);
22     "foo".to_string(); // Warn when the format makes no difference.
23     "foo".to_string(); // Warn when the format makes no difference.
24     format!("foo {}", "bar");
25     format!("{} bar", "foo");
26
27     let arg: String = "".to_owned();
28     arg.to_string();
29     format!("{:?}", arg); // Don't warn about debug.
30     format!("{:8}", arg);
31     format!("{:width$}", arg, width = 8);
32     arg.to_string(); // Warn when the format makes no difference.
33     arg.to_string(); // Warn when the format makes no difference.
34     format!("foo {}", arg);
35     format!("{} bar", arg);
36
37     // We don’t want to warn for non-string args; see issue #697.
38     format!("{}", 42);
39     format!("{:?}", 42);
40     format!("{:+}", 42);
41     format!("foo {}", 42);
42     format!("{} bar", 42);
43
44     // We only want to warn about `format!` itself.
45     println!("foo");
46     println!("{}", "foo");
47     println!("foo {}", "foo");
48     println!("{}", 42);
49     println!("foo {}", 42);
50
51     // A `format!` inside a macro should not trigger a warning.
52     foo!("should not warn");
53
54     // Precision on string means slicing without panicking on size.
55     format!("{:.1}", "foo"); // Could be `"foo"[..1]`
56     format!("{:.10}", "foo"); // Could not be `"foo"[..10]`
57     format!("{:.prec$}", "foo", prec = 1);
58     format!("{:.prec$}", "foo", prec = 10);
59
60     42.to_string();
61     let x = std::path::PathBuf::from("/bar/foo/qux");
62     x.display().to_string();
63
64     // False positive
65     let a = "foo".to_string();
66     let _ = Some(a + "bar");
67 }