Correctly handle expanded macros for literal_string_with_formatting_args lint

This commit is contained in:
Guillaume Gomez 2025-01-07 15:55:41 +01:00
parent 17f9344a96
commit a7fb37c1d8
2 changed files with 23 additions and 0 deletions

View file

@ -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;

View file

@ -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() {}