From a4c08cdab81b6bb11fe8a14220fa8beec2e216ae Mon Sep 17 00:00:00 2001 From: yanglsh Date: Sun, 29 Dec 2024 09:33:16 -0700 Subject: [PATCH] fix `literal_string_with_formatting_args` FP on format inside assert --- .../src/literal_string_with_formatting_args.rs | 12 ++++++++++-- tests/ui/literal_string_with_formatting_arg.rs | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/literal_string_with_formatting_args.rs b/clippy_lints/src/literal_string_with_formatting_args.rs index 49353a1b76be..403f9f082f8a 100644 --- a/clippy_lints/src/literal_string_with_formatting_args.rs +++ b/clippy_lints/src/literal_string_with_formatting_args.rs @@ -1,5 +1,5 @@ use rustc_ast::{LitKind, StrStyle}; -use rustc_hir::{Expr, ExprKind}; +use rustc_hir::{Expr, ExprKind, Node}; use rustc_lexer::is_ident; use rustc_lint::{LateContext, LateLintPass}; use rustc_parse_format::{ParseMode, Parser, Piece}; @@ -81,7 +81,7 @@ fn emit_lint(cx: &LateContext<'_>, expr: &Expr<'_>, spans: &[(Span, Option for LiteralStringWithFormattingArg { fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { - if expr.span.from_expansion() { + if expr.span.from_expansion() || expr_enclosing_from_expansion(cx, expr) { return; } if let ExprKind::Lit(lit) = expr.kind { @@ -165,3 +165,11 @@ impl LateLintPass<'_> for LiteralStringWithFormattingArg { } } } + +fn expr_enclosing_from_expansion(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool { + cx.tcx + .hir() + .parent_iter(expr.hir_id) + .take_while(|(_, node)| matches!(node, Node::Expr(_))) + .any(|(_, node)| node.expect_expr().span.from_expansion()) +} diff --git a/tests/ui/literal_string_with_formatting_arg.rs b/tests/ui/literal_string_with_formatting_arg.rs index f257c66f59d3..a2f9ec0b1901 100644 --- a/tests/ui/literal_string_with_formatting_arg.rs +++ b/tests/ui/literal_string_with_formatting_arg.rs @@ -35,3 +35,9 @@ fn main() { let x: Option = Some(0); x.expect("{…}"); } + +fn issue_13885() { + let value = 0; + dbg!(format!("{value}").is_empty()); // Nothing should happen + assert!(format!("{value}").is_empty()); // Nothing should happen +}