Skip to content

Commit a7fb37c

Browse files
Correctly handle expanded macros for literal_string_with_formatting_args lint
1 parent 17f9344 commit a7fb37c

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

clippy_lints/src/literal_string_with_formatting_args.rs

+9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_span::{BytePos, Span};
88

99
use clippy_utils::diagnostics::span_lint;
1010
use clippy_utils::mir::enclosing_mir;
11+
use clippy_utils::source::snippet_opt;
1112

1213
declare_clippy_lint! {
1314
/// ### What it does
@@ -95,7 +96,15 @@ impl LateLintPass<'_> for LiteralStringWithFormattingArg {
9596
},
9697
_ => return,
9798
};
99+
let Some(snippet) = snippet_opt(cx, expr.span) else {
100+
return;
101+
};
98102
let fmt_str = symbol.as_str();
103+
// If the literal has been generated by the macro, the snippet should not contain it,
104+
// allowing us to skip it.
105+
if !snippet.contains(fmt_str) {
106+
return;
107+
}
99108
let lo = expr.span.lo();
100109
let mut current = fmt_str;
101110
let mut diff_len = 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Regression test for <https://github.com/rust-lang/rust-clippy/issues/13885>.
2+
// The `dbg` macro generates a literal with the name of the current file, so
3+
// we need to ensure the lint is not emitted in this case.
4+
5+
#![crate_name = "foo"]
6+
#![allow(unused)]
7+
#![warn(clippy::literal_string_with_formatting_args)]
8+
9+
fn another_bad() {
10+
let literal_string_with_formatting_args = 0;
11+
dbg!("something");
12+
}
13+
14+
fn main() {}

0 commit comments

Comments
 (0)