Skip to content

Commit c394c00

Browse files
Enable clippy::panic in const contexts (#15565)
Fixes rust-lang/rust-clippy#15564. changelog: [`cargo::panic`]: Enabled to run in `const` contexts
2 parents c4acbce + e54afd3 commit c394c00

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
lines changed

clippy_lints/src/panic_unimplemented.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint;
3-
use clippy_utils::is_in_test;
43
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
4+
use clippy_utils::{is_in_test, is_inside_always_const_context};
55
use rustc_hir::def::{DefKind, Res};
66
use rustc_hir::{Expr, ExprKind, QPath};
77
use rustc_lint::{LateContext, LateLintPass};
@@ -99,7 +99,7 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
9999
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
100100
if let Some(macro_call) = root_macro_call_first_node(cx, expr) {
101101
if is_panic(cx, macro_call.def_id) {
102-
if cx.tcx.hir_is_inside_const_context(expr.hir_id)
102+
if is_inside_always_const_context(cx.tcx, expr.hir_id)
103103
|| self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)
104104
{
105105
return;
@@ -140,7 +140,7 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
140140
&& let Res::Def(DefKind::Fn, def_id) = expr_path.res
141141
&& cx.tcx.is_diagnostic_item(sym::panic_any, def_id)
142142
{
143-
if cx.tcx.hir_is_inside_const_context(expr.hir_id)
143+
if is_inside_always_const_context(cx.tcx, expr.hir_id)
144144
|| self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)
145145
{
146146
return;

tests/ui/panicking_macros.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ fn panic() {
3131
let b = a + 2;
3232
}
3333

34+
const fn panic_const() {
35+
let a = 2;
36+
panic!();
37+
//~^ panic
38+
39+
panic!("message");
40+
//~^ panic
41+
42+
panic!("{} {}", "panic with", "multiple arguments");
43+
//~^ panic
44+
45+
let b = a + 2;
46+
}
47+
3448
fn todo() {
3549
let a = 2;
3650
todo!();
@@ -114,6 +128,7 @@ fn debug_assert_msg() {
114128

115129
fn main() {
116130
panic();
131+
panic_const();
117132
todo();
118133
unimplemented();
119134
unreachable();

tests/ui/panicking_macros.stderr

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,47 @@ error: `panic` should not be present in production code
1919
LL | panic!("{} {}", "panic with", "multiple arguments");
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2121

22-
error: `todo` should not be present in production code
22+
error: `panic` should not be present in production code
2323
--> tests/ui/panicking_macros.rs:36:5
2424
|
25+
LL | panic!();
26+
| ^^^^^^^^
27+
28+
error: `panic` should not be present in production code
29+
--> tests/ui/panicking_macros.rs:39:5
30+
|
31+
LL | panic!("message");
32+
| ^^^^^^^^^^^^^^^^^
33+
34+
error: `panic` should not be present in production code
35+
--> tests/ui/panicking_macros.rs:42:5
36+
|
37+
LL | panic!("{} {}", "panic with", "multiple arguments");
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
40+
error: `todo` should not be present in production code
41+
--> tests/ui/panicking_macros.rs:50:5
42+
|
2543
LL | todo!();
2644
| ^^^^^^^
2745
|
2846
= note: `-D clippy::todo` implied by `-D warnings`
2947
= help: to override `-D warnings` add `#[allow(clippy::todo)]`
3048

3149
error: `todo` should not be present in production code
32-
--> tests/ui/panicking_macros.rs:39:5
50+
--> tests/ui/panicking_macros.rs:53:5
3351
|
3452
LL | todo!("message");
3553
| ^^^^^^^^^^^^^^^^
3654

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

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

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

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

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

7391
error: usage of the `unreachable!` macro
74-
--> tests/ui/panicking_macros.rs:67:5
92+
--> tests/ui/panicking_macros.rs:81:5
7593
|
7694
LL | unreachable!("message");
7795
| ^^^^^^^^^^^^^^^^^^^^^^^
7896

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

85103
error: `panic` should not be present in production code
86-
--> tests/ui/panicking_macros.rs:78:5
104+
--> tests/ui/panicking_macros.rs:92:5
87105
|
88106
LL | panic!();
89107
| ^^^^^^^^
90108

91109
error: `todo` should not be present in production code
92-
--> tests/ui/panicking_macros.rs:81:5
110+
--> tests/ui/panicking_macros.rs:95:5
93111
|
94112
LL | todo!();
95113
| ^^^^^^^
96114

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

103121
error: usage of the `unreachable!` macro
104-
--> tests/ui/panicking_macros.rs:87:5
122+
--> tests/ui/panicking_macros.rs:101:5
105123
|
106124
LL | unreachable!();
107125
| ^^^^^^^^^^^^^^
108126

109-
error: aborting due to 16 previous errors
127+
error: aborting due to 19 previous errors
110128

0 commit comments

Comments
 (0)