about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--clippy_lints/src/literal_string_with_formatting_args.rs9
-rw-r--r--tests/ui/{literal_string_with_formatting_args}.rs14
2 files changed, 23 insertions, 0 deletions
diff --git a/clippy_lints/src/literal_string_with_formatting_args.rs b/clippy_lints/src/literal_string_with_formatting_args.rs
index 4c8063ee6e6..c0d016ce08b 100644
--- a/clippy_lints/src/literal_string_with_formatting_args.rs
+++ b/clippy_lints/src/literal_string_with_formatting_args.rs
@@ -8,6 +8,7 @@ use rustc_span::{BytePos, Span};
 
 use clippy_utils::diagnostics::span_lint;
 use clippy_utils::mir::enclosing_mir;
+use clippy_utils::source::snippet_opt;
 
 declare_clippy_lint! {
     /// ### What it does
@@ -95,7 +96,15 @@ impl LateLintPass<'_> for LiteralStringWithFormattingArg {
                 },
                 _ => return,
             };
+            let Some(snippet) = snippet_opt(cx, expr.span) else {
+                return;
+            };
             let fmt_str = symbol.as_str();
+            // If the literal has been generated by the macro, the snippet should not contain it,
+            // allowing us to skip it.
+            if !snippet.contains(fmt_str) {
+                return;
+            }
             let lo = expr.span.lo();
             let mut current = fmt_str;
             let mut diff_len = 0;
diff --git a/tests/ui/{literal_string_with_formatting_args}.rs b/tests/ui/{literal_string_with_formatting_args}.rs
new file mode 100644
index 00000000000..8a06d0c7b36
--- /dev/null
+++ b/tests/ui/{literal_string_with_formatting_args}.rs
@@ -0,0 +1,14 @@
+// Regression test for <https://github.com/rust-lang/rust-clippy/issues/13885>.
+// The `dbg` macro generates a literal with the name of the current file, so
+// we need to ensure the lint is not emitted in this case.
+
+#![crate_name = "foo"]
+#![allow(unused)]
+#![warn(clippy::literal_string_with_formatting_args)]
+
+fn another_bad() {
+    let literal_string_with_formatting_args = 0;
+    dbg!("something");
+}
+
+fn main() {}