1 use clippy_utils
::diagnostics
::span_lint
;
2 use clippy_utils
::macros
::{is_panic, root_macro_call_first_node}
;
4 use rustc_lint
::{LateContext, LateLintPass}
;
5 use rustc_session
::declare_lint_pass
;
9 /// Checks for usage of `panic!`.
11 /// ### Why is this bad?
12 /// `panic!` will stop the execution of the executable.
16 /// panic!("even with a good reason");
18 #[clippy::version = "1.40.0"]
21 "usage of the `panic!` macro"
24 declare_clippy_lint
! {
26 /// Checks for usage of `unimplemented!`.
28 /// ### Why is this bad?
29 /// This macro should not be present in production code.
35 #[clippy::version = "pre 1.29.0"]
38 "`unimplemented!` should not be present in production code"
41 declare_clippy_lint
! {
43 /// Checks for usage of `todo!`.
45 /// ### Why is this bad?
46 /// The `todo!` macro is often used for unfinished code, and it causes
47 /// code to panic. It should not be present in production code.
53 /// Finish the implementation, or consider marking it as explicitly unimplemented.
57 #[clippy::version = "1.40.0"]
60 "`todo!` should not be present in production code"
63 declare_clippy_lint
! {
65 /// Checks for usage of `unreachable!`.
67 /// ### Why is this bad?
68 /// This macro can cause code to panic.
74 #[clippy::version = "1.40.0"]
77 "usage of the `unreachable!` macro"
80 declare_lint_pass
!(PanicUnimplemented
=> [UNIMPLEMENTED
, UNREACHABLE
, TODO
, PANIC
]);
82 impl<'tcx
> LateLintPass
<'tcx
> for PanicUnimplemented
{
83 fn check_expr(&mut self, cx
: &LateContext
<'tcx
>, expr
: &'tcx Expr
<'_
>) {
84 let Some(macro_call
) = root_macro_call_first_node(cx
, expr
) else {
87 if is_panic(cx
, macro_call
.def_id
) {
88 if cx
.tcx
.hir().is_inside_const_context(expr
.hir_id
) {
96 "`panic` should not be present in production code",
100 match cx
.tcx
.item_name(macro_call
.def_id
).as_str() {
106 "`todo` should not be present in production code",
114 "`unimplemented` should not be present in production code",
118 span_lint(cx
, UNREACHABLE
, macro_call
.span
, "usage of the `unreachable!` macro");