about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-06-07 09:52:17 +0000
committerbors <bors@rust-lang.org>2024-06-07 09:52:17 +0000
commitf990ea155e1c6b338048db3fd2841c686cadc352 (patch)
tree50c85a085251fae6f6709d22233622be90400da5
parent0f87a81882f1a110b6d11c18a2223723984d10dd (diff)
parent95b295d9768fc492540d415c34c7f59b265967c1 (diff)
downloadrust-f990ea155e1c6b338048db3fd2841c686cadc352.tar.gz
rust-f990ea155e1c6b338048db3fd2841c686cadc352.zip
Auto merge of #12844 - Alexendoo:to-string-in-format-args, r=dswij
Fix `to_string_in_format_args` with macro call receiver

Fixes part of #12837

changelog: none
-rw-r--r--clippy_lints/src/format_args.rs4
-rw-r--r--tests/ui/format_args.fixed1
-rw-r--r--tests/ui/format_args.rs1
-rw-r--r--tests/ui/format_args.stderr16
4 files changed, 15 insertions, 7 deletions
diff --git a/clippy_lints/src/format_args.rs b/clippy_lints/src/format_args.rs
index 86115807aa4..99def199af0 100644
--- a/clippy_lints/src/format_args.rs
+++ b/clippy_lints/src/format_args.rs
@@ -424,14 +424,14 @@ impl<'a, 'tcx> FormatArgsExpr<'a, 'tcx> {
                 count_needed_derefs(receiver_ty, cx.typeck_results().expr_adjustments(receiver).iter())
             && implements_trait(cx, target, display_trait_id, &[])
             && let Some(sized_trait_id) = cx.tcx.lang_items().sized_trait()
-            && let Some(receiver_snippet) = snippet_opt(cx, receiver.span)
+            && let Some(receiver_snippet) = snippet_opt(cx, receiver.span.source_callsite())
         {
             let needs_ref = !implements_trait(cx, receiver_ty, sized_trait_id, &[]);
             if n_needed_derefs == 0 && !needs_ref {
                 span_lint_and_sugg(
                     cx,
                     TO_STRING_IN_FORMAT_ARGS,
-                    to_string_span.with_lo(receiver.span.hi()),
+                    to_string_span.with_lo(receiver.span.source_callsite().hi()),
                     format!("`to_string` applied to a type that implements `Display` in `{name}!` args"),
                     "remove this",
                     String::new(),
diff --git a/tests/ui/format_args.fixed b/tests/ui/format_args.fixed
index cab20b11e07..4d812f6bf1d 100644
--- a/tests/ui/format_args.fixed
+++ b/tests/ui/format_args.fixed
@@ -104,6 +104,7 @@ fn main() {
     println!("{foo}{bar}", foo = "foo", bar = "bar");
     println!("{foo}{bar}", bar = "bar", foo = "foo");
     println!("{foo}{bar}", bar = "bar", foo = "foo");
+    println!("{}", my_other_macro!());
 
     // negative tests
     println!("error: something failed at {}", Somewhere.to_string());
diff --git a/tests/ui/format_args.rs b/tests/ui/format_args.rs
index bc3645cb2c2..d242623feb6 100644
--- a/tests/ui/format_args.rs
+++ b/tests/ui/format_args.rs
@@ -104,6 +104,7 @@ fn main() {
     println!("{foo}{bar}", foo = "foo", bar = "bar".to_string());
     println!("{foo}{bar}", bar = "bar".to_string(), foo = "foo");
     println!("{foo}{bar}", bar = "bar", foo = "foo".to_string());
+    println!("{}", my_other_macro!().to_string());
 
     // negative tests
     println!("error: something failed at {}", Somewhere.to_string());
diff --git a/tests/ui/format_args.stderr b/tests/ui/format_args.stderr
index f20cf9eca2f..91a45e27008 100644
--- a/tests/ui/format_args.stderr
+++ b/tests/ui/format_args.stderr
@@ -127,29 +127,35 @@ error: `to_string` applied to a type that implements `Display` in `println!` arg
 LL |     println!("{foo}{bar}", bar = "bar", foo = "foo".to_string());
    |                                                    ^^^^^^^^^^^^ help: remove this
 
+error: `to_string` applied to a type that implements `Display` in `println!` args
+  --> tests/ui/format_args.rs:107:37
+   |
+LL |     println!("{}", my_other_macro!().to_string());
+   |                                     ^^^^^^^^^^^^ help: remove this
+
 error: `to_string` applied to a type that implements `Display` in `print!` args
-  --> tests/ui/format_args.rs:118:37
+  --> tests/ui/format_args.rs:119:37
    |
 LL |     print!("{}", (Location::caller().to_string()));
    |                                     ^^^^^^^^^^^^ help: remove this
 
 error: `to_string` applied to a type that implements `Display` in `print!` args
-  --> tests/ui/format_args.rs:119:39
+  --> tests/ui/format_args.rs:120:39
    |
 LL |     print!("{}", ((Location::caller()).to_string()));
    |                                       ^^^^^^^^^^^^ help: remove this
 
 error: `to_string` applied to a type that implements `Display` in `format!` args
-  --> tests/ui/format_args.rs:147:38
+  --> tests/ui/format_args.rs:148:38
    |
 LL |         let x = format!("{} {}", a, b.to_string());
    |                                      ^^^^^^^^^^^^ help: remove this
 
 error: `to_string` applied to a type that implements `Display` in `println!` args
-  --> tests/ui/format_args.rs:161:24
+  --> tests/ui/format_args.rs:162:24
    |
 LL |         println!("{}", original[..10].to_string());
    |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use this: `&original[..10]`
 
-error: aborting due to 25 previous errors
+error: aborting due to 26 previous errors