]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/format_args.rs
Rollup merge of #89876 - AlexApps99:const_ops, r=oli-obk
[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 #![warn(clippy::to_string_in_format_args)]
9
10 use std::io::{stdout, Write};
11 use std::ops::Deref;
12 use std::panic::Location;
13
14 struct Somewhere;
15
16 impl ToString for Somewhere {
17     fn to_string(&self) -> String {
18         String::from("somewhere")
19     }
20 }
21
22 struct X(u32);
23
24 impl Deref for X {
25     type Target = u32;
26
27     fn deref(&self) -> &u32 {
28         &self.0
29     }
30 }
31
32 struct Y<'a>(&'a X);
33
34 impl<'a> Deref for Y<'a> {
35     type Target = &'a X;
36
37     fn deref(&self) -> &Self::Target {
38         &self.0
39     }
40 }
41
42 struct Z(u32);
43
44 impl Deref for Z {
45     type Target = u32;
46
47     fn deref(&self) -> &u32 {
48         &self.0
49     }
50 }
51
52 impl std::fmt::Display for Z {
53     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54         write!(f, "Z")
55     }
56 }
57
58 macro_rules! my_macro {
59     () => {
60         // here be dragons, do not enter (or lint)
61         println!("error: something failed at {}", Location::caller().to_string());
62     };
63 }
64
65 macro_rules! my_other_macro {
66     () => {
67         Location::caller().to_string()
68     };
69 }
70
71 fn main() {
72     let x = &X(1);
73     let x_ref = &x;
74
75     let _ = format!("error: something failed at {}", Location::caller().to_string());
76     let _ = write!(
77         stdout(),
78         "error: something failed at {}",
79         Location::caller().to_string()
80     );
81     let _ = writeln!(
82         stdout(),
83         "error: something failed at {}",
84         Location::caller().to_string()
85     );
86     print!("error: something failed at {}", Location::caller().to_string());
87     println!("error: something failed at {}", Location::caller().to_string());
88     eprint!("error: something failed at {}", Location::caller().to_string());
89     eprintln!("error: something failed at {}", Location::caller().to_string());
90     let _ = format_args!("error: something failed at {}", Location::caller().to_string());
91     assert!(true, "error: something failed at {}", Location::caller().to_string());
92     assert_eq!(0, 0, "error: something failed at {}", Location::caller().to_string());
93     assert_ne!(0, 0, "error: something failed at {}", Location::caller().to_string());
94     panic!("error: something failed at {}", Location::caller().to_string());
95     println!("{}", X(1).to_string());
96     println!("{}", Y(&X(1)).to_string());
97     println!("{}", Z(1).to_string());
98     println!("{}", x.to_string());
99     println!("{}", x_ref.to_string());
100
101     println!("error: something failed at {}", Somewhere.to_string());
102     println!("{} and again {0}", x.to_string());
103     my_macro!();
104     println!("error: something failed at {}", my_other_macro!());
105 }