Skip to content

Commit 57678c8

Browse files
committed
Auto merge of #5811 - JarredAllen:panic_multiple_args, r=phansch
Panic multiple args changelog: Fixes bug with `panic` lint reported in #5767. I also did the same changes to the lints for `todo`, `unimplemented` and `unreachable`, so those lints should now also detect calls to those macros with a message.
2 parents 82c8f25 + 07867fd commit 57678c8

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

clippy_lints/src/panic_unimplemented.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,20 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
9696
if_chain! {
9797
if let ExprKind::Block(ref block, _) = expr.kind;
9898
if let Some(ref ex) = block.expr;
99-
if let Some(params) = match_function_call(cx, ex, &paths::BEGIN_PANIC);
100-
if params.len() == 1;
99+
if let Some(params) = match_function_call(cx, ex, &paths::BEGIN_PANIC)
100+
.or_else(|| match_function_call(cx, ex, &paths::BEGIN_PANIC_FMT));
101101
then {
102+
let span = get_outer_span(expr);
102103
if is_expn_of(expr.span, "unimplemented").is_some() {
103-
let span = get_outer_span(expr);
104104
span_lint(cx, UNIMPLEMENTED, span,
105105
"`unimplemented` should not be present in production code");
106106
} else if is_expn_of(expr.span, "todo").is_some() {
107-
let span = get_outer_span(expr);
108107
span_lint(cx, TODO, span,
109108
"`todo` should not be present in production code");
110109
} else if is_expn_of(expr.span, "unreachable").is_some() {
111-
let span = get_outer_span(expr);
112110
span_lint(cx, UNREACHABLE, span,
113111
"`unreachable` should not be present in production code");
114112
} else if is_expn_of(expr.span, "panic").is_some() {
115-
let span = get_outer_span(expr);
116113
span_lint(cx, PANIC, span,
117114
"`panic` should not be present in production code");
118115
match_panic(params, expr, cx);

tests/ui/panicking_macros.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,32 @@
44
fn panic() {
55
let a = 2;
66
panic!();
7+
panic!("message");
8+
panic!("{} {}", "panic with", "multiple arguments");
79
let b = a + 2;
810
}
911

1012
fn todo() {
1113
let a = 2;
1214
todo!();
15+
todo!("message");
16+
todo!("{} {}", "panic with", "multiple arguments");
1317
let b = a + 2;
1418
}
1519

1620
fn unimplemented() {
1721
let a = 2;
1822
unimplemented!();
23+
unimplemented!("message");
24+
unimplemented!("{} {}", "panic with", "multiple arguments");
1925
let b = a + 2;
2026
}
2127

2228
fn unreachable() {
2329
let a = 2;
2430
unreachable!();
31+
unreachable!("message");
32+
unreachable!("{} {}", "panic with", "multiple arguments");
2533
let b = a + 2;
2634
}
2735

tests/ui/panicking_macros.stderr

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,83 @@ LL | panic!();
66
|
77
= note: `-D clippy::panic` implied by `-D warnings`
88

9+
error: `panic` should not be present in production code
10+
--> $DIR/panicking_macros.rs:7:5
11+
|
12+
LL | panic!("message");
13+
| ^^^^^^^^^^^^^^^^^^
14+
|
15+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
16+
17+
error: `panic` should not be present in production code
18+
--> $DIR/panicking_macros.rs:8:5
19+
|
20+
LL | panic!("{} {}", "panic with", "multiple arguments");
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
|
23+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
24+
925
error: `todo` should not be present in production code
10-
--> $DIR/panicking_macros.rs:12:5
26+
--> $DIR/panicking_macros.rs:14:5
1127
|
1228
LL | todo!();
1329
| ^^^^^^^^
1430
|
1531
= note: `-D clippy::todo` implied by `-D warnings`
1632

33+
error: `todo` should not be present in production code
34+
--> $DIR/panicking_macros.rs:15:5
35+
|
36+
LL | todo!("message");
37+
| ^^^^^^^^^^^^^^^^^
38+
39+
error: `todo` should not be present in production code
40+
--> $DIR/panicking_macros.rs:16:5
41+
|
42+
LL | todo!("{} {}", "panic with", "multiple arguments");
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
1745
error: `unimplemented` should not be present in production code
18-
--> $DIR/panicking_macros.rs:18:5
46+
--> $DIR/panicking_macros.rs:22:5
1947
|
2048
LL | unimplemented!();
2149
| ^^^^^^^^^^^^^^^^^
2250
|
2351
= note: `-D clippy::unimplemented` implied by `-D warnings`
2452

25-
error: `unreachable` should not be present in production code
53+
error: `unimplemented` should not be present in production code
54+
--> $DIR/panicking_macros.rs:23:5
55+
|
56+
LL | unimplemented!("message");
57+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
58+
59+
error: `unimplemented` should not be present in production code
2660
--> $DIR/panicking_macros.rs:24:5
2761
|
62+
LL | unimplemented!("{} {}", "panic with", "multiple arguments");
63+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
64+
65+
error: `unreachable` should not be present in production code
66+
--> $DIR/panicking_macros.rs:30:5
67+
|
2868
LL | unreachable!();
2969
| ^^^^^^^^^^^^^^^
3070
|
3171
= note: `-D clippy::unreachable` implied by `-D warnings`
3272

33-
error: aborting due to 4 previous errors
73+
error: `unreachable` should not be present in production code
74+
--> $DIR/panicking_macros.rs:31:5
75+
|
76+
LL | unreachable!("message");
77+
| ^^^^^^^^^^^^^^^^^^^^^^^^
78+
|
79+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
80+
81+
error: `unreachable` should not be present in production code
82+
--> $DIR/panicking_macros.rs:32:5
83+
|
84+
LL | unreachable!("{} {}", "panic with", "multiple arguments");
85+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86+
87+
error: aborting due to 12 previous errors
3488

0 commit comments

Comments
 (0)