diff options
Diffstat (limited to 'compiler/rustc_lint/src/non_fmt_panic.rs')
| -rw-r--r-- | compiler/rustc_lint/src/non_fmt_panic.rs | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/compiler/rustc_lint/src/non_fmt_panic.rs b/compiler/rustc_lint/src/non_fmt_panic.rs index a919b3c82aa..f2fee67115d 100644 --- a/compiler/rustc_lint/src/non_fmt_panic.rs +++ b/compiler/rustc_lint/src/non_fmt_panic.rs @@ -49,9 +49,11 @@ impl<'tcx> LateLintPass<'tcx> for NonPanicFmt { fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) { if let hir::ExprKind::Call(f, [arg]) = &expr.kind { if let &ty::FnDef(def_id, _) = cx.typeck_results().expr_ty(f).kind() { + let f_diagnostic_name = cx.tcx.get_diagnostic_name(def_id); + if Some(def_id) == cx.tcx.lang_items().begin_panic_fn() || Some(def_id) == cx.tcx.lang_items().panic_fn() - || Some(def_id) == cx.tcx.lang_items().panic_str() + || f_diagnostic_name == Some(sym::panic_str) { if let Some(id) = f.span.ctxt().outer_expn_data().macro_def_id { if matches!( @@ -61,6 +63,22 @@ impl<'tcx> LateLintPass<'tcx> for NonPanicFmt { check_panic(cx, f, arg); } } + } else if f_diagnostic_name == Some(sym::unreachable_display) { + if let Some(id) = f.span.ctxt().outer_expn_data().macro_def_id { + if cx.tcx.is_diagnostic_item(sym::unreachable_2015_macro, id) { + check_panic( + cx, + f, + // This is safe because we checked above that the callee is indeed + // unreachable_display + match &arg.kind { + // Get the borrowed arg not the borrow + hir::ExprKind::AddrOf(ast::BorrowKind::Ref, _, arg) => arg, + _ => bug!("call to unreachable_display without borrow"), + }, + ); + } + } } } } @@ -85,8 +103,8 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc return; } - // Find the span of the argument to `panic!()`, before expansion in the - // case of `panic!(some_macro!())`. + // Find the span of the argument to `panic!()` or `unreachable!`, before expansion in the + // case of `panic!(some_macro!())` or `unreachable!(some_macro!())`. // We don't use source_callsite(), because this `panic!(..)` might itself // be expanded from another macro, in which case we want to stop at that // expansion. @@ -319,6 +337,7 @@ fn panic_call<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>) -> (Span, | sym::std_panic_macro | sym::assert_macro | sym::debug_assert_macro + | sym::unreachable_macro ) { break; } |
