Skip to content

Commit a4c08cd

Browse files
committed
fix literal_string_with_formatting_args FP on format inside assert
1 parent 998c780 commit a4c08cd

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

clippy_lints/src/literal_string_with_formatting_args.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_ast::{LitKind, StrStyle};
2-
use rustc_hir::{Expr, ExprKind};
2+
use rustc_hir::{Expr, ExprKind, Node};
33
use rustc_lexer::is_ident;
44
use rustc_lint::{LateContext, LateLintPass};
55
use rustc_parse_format::{ParseMode, Parser, Piece};
@@ -81,7 +81,7 @@ fn emit_lint(cx: &LateContext<'_>, expr: &Expr<'_>, spans: &[(Span, Option<Strin
8181

8282
impl LateLintPass<'_> for LiteralStringWithFormattingArg {
8383
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
84-
if expr.span.from_expansion() {
84+
if expr.span.from_expansion() || expr_enclosing_from_expansion(cx, expr) {
8585
return;
8686
}
8787
if let ExprKind::Lit(lit) = expr.kind {
@@ -165,3 +165,11 @@ impl LateLintPass<'_> for LiteralStringWithFormattingArg {
165165
}
166166
}
167167
}
168+
169+
fn expr_enclosing_from_expansion(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
170+
cx.tcx
171+
.hir()
172+
.parent_iter(expr.hir_id)
173+
.take_while(|(_, node)| matches!(node, Node::Expr(_)))
174+
.any(|(_, node)| node.expect_expr().span.from_expansion())
175+
}

tests/ui/literal_string_with_formatting_arg.rs

+6
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,9 @@ fn main() {
3535
let x: Option<usize> = Some(0);
3636
x.expect("{…}");
3737
}
38+
39+
fn issue_13885() {
40+
let value = 0;
41+
dbg!(format!("{value}").is_empty()); // Nothing should happen
42+
assert!(format!("{value}").is_empty()); // Nothing should happen
43+
}

0 commit comments

Comments
 (0)