about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMara Bos <m-ou.se@m-ou.se>2020-10-18 22:29:40 +0200
committerMara Bos <m-ou.se@m-ou.se>2020-10-18 22:29:40 +0200
commitf228efc3f56ca13a4a969d0ee72c2e0844ac6a72 (patch)
tree82fd5a320a0030973258dfaaf0f30bdc9b3b349c
parentda66a501f6423d498627453dd7d0f8bc874ee42d (diff)
downloadrust-f228efc3f56ca13a4a969d0ee72c2e0844ac6a72.tar.gz
rust-f228efc3f56ca13a4a969d0ee72c2e0844ac6a72.zip
Make panic_fmt lint work properly for assert!(expr, msg) too.
-rw-r--r--compiler/rustc_lint/src/panic_fmt.rs12
-rw-r--r--compiler/rustc_span/src/symbol.rs1
-rw-r--r--library/core/src/macros/mod.rs1
3 files changed, 14 insertions, 0 deletions
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 */ }};