]> git.lizzy.rs Git - rust.git/blob - tests/ui/format.fixed
Don't lint `if_same_then_else` with `if let` conditions
[rust.git] / tests / ui / format.fixed
1 // run-rustfix
2
3 #![allow(clippy::print_literal, clippy::redundant_clone, clippy::to_string_in_format_args)]
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     r##"foo {}
17 " bar"##.to_string();
18
19     let _ = String::new();
20
21     "foo".to_string();
22     format!("{:?}", "foo"); // Don't warn about `Debug`.
23     format!("{:8}", "foo");
24     format!("{:width$}", "foo", width = 8);
25     "foo".to_string(); // Warn when the format makes no difference.
26     "foo".to_string(); // Warn when the format makes no difference.
27     format!("foo {}", "bar");
28     format!("{} bar", "foo");
29
30     let arg: String = "".to_owned();
31     arg.to_string();
32     format!("{:?}", arg); // Don't warn about debug.
33     format!("{:8}", arg);
34     format!("{:width$}", arg, width = 8);
35     arg.to_string(); // Warn when the format makes no difference.
36     arg.to_string(); // Warn when the format makes no difference.
37     format!("foo {}", arg);
38     format!("{} bar", arg);
39
40     // We don’t want to warn for non-string args; see issue #697.
41     format!("{}", 42);
42     format!("{:?}", 42);
43     format!("{:+}", 42);
44     format!("foo {}", 42);
45     format!("{} bar", 42);
46
47     // We only want to warn about `format!` itself.
48     println!("foo");
49     println!("{}", "foo");
50     println!("foo {}", "foo");
51     println!("{}", 42);
52     println!("foo {}", 42);
53
54     // A `format!` inside a macro should not trigger a warning.
55     foo!("should not warn");
56
57     // Precision on string means slicing without panicking on size.
58     format!("{:.1}", "foo"); // Could be `"foo"[..1]`
59     format!("{:.10}", "foo"); // Could not be `"foo"[..10]`
60     format!("{:.prec$}", "foo", prec = 1);
61     format!("{:.prec$}", "foo", prec = 10);
62
63     42.to_string();
64     let x = std::path::PathBuf::from("/bar/foo/qux");
65     x.display().to_string();
66
67     // False positive
68     let a = "foo".to_string();
69     let _ = Some(a + "bar");
70
71     // Wrap it with braces
72     let v: Vec<String> = vec!["foo".to_string(), "bar".to_string()];
73     let _s: String = (&*v.join("\n")).to_string();
74
75     format!("prepend {:+}", "s");
76
77     // Issue #8290
78     let x = "foo";
79     let _ = x.to_string();
80     let _ = format!("{x:?}"); // Don't lint on debug
81     let _ = x.to_string();
82 }