4 #![allow(clippy::assertions_on_constants)]
5 #![allow(clippy::eq_op)]
6 #![allow(clippy::print_literal)]
7 #![warn(clippy::to_string_in_format_args)]
9 use std::io::{stdout, Write};
11 use std::panic::Location;
15 impl ToString for Somewhere {
16 fn to_string(&self) -> String {
17 String::from("somewhere")
26 fn deref(&self) -> &u32 {
33 impl<'a> Deref for Y<'a> {
36 fn deref(&self) -> &Self::Target {
46 fn deref(&self) -> &u32 {
51 impl std::fmt::Display for Z {
52 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 macro_rules! my_macro {
59 // here be dragons, do not enter (or lint)
60 println!("error: something failed at {}", Location::caller().to_string());
64 macro_rules! my_other_macro {
66 Location::caller().to_string()
74 let _ = format!("error: something failed at {}", Location::caller());
77 "error: something failed at {}",
82 "error: something failed at {}",
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)));
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");
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());
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());
117 fn issue8643(vendor_id: usize, product_id: usize, name: &str) {
120 format!("0x{:x}", vendor_id),
121 format!("0x{:x}", product_id),
126 // https://github.com/rust-lang/rust-clippy/issues/8855
132 impl std::fmt::Display for A {
133 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
142 let x = format!("{} {}", a, b);
145 let x = format!("{:>6} {:>6}", a, b.to_string());
150 // https://github.com/rust-lang/rust-clippy/issues/9256
154 fn print_substring(original: &str) {
155 assert!(original.len() > 10);
156 println!("{}", &original[..10]);
160 print_substring("Hello, world!");