mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 13:13:34 +00:00
Auto merge of #13300 - kyoto7250:issue_13292, r=llogiq
check std::panic::panic_any in panic lint close #13292 This PR detects `std::panic::panic_any` in panic lint. changelog: check std::panic::panic_any in panic lint
This commit is contained in:
commit
b3fc578ca6
4 changed files with 62 additions and 32 deletions
|
@ -1,8 +1,9 @@
|
||||||
use clippy_config::Conf;
|
use clippy_config::Conf;
|
||||||
use clippy_utils::diagnostics::span_lint;
|
use clippy_utils::diagnostics::span_lint;
|
||||||
use clippy_utils::is_in_test;
|
|
||||||
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
|
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
|
||||||
use rustc_hir::Expr;
|
use clippy_utils::{is_in_test, match_def_path, paths};
|
||||||
|
use rustc_hir::def::{DefKind, Res};
|
||||||
|
use rustc_hir::{Expr, ExprKind, QPath};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_session::impl_lint_pass;
|
use rustc_session::impl_lint_pass;
|
||||||
|
|
||||||
|
@ -95,9 +96,7 @@ impl_lint_pass!(PanicUnimplemented => [UNIMPLEMENTED, UNREACHABLE, TODO, PANIC])
|
||||||
|
|
||||||
impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
|
impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
|
||||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||||
let Some(macro_call) = root_macro_call_first_node(cx, expr) else {
|
if let Some(macro_call) = root_macro_call_first_node(cx, expr) {
|
||||||
return;
|
|
||||||
};
|
|
||||||
if is_panic(cx, macro_call.def_id) {
|
if is_panic(cx, macro_call.def_id) {
|
||||||
if cx.tcx.hir().is_inside_const_context(expr.hir_id)
|
if cx.tcx.hir().is_inside_const_context(expr.hir_id)
|
||||||
|| self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)
|
|| self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)
|
||||||
|
@ -135,5 +134,24 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
|
||||||
},
|
},
|
||||||
_ => {},
|
_ => {},
|
||||||
}
|
}
|
||||||
|
} else if let ExprKind::Call(func, [_]) = expr.kind
|
||||||
|
&& let ExprKind::Path(QPath::Resolved(None, expr_path)) = func.kind
|
||||||
|
&& let Res::Def(DefKind::Fn, def_id) = expr_path.res
|
||||||
|
&& match_def_path(cx, def_id, &paths::PANIC_ANY)
|
||||||
|
{
|
||||||
|
if cx.tcx.hir().is_inside_const_context(expr.hir_id)
|
||||||
|
|| self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
span_lint(
|
||||||
|
cx,
|
||||||
|
PANIC,
|
||||||
|
expr.span,
|
||||||
|
"`panic_any` should not be present in production code",
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,6 +56,7 @@ pub const OS_STR_TO_OS_STRING: [&str; 5] = ["std", "ffi", "os_str", "OsStr", "to
|
||||||
pub const PARKING_LOT_MUTEX_GUARD: [&str; 3] = ["lock_api", "mutex", "MutexGuard"];
|
pub const PARKING_LOT_MUTEX_GUARD: [&str; 3] = ["lock_api", "mutex", "MutexGuard"];
|
||||||
pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockReadGuard"];
|
pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockReadGuard"];
|
||||||
pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockWriteGuard"];
|
pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockWriteGuard"];
|
||||||
|
pub const PANIC_ANY: [&str; 3] = ["std", "panic", "panic_any"];
|
||||||
pub const PATH_BUF_AS_PATH: [&str; 4] = ["std", "path", "PathBuf", "as_path"];
|
pub const PATH_BUF_AS_PATH: [&str; 4] = ["std", "path", "PathBuf", "as_path"];
|
||||||
pub const PATH_MAIN_SEPARATOR: [&str; 3] = ["std", "path", "MAIN_SEPARATOR"];
|
pub const PATH_MAIN_SEPARATOR: [&str; 3] = ["std", "path", "MAIN_SEPARATOR"];
|
||||||
pub const PATH_TO_PATH_BUF: [&str; 4] = ["std", "path", "Path", "to_path_buf"];
|
pub const PATH_TO_PATH_BUF: [&str; 4] = ["std", "path", "Path", "to_path_buf"];
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
//@compile-flags: --test
|
//@compile-flags: --test
|
||||||
#![warn(clippy::panic)]
|
#![warn(clippy::panic)]
|
||||||
|
use std::panic::panic_any;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
enum Enam {
|
enum Enam {
|
||||||
|
@ -12,6 +13,10 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn issue_13292() {
|
||||||
|
panic_any("should lint")
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn lonely_test() {
|
fn lonely_test() {
|
||||||
enum Enam {
|
enum Enam {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
error: `panic` should not be present in production code
|
error: `panic` should not be present in production code
|
||||||
--> tests/ui-toml/panic/panic.rs:11:14
|
--> tests/ui-toml/panic/panic.rs:12:14
|
||||||
|
|
|
|
||||||
LL | _ => panic!(""),
|
LL | _ => panic!(""),
|
||||||
| ^^^^^^^^^^
|
| ^^^^^^^^^^
|
||||||
|
@ -7,5 +7,11 @@ LL | _ => panic!(""),
|
||||||
= note: `-D clippy::panic` implied by `-D warnings`
|
= note: `-D clippy::panic` implied by `-D warnings`
|
||||||
= help: to override `-D warnings` add `#[allow(clippy::panic)]`
|
= help: to override `-D warnings` add `#[allow(clippy::panic)]`
|
||||||
|
|
||||||
error: aborting due to 1 previous error
|
error: `panic_any` should not be present in production code
|
||||||
|
--> tests/ui-toml/panic/panic.rs:17:5
|
||||||
|
|
|
||||||
|
LL | panic_any("should lint")
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue