From f228efc3f56ca13a4a969d0ee72c2e0844ac6a72 Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Sun, 18 Oct 2020 22:29:40 +0200 Subject: [PATCH] Make panic_fmt lint work properly for assert!(expr, msg) too. --- compiler/rustc_lint/src/panic_fmt.rs | 12 ++++++++++++ compiler/rustc_span/src/symbol.rs | 1 + library/core/src/macros/mod.rs | 1 + 3 files changed, 14 insertions(+) diff --git a/compiler/rustc_lint/src/panic_fmt.rs b/compiler/rustc_lint/src/panic_fmt.rs index e0752515377..198797974ff 100644 --- a/compiler/rustc_lint/src/panic_fmt.rs +++ b/compiler/rustc_lint/src/panic_fmt.rs @@ -53,6 +53,18 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc if cx.tcx.is_diagnostic_item(sym::std_panic_macro, id) || cx.tcx.is_diagnostic_item(sym::core_panic_macro, id) { + let expn = { + // Unwrap another level of macro expansion if this + // panic!() was expanded from assert!(). + let parent = expn.call_site.ctxt().outer_expn_data(); + if parent.macro_def_id.map_or(false, |id| { + cx.tcx.is_diagnostic_item(sym::assert_macro, id) + }) { + parent + } else { + expn + } + }; cx.struct_span_lint(PANIC_FMT, expn.call_site, |lint| { let mut l = lint.build("Panic message contains a brace"); l.note("This message is not used as a format string, but will be in a future Rust version"); diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index c36d6380771..c6949d9387c 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -267,6 +267,7 @@ symbols! { asm, assert, assert_inhabited, + assert_macro, assert_receiver_is_total_eq, assert_uninit_valid, assert_zero_valid, diff --git a/library/core/src/macros/mod.rs b/library/core/src/macros/mod.rs index 4999933fee5..9f28186a33c 100644 --- a/library/core/src/macros/mod.rs +++ b/library/core/src/macros/mod.rs @@ -1216,6 +1216,7 @@ pub(crate) mod builtin { #[stable(feature = "rust1", since = "1.0.0")] #[rustc_builtin_macro] #[macro_export] + #[rustc_diagnostic_item = "assert_macro"] macro_rules! assert { ($cond:expr $(,)?) => {{ /* compiler built-in */ }}; ($cond:expr, $($arg:tt)+) => {{ /* compiler built-in */ }};