Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix literal_string_with_formatting_args FP on format inside assert #13898

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions clippy_lints/src/literal_string_with_formatting_args.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -81,7 +81,7 @@ fn emit_lint(cx: &LateContext<'_>, expr: &Expr<'_>, spans: &[(Span, Option<Strin

impl LateLintPass<'_> 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 {
Expand Down Expand Up @@ -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())
}
6 changes: 6 additions & 0 deletions tests/ui/literal_string_with_formatting_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ fn main() {
let x: Option<usize> = 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
}
Loading