Skip to content

Commit 0031f69

Browse files
committed
Auto merge of rust-lang#8592 - c410-f3r:stuff, r=flip1995
Do not fire `panic` in a constant environment Let rustc handle panics in constant environments. Since rust-lang/rust-clippy#8348 I thought that such modification would require a lot of work but thanks to rust-lang/rust-clippy#8588 I now know that it is not the case. changelog: [`panic`]: No longer lint in constant context. `rustc` already handles this.
2 parents e1ddf29 + af8ed04 commit 0031f69

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

clippy_lints/src/panic_unimplemented.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
7878
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
7979
let Some(macro_call) = root_macro_call_first_node(cx, expr) else { return };
8080
if is_panic(cx, macro_call.def_id) {
81+
if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
82+
return;
83+
}
84+
8185
span_lint(
8286
cx,
8387
PANIC,

tests/ui/panicking_macros.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1-
#![warn(clippy::unimplemented, clippy::unreachable, clippy::todo, clippy::panic)]
21
#![allow(clippy::assertions_on_constants, clippy::eq_op)]
2+
#![feature(inline_const)]
3+
#![warn(clippy::unimplemented, clippy::unreachable, clippy::todo, clippy::panic)]
34

45
extern crate core;
56

7+
const _: () = {
8+
if 1 == 0 {
9+
panic!("A balanced diet means a cupcake in each hand");
10+
}
11+
};
12+
13+
fn inline_const() {
14+
let _ = const {
15+
if 1 == 0 {
16+
panic!("When nothing goes right, go left")
17+
}
18+
};
19+
}
20+
621
fn panic() {
722
let a = 2;
823
panic!();

tests/ui/panicking_macros.stderr

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,103 @@
11
error: `panic` should not be present in production code
2-
--> $DIR/panicking_macros.rs:8:5
2+
--> $DIR/panicking_macros.rs:23:5
33
|
44
LL | panic!();
55
| ^^^^^^^^
66
|
77
= note: `-D clippy::panic` implied by `-D warnings`
88

99
error: `panic` should not be present in production code
10-
--> $DIR/panicking_macros.rs:9:5
10+
--> $DIR/panicking_macros.rs:24:5
1111
|
1212
LL | panic!("message");
1313
| ^^^^^^^^^^^^^^^^^
1414

1515
error: `panic` should not be present in production code
16-
--> $DIR/panicking_macros.rs:10:5
16+
--> $DIR/panicking_macros.rs:25:5
1717
|
1818
LL | panic!("{} {}", "panic with", "multiple arguments");
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2020

2121
error: `todo` should not be present in production code
22-
--> $DIR/panicking_macros.rs:16:5
22+
--> $DIR/panicking_macros.rs:31:5
2323
|
2424
LL | todo!();
2525
| ^^^^^^^
2626
|
2727
= note: `-D clippy::todo` implied by `-D warnings`
2828

2929
error: `todo` should not be present in production code
30-
--> $DIR/panicking_macros.rs:17:5
30+
--> $DIR/panicking_macros.rs:32:5
3131
|
3232
LL | todo!("message");
3333
| ^^^^^^^^^^^^^^^^
3434

3535
error: `todo` should not be present in production code
36-
--> $DIR/panicking_macros.rs:18:5
36+
--> $DIR/panicking_macros.rs:33:5
3737
|
3838
LL | todo!("{} {}", "panic with", "multiple arguments");
3939
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4040

4141
error: `unimplemented` should not be present in production code
42-
--> $DIR/panicking_macros.rs:24:5
42+
--> $DIR/panicking_macros.rs:39:5
4343
|
4444
LL | unimplemented!();
4545
| ^^^^^^^^^^^^^^^^
4646
|
4747
= note: `-D clippy::unimplemented` implied by `-D warnings`
4848

4949
error: `unimplemented` should not be present in production code
50-
--> $DIR/panicking_macros.rs:25:5
50+
--> $DIR/panicking_macros.rs:40:5
5151
|
5252
LL | unimplemented!("message");
5353
| ^^^^^^^^^^^^^^^^^^^^^^^^^
5454

5555
error: `unimplemented` should not be present in production code
56-
--> $DIR/panicking_macros.rs:26:5
56+
--> $DIR/panicking_macros.rs:41:5
5757
|
5858
LL | unimplemented!("{} {}", "panic with", "multiple arguments");
5959
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6060

6161
error: usage of the `unreachable!` macro
62-
--> $DIR/panicking_macros.rs:32:5
62+
--> $DIR/panicking_macros.rs:47:5
6363
|
6464
LL | unreachable!();
6565
| ^^^^^^^^^^^^^^
6666
|
6767
= note: `-D clippy::unreachable` implied by `-D warnings`
6868

6969
error: usage of the `unreachable!` macro
70-
--> $DIR/panicking_macros.rs:33:5
70+
--> $DIR/panicking_macros.rs:48:5
7171
|
7272
LL | unreachable!("message");
7373
| ^^^^^^^^^^^^^^^^^^^^^^^
7474

7575
error: usage of the `unreachable!` macro
76-
--> $DIR/panicking_macros.rs:34:5
76+
--> $DIR/panicking_macros.rs:49:5
7777
|
7878
LL | unreachable!("{} {}", "panic with", "multiple arguments");
7979
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8080

8181
error: `panic` should not be present in production code
82-
--> $DIR/panicking_macros.rs:40:5
82+
--> $DIR/panicking_macros.rs:55:5
8383
|
8484
LL | panic!();
8585
| ^^^^^^^^
8686

8787
error: `todo` should not be present in production code
88-
--> $DIR/panicking_macros.rs:41:5
88+
--> $DIR/panicking_macros.rs:56:5
8989
|
9090
LL | todo!();
9191
| ^^^^^^^
9292

9393
error: `unimplemented` should not be present in production code
94-
--> $DIR/panicking_macros.rs:42:5
94+
--> $DIR/panicking_macros.rs:57:5
9595
|
9696
LL | unimplemented!();
9797
| ^^^^^^^^^^^^^^^^
9898

9999
error: usage of the `unreachable!` macro
100-
--> $DIR/panicking_macros.rs:43:5
100+
--> $DIR/panicking_macros.rs:58:5
101101
|
102102
LL | unreachable!();
103103
| ^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)