Skip to content

Commit 096f936

Browse files
committed
Enable clippy::panic in const contexts
1 parent 798f754 commit 096f936

File tree

3 files changed

+35
-25
lines changed

3 files changed

+35
-25
lines changed

clippy_lints/src/panic_unimplemented.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +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)
103-
|| self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)
104-
{
102+
if self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id) {
105103
return;
106104
}
107105

@@ -140,9 +138,7 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
140138
&& let Res::Def(DefKind::Fn, def_id) = expr_path.res
141139
&& cx.tcx.is_diagnostic_item(sym::panic_any, def_id)
142140
{
143-
if cx.tcx.hir_is_inside_const_context(expr.hir_id)
144-
|| self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)
145-
{
141+
if self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id) {
146142
return;
147143
}
148144

tests/ui/panicking_macros.rs

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

1213
fn inline_const() {
1314
let _ = const {
1415
if 1 == 0 {
1516
panic!("When nothing goes right, go left")
17+
//~^ panic
1618
}
1719
};
1820
}

tests/ui/panicking_macros.stderr

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
11
error: `panic` should not be present in production code
2-
--> tests/ui/panicking_macros.rs:22:5
2+
--> tests/ui/panicking_macros.rs:8:9
33
|
4-
LL | panic!();
5-
| ^^^^^^^^
4+
LL | panic!("A balanced diet means a cupcake in each hand");
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:25:5
11+
--> tests/ui/panicking_macros.rs:16:13
12+
|
13+
LL | panic!("When nothing goes right, go left")
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
16+
error: `panic` should not be present in production code
17+
--> tests/ui/panicking_macros.rs:24:5
18+
|
19+
LL | panic!();
20+
| ^^^^^^^^
21+
22+
error: `panic` should not be present in production code
23+
--> tests/ui/panicking_macros.rs:27:5
1224
|
1325
LL | panic!("message");
1426
| ^^^^^^^^^^^^^^^^^
1527

1628
error: `panic` should not be present in production code
17-
--> tests/ui/panicking_macros.rs:28:5
29+
--> tests/ui/panicking_macros.rs:30:5
1830
|
1931
LL | panic!("{} {}", "panic with", "multiple arguments");
2032
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2133

2234
error: `todo` should not be present in production code
23-
--> tests/ui/panicking_macros.rs:36:5
35+
--> tests/ui/panicking_macros.rs:38:5
2436
|
2537
LL | todo!();
2638
| ^^^^^^^
@@ -29,19 +41,19 @@ LL | todo!();
2941
= help: to override `-D warnings` add `#[allow(clippy::todo)]`
3042

3143
error: `todo` should not be present in production code
32-
--> tests/ui/panicking_macros.rs:39:5
44+
--> tests/ui/panicking_macros.rs:41:5
3345
|
3446
LL | todo!("message");
3547
| ^^^^^^^^^^^^^^^^
3648

3749
error: `todo` should not be present in production code
38-
--> tests/ui/panicking_macros.rs:42:5
50+
--> tests/ui/panicking_macros.rs:44:5
3951
|
4052
LL | todo!("{} {}", "panic with", "multiple arguments");
4153
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4254

4355
error: `unimplemented` should not be present in production code
44-
--> tests/ui/panicking_macros.rs:50:5
56+
--> tests/ui/panicking_macros.rs:52:5
4557
|
4658
LL | unimplemented!();
4759
| ^^^^^^^^^^^^^^^^
@@ -50,19 +62,19 @@ LL | unimplemented!();
5062
= help: to override `-D warnings` add `#[allow(clippy::unimplemented)]`
5163

5264
error: `unimplemented` should not be present in production code
53-
--> tests/ui/panicking_macros.rs:53:5
65+
--> tests/ui/panicking_macros.rs:55:5
5466
|
5567
LL | unimplemented!("message");
5668
| ^^^^^^^^^^^^^^^^^^^^^^^^^
5769

5870
error: `unimplemented` should not be present in production code
59-
--> tests/ui/panicking_macros.rs:56:5
71+
--> tests/ui/panicking_macros.rs:58:5
6072
|
6173
LL | unimplemented!("{} {}", "panic with", "multiple arguments");
6274
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6375

6476
error: usage of the `unreachable!` macro
65-
--> tests/ui/panicking_macros.rs:64:5
77+
--> tests/ui/panicking_macros.rs:66:5
6678
|
6779
LL | unreachable!();
6880
| ^^^^^^^^^^^^^^
@@ -71,40 +83,40 @@ LL | unreachable!();
7183
= help: to override `-D warnings` add `#[allow(clippy::unreachable)]`
7284

7385
error: usage of the `unreachable!` macro
74-
--> tests/ui/panicking_macros.rs:67:5
86+
--> tests/ui/panicking_macros.rs:69:5
7587
|
7688
LL | unreachable!("message");
7789
| ^^^^^^^^^^^^^^^^^^^^^^^
7890

7991
error: usage of the `unreachable!` macro
80-
--> tests/ui/panicking_macros.rs:70:5
92+
--> tests/ui/panicking_macros.rs:72:5
8193
|
8294
LL | unreachable!("{} {}", "panic with", "multiple arguments");
8395
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8496

8597
error: `panic` should not be present in production code
86-
--> tests/ui/panicking_macros.rs:78:5
98+
--> tests/ui/panicking_macros.rs:80:5
8799
|
88100
LL | panic!();
89101
| ^^^^^^^^
90102

91103
error: `todo` should not be present in production code
92-
--> tests/ui/panicking_macros.rs:81:5
104+
--> tests/ui/panicking_macros.rs:83:5
93105
|
94106
LL | todo!();
95107
| ^^^^^^^
96108

97109
error: `unimplemented` should not be present in production code
98-
--> tests/ui/panicking_macros.rs:84:5
110+
--> tests/ui/panicking_macros.rs:86:5
99111
|
100112
LL | unimplemented!();
101113
| ^^^^^^^^^^^^^^^^
102114

103115
error: usage of the `unreachable!` macro
104-
--> tests/ui/panicking_macros.rs:87:5
116+
--> tests/ui/panicking_macros.rs:89:5
105117
|
106118
LL | unreachable!();
107119
| ^^^^^^^^^^^^^^
108120

109-
error: aborting due to 16 previous errors
121+
error: aborting due to 18 previous errors
110122

0 commit comments

Comments
 (0)