Skip to content

Commit 115030e

Browse files
committed
Don't panic in always const
1 parent 096f936 commit 115030e

File tree

3 files changed

+51
-28
lines changed

3 files changed

+51
-28
lines changed

clippy_lints/src/panic_unimplemented.rs

Lines changed: 7 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,9 @@ 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 self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id) {
102+
if is_inside_always_const_context(cx.tcx, expr.hir_id)
103+
|| self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)
104+
{
103105
return;
104106
}
105107

@@ -138,7 +140,9 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
138140
&& let Res::Def(DefKind::Fn, def_id) = expr_path.res
139141
&& cx.tcx.is_diagnostic_item(sym::panic_any, def_id)
140142
{
141-
if self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id) {
143+
if is_inside_always_const_context(cx.tcx, expr.hir_id)
144+
|| self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)
145+
{
142146
return;
143147
}
144148

tests/ui/panicking_macros.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ extern crate core;
66
const _: () = {
77
if 1 == 0 {
88
panic!("A balanced diet means a cupcake in each hand");
9-
//~^ panic
109
}
1110
};
1211

1312
fn inline_const() {
1413
let _ = const {
1514
if 1 == 0 {
1615
panic!("When nothing goes right, go left")
17-
//~^ panic
1816
}
1917
};
2018
}
@@ -33,6 +31,20 @@ fn panic() {
3331
let b = a + 2;
3432
}
3533

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+
3648
fn todo() {
3749
let a = 2;
3850
todo!();
@@ -116,6 +128,7 @@ fn debug_assert_msg() {
116128

117129
fn main() {
118130
panic();
131+
panic_const();
119132
todo();
120133
unimplemented();
121134
unreachable();

tests/ui/panicking_macros.stderr

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
11
error: `panic` should not be present in production code
2-
--> tests/ui/panicking_macros.rs:8:9
2+
--> tests/ui/panicking_macros.rs:22:5
33
|
4-
LL | panic!("A balanced diet means a cupcake in each hand");
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
LL | panic!();
5+
| ^^^^^^^^
66
|
77
= note: `-D clippy::panic` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::panic)]`
99

1010
error: `panic` should not be present in production code
11-
--> tests/ui/panicking_macros.rs:16:13
11+
--> tests/ui/panicking_macros.rs:25:5
1212
|
13-
LL | panic!("When nothing goes right, go left")
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
LL | panic!("message");
14+
| ^^^^^^^^^^^^^^^^^
15+
16+
error: `panic` should not be present in production code
17+
--> tests/ui/panicking_macros.rs:28:5
18+
|
19+
LL | panic!("{} {}", "panic with", "multiple arguments");
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1521

1622
error: `panic` should not be present in production code
17-
--> tests/ui/panicking_macros.rs:24:5
23+
--> tests/ui/panicking_macros.rs:36:5
1824
|
1925
LL | panic!();
2026
| ^^^^^^^^
2127

2228
error: `panic` should not be present in production code
23-
--> tests/ui/panicking_macros.rs:27:5
29+
--> tests/ui/panicking_macros.rs:39:5
2430
|
2531
LL | panic!("message");
2632
| ^^^^^^^^^^^^^^^^^
2733

2834
error: `panic` should not be present in production code
29-
--> tests/ui/panicking_macros.rs:30:5
35+
--> tests/ui/panicking_macros.rs:42:5
3036
|
3137
LL | panic!("{} {}", "panic with", "multiple arguments");
3238
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3339

3440
error: `todo` should not be present in production code
35-
--> tests/ui/panicking_macros.rs:38:5
41+
--> tests/ui/panicking_macros.rs:50:5
3642
|
3743
LL | todo!();
3844
| ^^^^^^^
@@ -41,19 +47,19 @@ LL | todo!();
4147
= help: to override `-D warnings` add `#[allow(clippy::todo)]`
4248

4349
error: `todo` should not be present in production code
44-
--> tests/ui/panicking_macros.rs:41:5
50+
--> tests/ui/panicking_macros.rs:53:5
4551
|
4652
LL | todo!("message");
4753
| ^^^^^^^^^^^^^^^^
4854

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

5561
error: `unimplemented` should not be present in production code
56-
--> tests/ui/panicking_macros.rs:52:5
62+
--> tests/ui/panicking_macros.rs:64:5
5763
|
5864
LL | unimplemented!();
5965
| ^^^^^^^^^^^^^^^^
@@ -62,19 +68,19 @@ LL | unimplemented!();
6268
= help: to override `-D warnings` add `#[allow(clippy::unimplemented)]`
6369

6470
error: `unimplemented` should not be present in production code
65-
--> tests/ui/panicking_macros.rs:55:5
71+
--> tests/ui/panicking_macros.rs:67:5
6672
|
6773
LL | unimplemented!("message");
6874
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6975

7076
error: `unimplemented` should not be present in production code
71-
--> tests/ui/panicking_macros.rs:58:5
77+
--> tests/ui/panicking_macros.rs:70:5
7278
|
7379
LL | unimplemented!("{} {}", "panic with", "multiple arguments");
7480
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7581

7682
error: usage of the `unreachable!` macro
77-
--> tests/ui/panicking_macros.rs:66:5
83+
--> tests/ui/panicking_macros.rs:78:5
7884
|
7985
LL | unreachable!();
8086
| ^^^^^^^^^^^^^^
@@ -83,40 +89,40 @@ LL | unreachable!();
8389
= help: to override `-D warnings` add `#[allow(clippy::unreachable)]`
8490

8591
error: usage of the `unreachable!` macro
86-
--> tests/ui/panicking_macros.rs:69:5
92+
--> tests/ui/panicking_macros.rs:81:5
8793
|
8894
LL | unreachable!("message");
8995
| ^^^^^^^^^^^^^^^^^^^^^^^
9096

9197
error: usage of the `unreachable!` macro
92-
--> tests/ui/panicking_macros.rs:72:5
98+
--> tests/ui/panicking_macros.rs:84:5
9399
|
94100
LL | unreachable!("{} {}", "panic with", "multiple arguments");
95101
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
96102

97103
error: `panic` should not be present in production code
98-
--> tests/ui/panicking_macros.rs:80:5
104+
--> tests/ui/panicking_macros.rs:92:5
99105
|
100106
LL | panic!();
101107
| ^^^^^^^^
102108

103109
error: `todo` should not be present in production code
104-
--> tests/ui/panicking_macros.rs:83:5
110+
--> tests/ui/panicking_macros.rs:95:5
105111
|
106112
LL | todo!();
107113
| ^^^^^^^
108114

109115
error: `unimplemented` should not be present in production code
110-
--> tests/ui/panicking_macros.rs:86:5
116+
--> tests/ui/panicking_macros.rs:98:5
111117
|
112118
LL | unimplemented!();
113119
| ^^^^^^^^^^^^^^^^
114120

115121
error: usage of the `unreachable!` macro
116-
--> tests/ui/panicking_macros.rs:89:5
122+
--> tests/ui/panicking_macros.rs:101:5
117123
|
118124
LL | unreachable!();
119125
| ^^^^^^^^^^^^^^
120126

121-
error: aborting due to 18 previous errors
127+
error: aborting due to 19 previous errors
122128

0 commit comments

Comments
 (0)