Skip to content

Commit

Permalink
Correctly handle expanded macros for `literal_string_with_formatting_…
Browse files Browse the repository at this point in the history
…args` lint
  • Loading branch information
GuillaumeGomez committed Jan 7, 2025
1 parent 17f9344 commit a7fb37c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
9 changes: 9 additions & 0 deletions clippy_lints/src/literal_string_with_formatting_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/{literal_string_with_formatting_args}.rs
Original file line number Diff line number Diff line change
@@ -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() {}

0 comments on commit a7fb37c

Please sign in to comment.