rust/clippy_lints/src/panic_unimplemented.rs

124 lines
3.4 KiB
Rust
Raw Normal View History

use clippy_utils::diagnostics::span_lint;
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
use rustc_hir::Expr;
2020-01-12 00:08:41 -06:00
use rustc_lint::{LateContext, LateLintPass};
2020-01-11 05:37:08 -06:00
use rustc_session::{declare_lint_pass, declare_tool_lint};
2015-12-23 15:37:52 -06:00
declare_clippy_lint! {
/// ### What it does
/// Checks for usage of `panic!`.
///
/// ### Why is this bad?
/// `panic!` will stop the execution of the executable.
///
/// ### Example
/// ```no_run
/// panic!("even with a good reason");
/// ```
#[clippy::version = "1.40.0"]
pub PANIC,
restriction,
2019-10-16 12:43:26 -05:00
"usage of the `panic!` macro"
}
2018-05-23 09:43:05 -05:00
declare_clippy_lint! {
/// ### What it does
/// Checks for usage of `unimplemented!`.
///
/// ### Why is this bad?
/// This macro should not be present in production code.
///
/// ### Example
2019-03-05 16:23:50 -06:00
/// ```no_run
/// unimplemented!();
/// ```
#[clippy::version = "pre 1.29.0"]
2018-05-23 09:43:05 -05:00
pub UNIMPLEMENTED,
restriction,
2018-05-23 09:43:05 -05:00
"`unimplemented!` should not be present in production code"
}
2019-10-12 06:26:14 -05:00
declare_clippy_lint! {
/// ### What it does
/// Checks for usage of `todo!`.
2019-10-12 06:26:14 -05:00
///
/// ### Why is this bad?
/// The `todo!` macro is often used for unfinished code, and it causes
/// code to panic. It should not be present in production code.
2019-10-12 06:26:14 -05:00
///
/// ### Example
2019-10-12 06:26:14 -05:00
/// ```no_run
/// todo!();
/// ```
/// Finish the implementation, or consider marking it as explicitly unimplemented.
/// ```no_run
/// unimplemented!();
/// ```
#[clippy::version = "1.40.0"]
2019-10-12 06:26:14 -05:00
pub TODO,
restriction,
"`todo!` should not be present in production code"
}
declare_clippy_lint! {
/// ### What it does
/// Checks for usage of `unreachable!`.
///
/// ### Why is this bad?
/// This macro can cause code to panic.
///
/// ### Example
/// ```no_run
/// unreachable!();
/// ```
#[clippy::version = "1.40.0"]
pub UNREACHABLE,
restriction,
"usage of the `unreachable!` macro"
}
declare_lint_pass!(PanicUnimplemented => [UNIMPLEMENTED, UNREACHABLE, TODO, PANIC]);
2015-12-23 15:37:52 -06:00
impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
let Some(macro_call) = root_macro_call_first_node(cx, expr) else {
return;
};
if is_panic(cx, macro_call.def_id) {
if cx.tcx.hir().is_inside_const_context(expr.hir_id) {
return;
}
span_lint(
cx,
PANIC,
macro_call.span,
"`panic` should not be present in production code",
);
return;
}
match cx.tcx.item_name(macro_call.def_id).as_str() {
"todo" => {
span_lint(
cx,
TODO,
macro_call.span,
"`todo` should not be present in production code",
);
},
"unimplemented" => {
span_lint(
cx,
UNIMPLEMENTED,
macro_call.span,
"`unimplemented` should not be present in production code",
);
},
"unreachable" => {
span_lint(cx, UNREACHABLE, macro_call.span, "usage of the `unreachable!` macro");
},
_ => {},
}
}
}