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