Skip to content
Merged
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
6 changes: 3 additions & 3 deletions clippy_lints/src/panic_unimplemented.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint;
use clippy_utils::is_in_test;
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
use clippy_utils::{is_in_test, is_inside_always_const_context};
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{Expr, ExprKind, QPath};
use rustc_lint::{LateContext, LateLintPass};
Expand Down Expand Up @@ -99,7 +99,7 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if let Some(macro_call) = root_macro_call_first_node(cx, expr) {
if is_panic(cx, macro_call.def_id) {
if cx.tcx.hir_is_inside_const_context(expr.hir_id)
if is_inside_always_const_context(cx.tcx, expr.hir_id)
|| self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)
{
return;
Expand Down Expand Up @@ -140,7 +140,7 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
&& let Res::Def(DefKind::Fn, def_id) = expr_path.res
&& cx.tcx.is_diagnostic_item(sym::panic_any, def_id)
{
if cx.tcx.hir_is_inside_const_context(expr.hir_id)
if is_inside_always_const_context(cx.tcx, expr.hir_id)
|| self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)
{
return;
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/panicking_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ fn panic() {
let b = a + 2;
}

const fn panic_const() {
let a = 2;
panic!();
//~^ panic

panic!("message");
//~^ panic

panic!("{} {}", "panic with", "multiple arguments");
//~^ panic

let b = a + 2;
}

fn todo() {
let a = 2;
todo!();
Expand Down Expand Up @@ -114,6 +128,7 @@ fn debug_assert_msg() {

fn main() {
panic();
panic_const();
todo();
unimplemented();
unreachable();
Expand Down
46 changes: 32 additions & 14 deletions tests/ui/panicking_macros.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,47 @@ error: `panic` should not be present in production code
LL | panic!("{} {}", "panic with", "multiple arguments");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: `todo` should not be present in production code
error: `panic` should not be present in production code
--> tests/ui/panicking_macros.rs:36:5
|
LL | panic!();
| ^^^^^^^^

error: `panic` should not be present in production code
--> tests/ui/panicking_macros.rs:39:5
|
LL | panic!("message");
| ^^^^^^^^^^^^^^^^^

error: `panic` should not be present in production code
--> tests/ui/panicking_macros.rs:42:5
|
LL | panic!("{} {}", "panic with", "multiple arguments");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: `todo` should not be present in production code
--> tests/ui/panicking_macros.rs:50:5
|
LL | todo!();
| ^^^^^^^
|
= note: `-D clippy::todo` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::todo)]`

error: `todo` should not be present in production code
--> tests/ui/panicking_macros.rs:39:5
--> tests/ui/panicking_macros.rs:53:5
|
LL | todo!("message");
| ^^^^^^^^^^^^^^^^

error: `todo` should not be present in production code
--> tests/ui/panicking_macros.rs:42:5
--> tests/ui/panicking_macros.rs:56:5
|
LL | todo!("{} {}", "panic with", "multiple arguments");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: `unimplemented` should not be present in production code
--> tests/ui/panicking_macros.rs:50:5
--> tests/ui/panicking_macros.rs:64:5
|
LL | unimplemented!();
| ^^^^^^^^^^^^^^^^
Expand All @@ -50,19 +68,19 @@ LL | unimplemented!();
= help: to override `-D warnings` add `#[allow(clippy::unimplemented)]`

error: `unimplemented` should not be present in production code
--> tests/ui/panicking_macros.rs:53:5
--> tests/ui/panicking_macros.rs:67:5
|
LL | unimplemented!("message");
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: `unimplemented` should not be present in production code
--> tests/ui/panicking_macros.rs:56:5
--> tests/ui/panicking_macros.rs:70:5
|
LL | unimplemented!("{} {}", "panic with", "multiple arguments");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: usage of the `unreachable!` macro
--> tests/ui/panicking_macros.rs:64:5
--> tests/ui/panicking_macros.rs:78:5
|
LL | unreachable!();
| ^^^^^^^^^^^^^^
Expand All @@ -71,40 +89,40 @@ LL | unreachable!();
= help: to override `-D warnings` add `#[allow(clippy::unreachable)]`

error: usage of the `unreachable!` macro
--> tests/ui/panicking_macros.rs:67:5
--> tests/ui/panicking_macros.rs:81:5
|
LL | unreachable!("message");
| ^^^^^^^^^^^^^^^^^^^^^^^

error: usage of the `unreachable!` macro
--> tests/ui/panicking_macros.rs:70:5
--> tests/ui/panicking_macros.rs:84:5
|
LL | unreachable!("{} {}", "panic with", "multiple arguments");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: `panic` should not be present in production code
--> tests/ui/panicking_macros.rs:78:5
--> tests/ui/panicking_macros.rs:92:5
|
LL | panic!();
| ^^^^^^^^

error: `todo` should not be present in production code
--> tests/ui/panicking_macros.rs:81:5
--> tests/ui/panicking_macros.rs:95:5
|
LL | todo!();
| ^^^^^^^

error: `unimplemented` should not be present in production code
--> tests/ui/panicking_macros.rs:84:5
--> tests/ui/panicking_macros.rs:98:5
|
LL | unimplemented!();
| ^^^^^^^^^^^^^^^^

error: usage of the `unreachable!` macro
--> tests/ui/panicking_macros.rs:87:5
--> tests/ui/panicking_macros.rs:101:5
|
LL | unreachable!();
| ^^^^^^^^^^^^^^

error: aborting due to 16 previous errors
error: aborting due to 19 previous errors