Skip to content

Commit 95b295d

Browse files
committed
Fix to_string_in_format_args with macro call receiver
1 parent 76856ff commit 95b295d

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

clippy_lints/src/format_args.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -424,14 +424,14 @@ impl<'a, 'tcx> FormatArgsExpr<'a, 'tcx> {
424424
count_needed_derefs(receiver_ty, cx.typeck_results().expr_adjustments(receiver).iter())
425425
&& implements_trait(cx, target, display_trait_id, &[])
426426
&& let Some(sized_trait_id) = cx.tcx.lang_items().sized_trait()
427-
&& let Some(receiver_snippet) = snippet_opt(cx, receiver.span)
427+
&& let Some(receiver_snippet) = snippet_opt(cx, receiver.span.source_callsite())
428428
{
429429
let needs_ref = !implements_trait(cx, receiver_ty, sized_trait_id, &[]);
430430
if n_needed_derefs == 0 && !needs_ref {
431431
span_lint_and_sugg(
432432
cx,
433433
TO_STRING_IN_FORMAT_ARGS,
434-
to_string_span.with_lo(receiver.span.hi()),
434+
to_string_span.with_lo(receiver.span.source_callsite().hi()),
435435
format!("`to_string` applied to a type that implements `Display` in `{name}!` args"),
436436
"remove this",
437437
String::new(),

tests/ui/format_args.fixed

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ fn main() {
104104
println!("{foo}{bar}", foo = "foo", bar = "bar");
105105
println!("{foo}{bar}", bar = "bar", foo = "foo");
106106
println!("{foo}{bar}", bar = "bar", foo = "foo");
107+
println!("{}", my_other_macro!());
107108

108109
// negative tests
109110
println!("error: something failed at {}", Somewhere.to_string());

tests/ui/format_args.rs

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ fn main() {
104104
println!("{foo}{bar}", foo = "foo", bar = "bar".to_string());
105105
println!("{foo}{bar}", bar = "bar".to_string(), foo = "foo");
106106
println!("{foo}{bar}", bar = "bar", foo = "foo".to_string());
107+
println!("{}", my_other_macro!().to_string());
107108

108109
// negative tests
109110
println!("error: something failed at {}", Somewhere.to_string());

tests/ui/format_args.stderr

+11-5
Original file line numberDiff line numberDiff line change
@@ -127,29 +127,35 @@ error: `to_string` applied to a type that implements `Display` in `println!` arg
127127
LL | println!("{foo}{bar}", bar = "bar", foo = "foo".to_string());
128128
| ^^^^^^^^^^^^ help: remove this
129129

130+
error: `to_string` applied to a type that implements `Display` in `println!` args
131+
--> tests/ui/format_args.rs:107:37
132+
|
133+
LL | println!("{}", my_other_macro!().to_string());
134+
| ^^^^^^^^^^^^ help: remove this
135+
130136
error: `to_string` applied to a type that implements `Display` in `print!` args
131-
--> tests/ui/format_args.rs:118:37
137+
--> tests/ui/format_args.rs:119:37
132138
|
133139
LL | print!("{}", (Location::caller().to_string()));
134140
| ^^^^^^^^^^^^ help: remove this
135141

136142
error: `to_string` applied to a type that implements `Display` in `print!` args
137-
--> tests/ui/format_args.rs:119:39
143+
--> tests/ui/format_args.rs:120:39
138144
|
139145
LL | print!("{}", ((Location::caller()).to_string()));
140146
| ^^^^^^^^^^^^ help: remove this
141147

142148
error: `to_string` applied to a type that implements `Display` in `format!` args
143-
--> tests/ui/format_args.rs:147:38
149+
--> tests/ui/format_args.rs:148:38
144150
|
145151
LL | let x = format!("{} {}", a, b.to_string());
146152
| ^^^^^^^^^^^^ help: remove this
147153

148154
error: `to_string` applied to a type that implements `Display` in `println!` args
149-
--> tests/ui/format_args.rs:161:24
155+
--> tests/ui/format_args.rs:162:24
150156
|
151157
LL | println!("{}", original[..10].to_string());
152158
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use this: `&original[..10]`
153159

154-
error: aborting due to 25 previous errors
160+
error: aborting due to 26 previous errors
155161

0 commit comments

Comments
 (0)