]> git.lizzy.rs Git - rust.git/blob - tests/ui/format_args.fixed
Move MSRV tests into the lint specific test files
[rust.git] / tests / ui / format_args.fixed
1 // run-rustfix
2 #![warn(clippy::to_string_in_format_args)]
3 #![allow(unused)]
4 #![allow(
5     clippy::assertions_on_constants,
6     clippy::double_parens,
7     clippy::eq_op,
8     clippy::print_literal,
9     clippy::uninlined_format_args
10 )]
11
12 use std::io::{stdout, Write};
13 use std::ops::Deref;
14 use std::panic::Location;
15
16 struct Somewhere;
17
18 impl ToString for Somewhere {
19     fn to_string(&self) -> String {
20         String::from("somewhere")
21     }
22 }
23
24 struct X(u32);
25
26 impl Deref for X {
27     type Target = u32;
28
29     fn deref(&self) -> &u32 {
30         &self.0
31     }
32 }
33
34 struct Y<'a>(&'a X);
35
36 impl<'a> Deref for Y<'a> {
37     type Target = &'a X;
38
39     fn deref(&self) -> &Self::Target {
40         &self.0
41     }
42 }
43
44 struct Z(u32);
45
46 impl Deref for Z {
47     type Target = u32;
48
49     fn deref(&self) -> &u32 {
50         &self.0
51     }
52 }
53
54 impl std::fmt::Display for Z {
55     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56         write!(f, "Z")
57     }
58 }
59
60 macro_rules! my_macro {
61     () => {
62         // here be dragons, do not enter (or lint)
63         println!("error: something failed at {}", Location::caller().to_string());
64     };
65 }
66
67 macro_rules! my_other_macro {
68     () => {
69         Location::caller().to_string()
70     };
71 }
72
73 fn main() {
74     let x = &X(1);
75     let x_ref = &x;
76
77     let _ = format!("error: something failed at {}", Location::caller());
78     let _ = write!(
79         stdout(),
80         "error: something failed at {}",
81         Location::caller()
82     );
83     let _ = writeln!(
84         stdout(),
85         "error: something failed at {}",
86         Location::caller()
87     );
88     print!("error: something failed at {}", Location::caller());
89     println!("error: something failed at {}", Location::caller());
90     eprint!("error: something failed at {}", Location::caller());
91     eprintln!("error: something failed at {}", Location::caller());
92     let _ = format_args!("error: something failed at {}", Location::caller());
93     assert!(true, "error: something failed at {}", Location::caller());
94     assert_eq!(0, 0, "error: something failed at {}", Location::caller());
95     assert_ne!(0, 0, "error: something failed at {}", Location::caller());
96     panic!("error: something failed at {}", Location::caller());
97     println!("{}", *X(1));
98     println!("{}", ***Y(&X(1)));
99     println!("{}", Z(1));
100     println!("{}", **x);
101     println!("{}", ***x_ref);
102     // https://github.com/rust-lang/rust-clippy/issues/7903
103     println!("{foo}{bar}", foo = "foo", bar = "bar");
104     println!("{foo}{bar}", foo = "foo", bar = "bar");
105     println!("{foo}{bar}", bar = "bar", foo = "foo");
106     println!("{foo}{bar}", bar = "bar", foo = "foo");
107
108     // negative tests
109     println!("error: something failed at {}", Somewhere.to_string());
110     // The next two tests are negative because caching the string might be faster than calling `<X as
111     // Display>::fmt` twice.
112     println!("{} and again {0}", x.to_string());
113     println!("{foo}{foo}", foo = "foo".to_string());
114     my_macro!();
115     println!("error: something failed at {}", my_other_macro!());
116     // https://github.com/rust-lang/rust-clippy/issues/7903
117     println!("{foo}{foo:?}", foo = "foo".to_string());
118     print!("{}", (Location::caller()));
119     print!("{}", ((Location::caller())));
120 }
121
122 fn issue8643(vendor_id: usize, product_id: usize, name: &str) {
123     println!(
124         "{:<9}  {:<10}  {}",
125         format!("0x{:x}", vendor_id),
126         format!("0x{:x}", product_id),
127         name
128     );
129 }
130
131 // https://github.com/rust-lang/rust-clippy/issues/8855
132 mod issue_8855 {
133     #![allow(dead_code)]
134
135     struct A {}
136
137     impl std::fmt::Display for A {
138         fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
139             write!(f, "test")
140         }
141     }
142
143     fn main() {
144         let a = A {};
145         let b = A {};
146
147         let x = format!("{} {}", a, b);
148         dbg!(x);
149
150         let x = format!("{:>6} {:>6}", a, b.to_string());
151         dbg!(x);
152     }
153 }
154
155 // https://github.com/rust-lang/rust-clippy/issues/9256
156 mod issue_9256 {
157     #![allow(dead_code)]
158
159     fn print_substring(original: &str) {
160         assert!(original.len() > 10);
161         println!("{}", &original[..10]);
162     }
163
164     fn main() {
165         print_substring("Hello, world!");
166     }
167 }