Skip to content

Commit b3fc578

Browse files
committed
Auto merge of rust-lang#13300 - kyoto7250:issue_13292, r=llogiq
check std::panic::panic_any in panic lint close rust-lang#13292 This PR detects `std::panic::panic_any` in panic lint. changelog: check std::panic::panic_any in panic lint
2 parents ebcd6bc + b179c3e commit b3fc578

File tree

4 files changed

+62
-32
lines changed

4 files changed

+62
-32
lines changed

clippy_lints/src/panic_unimplemented.rs

+48-30
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
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};
5-
use rustc_hir::Expr;
4+
use clippy_utils::{is_in_test, match_def_path, paths};
5+
use rustc_hir::def::{DefKind, Res};
6+
use rustc_hir::{Expr, ExprKind, QPath};
67
use rustc_lint::{LateContext, LateLintPass};
78
use rustc_session::impl_lint_pass;
89

@@ -95,10 +96,49 @@ impl_lint_pass!(PanicUnimplemented => [UNIMPLEMENTED, UNREACHABLE, TODO, PANIC])
9596

9697
impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
9798
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
98-
let Some(macro_call) = root_macro_call_first_node(cx, expr) else {
99-
return;
100-
};
101-
if is_panic(cx, macro_call.def_id) {
99+
if let Some(macro_call) = root_macro_call_first_node(cx, expr) {
100+
if is_panic(cx, macro_call.def_id) {
101+
if cx.tcx.hir().is_inside_const_context(expr.hir_id)
102+
|| self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)
103+
{
104+
return;
105+
}
106+
107+
span_lint(
108+
cx,
109+
PANIC,
110+
macro_call.span,
111+
"`panic` should not be present in production code",
112+
);
113+
return;
114+
}
115+
match cx.tcx.item_name(macro_call.def_id).as_str() {
116+
"todo" => {
117+
span_lint(
118+
cx,
119+
TODO,
120+
macro_call.span,
121+
"`todo` should not be present in production code",
122+
);
123+
},
124+
"unimplemented" => {
125+
span_lint(
126+
cx,
127+
UNIMPLEMENTED,
128+
macro_call.span,
129+
"`unimplemented` should not be present in production code",
130+
);
131+
},
132+
"unreachable" => {
133+
span_lint(cx, UNREACHABLE, macro_call.span, "usage of the `unreachable!` macro");
134+
},
135+
_ => {},
136+
}
137+
} else if let ExprKind::Call(func, [_]) = expr.kind
138+
&& let ExprKind::Path(QPath::Resolved(None, expr_path)) = func.kind
139+
&& let Res::Def(DefKind::Fn, def_id) = expr_path.res
140+
&& match_def_path(cx, def_id, &paths::PANIC_ANY)
141+
{
102142
if cx.tcx.hir().is_inside_const_context(expr.hir_id)
103143
|| self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)
104144
{
@@ -108,32 +148,10 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
108148
span_lint(
109149
cx,
110150
PANIC,
111-
macro_call.span,
112-
"`panic` should not be present in production code",
151+
expr.span,
152+
"`panic_any` should not be present in production code",
113153
);
114154
return;
115155
}
116-
match cx.tcx.item_name(macro_call.def_id).as_str() {
117-
"todo" => {
118-
span_lint(
119-
cx,
120-
TODO,
121-
macro_call.span,
122-
"`todo` should not be present in production code",
123-
);
124-
},
125-
"unimplemented" => {
126-
span_lint(
127-
cx,
128-
UNIMPLEMENTED,
129-
macro_call.span,
130-
"`unimplemented` should not be present in production code",
131-
);
132-
},
133-
"unreachable" => {
134-
span_lint(cx, UNREACHABLE, macro_call.span, "usage of the `unreachable!` macro");
135-
},
136-
_ => {},
137-
}
138156
}
139157
}

clippy_utils/src/paths.rs

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub const OS_STR_TO_OS_STRING: [&str; 5] = ["std", "ffi", "os_str", "OsStr", "to
5656
pub const PARKING_LOT_MUTEX_GUARD: [&str; 3] = ["lock_api", "mutex", "MutexGuard"];
5757
pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockReadGuard"];
5858
pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockWriteGuard"];
59+
pub const PANIC_ANY: [&str; 3] = ["std", "panic", "panic_any"];
5960
pub const PATH_BUF_AS_PATH: [&str; 4] = ["std", "path", "PathBuf", "as_path"];
6061
pub const PATH_MAIN_SEPARATOR: [&str; 3] = ["std", "path", "MAIN_SEPARATOR"];
6162
pub const PATH_TO_PATH_BUF: [&str; 4] = ["std", "path", "Path", "to_path_buf"];

tests/ui-toml/panic/panic.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//@compile-flags: --test
22
#![warn(clippy::panic)]
3+
use std::panic::panic_any;
34

45
fn main() {
56
enum Enam {
@@ -12,6 +13,10 @@ fn main() {
1213
}
1314
}
1415

16+
fn issue_13292() {
17+
panic_any("should lint")
18+
}
19+
1520
#[test]
1621
fn lonely_test() {
1722
enum Enam {

tests/ui-toml/panic/panic.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
error: `panic` should not be present in production code
2-
--> tests/ui-toml/panic/panic.rs:11:14
2+
--> tests/ui-toml/panic/panic.rs:12:14
33
|
44
LL | _ => panic!(""),
55
| ^^^^^^^^^^
66
|
77
= note: `-D clippy::panic` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::panic)]`
99

10-
error: aborting due to 1 previous error
10+
error: `panic_any` should not be present in production code
11+
--> tests/ui-toml/panic/panic.rs:17:5
12+
|
13+
LL | panic_any("should lint")
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^
15+
16+
error: aborting due to 2 previous errors
1117

0 commit comments

Comments
 (0)