mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-17 05:25:37 +03:00
Enable clippy::panic in const contexts (#15565)
Fixes rust-lang/rust-clippy#15564. changelog: [`cargo::panic`]: Enabled to run in `const` contexts
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use clippy_config::Conf;
|
||||
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::{is_in_test, is_inside_always_const_context};
|
||||
use rustc_hir::def::{DefKind, Res};
|
||||
use rustc_hir::{Expr, ExprKind, QPath};
|
||||
use rustc_lint::{LateContext, LateLintPass};
|
||||
@@ -99,7 +99,7 @@ impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
if let Some(macro_call) = root_macro_call_first_node(cx, expr) {
|
||||
if is_panic(cx, macro_call.def_id) {
|
||||
if cx.tcx.hir_is_inside_const_context(expr.hir_id)
|
||||
if is_inside_always_const_context(cx.tcx, expr.hir_id)
|
||||
|| self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)
|
||||
{
|
||||
return;
|
||||
@@ -140,7 +140,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||
&& let Res::Def(DefKind::Fn, def_id) = expr_path.res
|
||||
&& cx.tcx.is_diagnostic_item(sym::panic_any, def_id)
|
||||
{
|
||||
if cx.tcx.hir_is_inside_const_context(expr.hir_id)
|
||||
if is_inside_always_const_context(cx.tcx, expr.hir_id)
|
||||
|| self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)
|
||||
{
|
||||
return;
|
||||
|
||||
@@ -31,6 +31,20 @@ fn panic() {
|
||||
let b = a + 2;
|
||||
}
|
||||
|
||||
const fn panic_const() {
|
||||
let a = 2;
|
||||
panic!();
|
||||
//~^ panic
|
||||
|
||||
panic!("message");
|
||||
//~^ panic
|
||||
|
||||
panic!("{} {}", "panic with", "multiple arguments");
|
||||
//~^ panic
|
||||
|
||||
let b = a + 2;
|
||||
}
|
||||
|
||||
fn todo() {
|
||||
let a = 2;
|
||||
todo!();
|
||||
@@ -114,6 +128,7 @@ fn debug_assert_msg() {
|
||||
|
||||
fn main() {
|
||||
panic();
|
||||
panic_const();
|
||||
todo();
|
||||
unimplemented();
|
||||
unreachable();
|
||||
|
||||
@@ -19,9 +19,27 @@ error: `panic` should not be present in production code
|
||||
LL | panic!("{} {}", "panic with", "multiple arguments");
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: `todo` should not be present in production code
|
||||
error: `panic` should not be present in production code
|
||||
--> tests/ui/panicking_macros.rs:36:5
|
||||
|
|
||||
LL | panic!();
|
||||
| ^^^^^^^^
|
||||
|
||||
error: `panic` should not be present in production code
|
||||
--> tests/ui/panicking_macros.rs:39:5
|
||||
|
|
||||
LL | panic!("message");
|
||||
| ^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: `panic` should not be present in production code
|
||||
--> tests/ui/panicking_macros.rs:42:5
|
||||
|
|
||||
LL | panic!("{} {}", "panic with", "multiple arguments");
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: `todo` should not be present in production code
|
||||
--> tests/ui/panicking_macros.rs:50:5
|
||||
|
|
||||
LL | todo!();
|
||||
| ^^^^^^^
|
||||
|
|
||||
@@ -29,19 +47,19 @@ LL | todo!();
|
||||
= help: to override `-D warnings` add `#[allow(clippy::todo)]`
|
||||
|
||||
error: `todo` should not be present in production code
|
||||
--> tests/ui/panicking_macros.rs:39:5
|
||||
--> tests/ui/panicking_macros.rs:53:5
|
||||
|
|
||||
LL | todo!("message");
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
|
||||
error: `todo` should not be present in production code
|
||||
--> tests/ui/panicking_macros.rs:42:5
|
||||
--> tests/ui/panicking_macros.rs:56:5
|
||||
|
|
||||
LL | todo!("{} {}", "panic with", "multiple arguments");
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: `unimplemented` should not be present in production code
|
||||
--> tests/ui/panicking_macros.rs:50:5
|
||||
--> tests/ui/panicking_macros.rs:64:5
|
||||
|
|
||||
LL | unimplemented!();
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
@@ -50,19 +68,19 @@ LL | unimplemented!();
|
||||
= help: to override `-D warnings` add `#[allow(clippy::unimplemented)]`
|
||||
|
||||
error: `unimplemented` should not be present in production code
|
||||
--> tests/ui/panicking_macros.rs:53:5
|
||||
--> tests/ui/panicking_macros.rs:67:5
|
||||
|
|
||||
LL | unimplemented!("message");
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: `unimplemented` should not be present in production code
|
||||
--> tests/ui/panicking_macros.rs:56:5
|
||||
--> tests/ui/panicking_macros.rs:70:5
|
||||
|
|
||||
LL | unimplemented!("{} {}", "panic with", "multiple arguments");
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: usage of the `unreachable!` macro
|
||||
--> tests/ui/panicking_macros.rs:64:5
|
||||
--> tests/ui/panicking_macros.rs:78:5
|
||||
|
|
||||
LL | unreachable!();
|
||||
| ^^^^^^^^^^^^^^
|
||||
@@ -71,40 +89,40 @@ LL | unreachable!();
|
||||
= help: to override `-D warnings` add `#[allow(clippy::unreachable)]`
|
||||
|
||||
error: usage of the `unreachable!` macro
|
||||
--> tests/ui/panicking_macros.rs:67:5
|
||||
--> tests/ui/panicking_macros.rs:81:5
|
||||
|
|
||||
LL | unreachable!("message");
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: usage of the `unreachable!` macro
|
||||
--> tests/ui/panicking_macros.rs:70:5
|
||||
--> tests/ui/panicking_macros.rs:84:5
|
||||
|
|
||||
LL | unreachable!("{} {}", "panic with", "multiple arguments");
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: `panic` should not be present in production code
|
||||
--> tests/ui/panicking_macros.rs:78:5
|
||||
--> tests/ui/panicking_macros.rs:92:5
|
||||
|
|
||||
LL | panic!();
|
||||
| ^^^^^^^^
|
||||
|
||||
error: `todo` should not be present in production code
|
||||
--> tests/ui/panicking_macros.rs:81:5
|
||||
--> tests/ui/panicking_macros.rs:95:5
|
||||
|
|
||||
LL | todo!();
|
||||
| ^^^^^^^
|
||||
|
||||
error: `unimplemented` should not be present in production code
|
||||
--> tests/ui/panicking_macros.rs:84:5
|
||||
--> tests/ui/panicking_macros.rs:98:5
|
||||
|
|
||||
LL | unimplemented!();
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
|
||||
error: usage of the `unreachable!` macro
|
||||
--> tests/ui/panicking_macros.rs:87:5
|
||||
--> tests/ui/panicking_macros.rs:101:5
|
||||
|
|
||||
LL | unreachable!();
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
||||
error: aborting due to 16 previous errors
|
||||
error: aborting due to 19 previous errors
|
||||
|
||||
|
||||
Reference in New Issue
Block a user