2 #![warn(clippy::to_string_in_format_args)]
5 clippy::assertions_on_constants,
9 clippy::uninlined_format_args
12 use std::io::{stdout, Write};
14 use std::panic::Location;
18 impl ToString for Somewhere {
19 fn to_string(&self) -> String {
20 String::from("somewhere")
29 fn deref(&self) -> &u32 {
36 impl<'a> Deref for Y<'a> {
39 fn deref(&self) -> &Self::Target {
49 fn deref(&self) -> &u32 {
54 impl std::fmt::Display for Z {
55 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60 macro_rules! my_macro {
62 // here be dragons, do not enter (or lint)
63 println!("error: something failed at {}", Location::caller().to_string());
67 macro_rules! my_other_macro {
69 Location::caller().to_string()
77 let _ = format!("error: something failed at {}", Location::caller());
80 "error: something failed at {}",
85 "error: something failed at {}",
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)));
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");
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());
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())));
122 fn issue8643(vendor_id: usize, product_id: usize, name: &str) {
125 format!("0x{:x}", vendor_id),
126 format!("0x{:x}", product_id),
131 // https://github.com/rust-lang/rust-clippy/issues/8855
137 impl std::fmt::Display for A {
138 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
147 let x = format!("{} {}", a, b);
150 let x = format!("{:>6} {:>6}", a, b.to_string());
155 // https://github.com/rust-lang/rust-clippy/issues/9256
159 fn print_substring(original: &str) {
160 assert!(original.len() > 10);
161 println!("{}", &original[..10]);
165 print_substring("Hello, world!");