]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/format_args.rs
Auto merge of #83846 - torhovland:issue-10971, r=davidtwco
[rust.git] / src / tools / clippy / tests / ui / format_args.rs
1 // run-rustfix
2
3 #![allow(unreachable_code)]
4 #![allow(unused_macros)]
5 #![allow(unused_variables)]
6 #![allow(clippy::assertions_on_constants)]
7 #![allow(clippy::eq_op)]
8 #![allow(clippy::print_literal)]
9 #![warn(clippy::to_string_in_format_args)]
10
11 use std::io::{stdout, Write};
12 use std::ops::Deref;
13 use std::panic::Location;
14
15 struct Somewhere;
16
17 impl ToString for Somewhere {
18     fn to_string(&self) -> String {
19         String::from("somewhere")
20     }
21 }
22
23 struct X(u32);
24
25 impl Deref for X {
26     type Target = u32;
27
28     fn deref(&self) -> &u32 {
29         &self.0
30     }
31 }
32
33 struct Y<'a>(&'a X);
34
35 impl<'a> Deref for Y<'a> {
36     type Target = &'a X;
37
38     fn deref(&self) -> &Self::Target {
39         &self.0
40     }
41 }
42
43 struct Z(u32);
44
45 impl Deref for Z {
46     type Target = u32;
47
48     fn deref(&self) -> &u32 {
49         &self.0
50     }
51 }
52
53 impl std::fmt::Display for Z {
54     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55         write!(f, "Z")
56     }
57 }
58
59 macro_rules! my_macro {
60     () => {
61         // here be dragons, do not enter (or lint)
62         println!("error: something failed at {}", Location::caller().to_string());
63     };
64 }
65
66 macro_rules! my_other_macro {
67     () => {
68         Location::caller().to_string()
69     };
70 }
71
72 fn main() {
73     let x = &X(1);
74     let x_ref = &x;
75
76     let _ = format!("error: something failed at {}", Location::caller().to_string());
77     let _ = write!(
78         stdout(),
79         "error: something failed at {}",
80         Location::caller().to_string()
81     );
82     let _ = writeln!(
83         stdout(),
84         "error: something failed at {}",
85         Location::caller().to_string()
86     );
87     print!("error: something failed at {}", Location::caller().to_string());
88     println!("error: something failed at {}", Location::caller().to_string());
89     eprint!("error: something failed at {}", Location::caller().to_string());
90     eprintln!("error: something failed at {}", Location::caller().to_string());
91     let _ = format_args!("error: something failed at {}", Location::caller().to_string());
92     assert!(true, "error: something failed at {}", Location::caller().to_string());
93     assert_eq!(0, 0, "error: something failed at {}", Location::caller().to_string());
94     assert_ne!(0, 0, "error: something failed at {}", Location::caller().to_string());
95     panic!("error: something failed at {}", Location::caller().to_string());
96     println!("{}", X(1).to_string());
97     println!("{}", Y(&X(1)).to_string());
98     println!("{}", Z(1).to_string());
99     println!("{}", x.to_string());
100     println!("{}", x_ref.to_string());
101     // https://github.com/rust-lang/rust-clippy/issues/7903
102     println!("{foo}{bar}", foo = "foo".to_string(), bar = "bar");
103     println!("{foo}{bar}", foo = "foo", bar = "bar".to_string());
104     println!("{foo}{bar}", bar = "bar".to_string(), foo = "foo");
105     println!("{foo}{bar}", bar = "bar", foo = "foo".to_string());
106
107     // negative tests
108     println!("error: something failed at {}", Somewhere.to_string());
109     // The next two tests are negative because caching the string might be faster than calling `<X as
110     // Display>::fmt` twice.
111     println!("{} and again {0}", x.to_string());
112     println!("{foo}{foo}", foo = "foo".to_string());
113     my_macro!();
114     println!("error: something failed at {}", my_other_macro!());
115     // https://github.com/rust-lang/rust-clippy/issues/7903
116     println!("{foo}{foo:?}", foo = "foo".to_string());
117 }