Skip to content

Commit f228efc

Browse files
committed
Make panic_fmt lint work properly for assert!(expr, msg) too.
1 parent da66a50 commit f228efc

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

compiler/rustc_lint/src/panic_fmt.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc
5353
if cx.tcx.is_diagnostic_item(sym::std_panic_macro, id)
5454
|| cx.tcx.is_diagnostic_item(sym::core_panic_macro, id)
5555
{
56+
let expn = {
57+
// Unwrap another level of macro expansion if this
58+
// panic!() was expanded from assert!().
59+
let parent = expn.call_site.ctxt().outer_expn_data();
60+
if parent.macro_def_id.map_or(false, |id| {
61+
cx.tcx.is_diagnostic_item(sym::assert_macro, id)
62+
}) {
63+
parent
64+
} else {
65+
expn
66+
}
67+
};
5668
cx.struct_span_lint(PANIC_FMT, expn.call_site, |lint| {
5769
let mut l = lint.build("Panic message contains a brace");
5870
l.note("This message is not used as a format string, but will be in a future Rust version");

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ symbols! {
267267
asm,
268268
assert,
269269
assert_inhabited,
270+
assert_macro,
270271
assert_receiver_is_total_eq,
271272
assert_uninit_valid,
272273
assert_zero_valid,

library/core/src/macros/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,7 @@ pub(crate) mod builtin {
12161216
#[stable(feature = "rust1", since = "1.0.0")]
12171217
#[rustc_builtin_macro]
12181218
#[macro_export]
1219+
#[rustc_diagnostic_item = "assert_macro"]
12191220
macro_rules! assert {
12201221
($cond:expr $(,)?) => {{ /* compiler built-in */ }};
12211222
($cond:expr, $($arg:tt)+) => {{ /* compiler built-in */ }};

0 commit comments

Comments
 (0)