]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/format_args.fixed
Merge commit 'ac0e10aa68325235069a842f47499852b2dee79e' into clippyup
[rust.git] / src / tools / clippy / 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::eq_op,
7     clippy::print_literal,
8     clippy::uninlined_format_args
9 )]
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());
77     let _ = write!(
78         stdout(),
79         "error: something failed at {}",
80         Location::caller()
81     );
82     let _ = writeln!(
83         stdout(),
84         "error: something failed at {}",
85         Location::caller()
86     );
87     print!("error: something failed at {}", Location::caller());
88     println!("error: something failed at {}", Location::caller());
89     eprint!("error: something failed at {}", Location::caller());
90     eprintln!("error: something failed at {}", Location::caller());
91     let _ = format_args!("error: something failed at {}", Location::caller());
92     assert!(true, "error: something failed at {}", Location::caller());
93     assert_eq!(0, 0, "error: something failed at {}", Location::caller());
94     assert_ne!(0, 0, "error: something failed at {}", Location::caller());
95     panic!("error: something failed at {}", Location::caller());
96     println!("{}", *X(1));
97     println!("{}", ***Y(&X(1)));
98     println!("{}", Z(1));
99     println!("{}", **x);
100     println!("{}", ***x_ref);
101     // https://github.com/rust-lang/rust-clippy/issues/7903
102     println!("{foo}{bar}", foo = "foo", bar = "bar");
103     println!("{foo}{bar}", foo = "foo", bar = "bar");
104     println!("{foo}{bar}", bar = "bar", foo = "foo");
105     println!("{foo}{bar}", bar = "bar", foo = "foo");
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 }
118
119 fn issue8643(vendor_id: usize, product_id: usize, name: &str) {
120     println!(
121         "{:<9}  {:<10}  {}",
122         format!("0x{:x}", vendor_id),
123         format!("0x{:x}", product_id),
124         name
125     );
126 }
127
128 // https://github.com/rust-lang/rust-clippy/issues/8855
129 mod issue_8855 {
130     #![allow(dead_code)]
131
132     struct A {}
133
134     impl std::fmt::Display for A {
135         fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
136             write!(f, "test")
137         }
138     }
139
140     fn main() {
141         let a = A {};
142         let b = A {};
143
144         let x = format!("{} {}", a, b);
145         dbg!(x);
146
147         let x = format!("{:>6} {:>6}", a, b.to_string());
148         dbg!(x);
149     }
150 }
151
152 // https://github.com/rust-lang/rust-clippy/issues/9256
153 mod issue_9256 {
154     #![allow(dead_code)]
155
156     fn print_substring(original: &str) {
157         assert!(original.len() > 10);
158         println!("{}", &original[..10]);
159     }
160
161     fn main() {
162         print_substring("Hello, world!");
163     }
164 }