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