src/tools/clippy/clippy_lints/src/panic_unimplemented.rs RUST 157 lines View on github.com → Search inside
1use clippy_config::Conf;2use clippy_utils::diagnostics::span_lint;3use clippy_utils::macros::{is_panic, root_macro_call_first_node};4use clippy_utils::{is_in_test, is_inside_always_const_context, sym};5use rustc_hir::def::{DefKind, Res};6use rustc_hir::{Expr, ExprKind, QPath};7use rustc_lint::{LateContext, LateLintPass};8use rustc_session::impl_lint_pass;910pub struct PanicUnimplemented {11    allow_panic_in_tests: bool,12}1314impl PanicUnimplemented {15    pub fn new(conf: &'static Conf) -> Self {16        Self {17            allow_panic_in_tests: conf.allow_panic_in_tests,18        }19    }20}2122declare_clippy_lint! {23    /// ### What it does24    /// Checks for usage of `panic!`.25    ///26    /// ### Why restrict this?27    /// This macro, or panics in general, may be unwanted in production code.28    ///29    /// ### Example30    /// ```no_run31    /// panic!("even with a good reason");32    /// ```33    #[clippy::version = "1.40.0"]34    pub PANIC,35    restriction,36    "usage of the `panic!` macro"37}3839declare_clippy_lint! {40    /// ### What it does41    /// Checks for usage of `todo!`.42    ///43    /// ### Why restrict this?44    /// The `todo!` macro indicates the presence of unfinished code,45    /// so it should not be present in production code.46    ///47    /// ### Example48    /// ```no_run49    /// todo!();50    /// ```51    /// Finish the implementation, or consider marking it as explicitly unimplemented.52    /// ```no_run53    /// unimplemented!();54    /// ```55    #[clippy::version = "1.40.0"]56    pub TODO,57    restriction,58    "`todo!` should not be present in production code"59}6061declare_clippy_lint! {62    /// ### What it does63    /// Checks for usage of `unimplemented!`.64    ///65    /// ### Why restrict this?66    /// This macro, or panics in general, may be unwanted in production code.67    ///68    /// ### Example69    /// ```no_run70    /// unimplemented!();71    /// ```72    #[clippy::version = "pre 1.29.0"]73    pub UNIMPLEMENTED,74    restriction,75    "`unimplemented!` should not be present in production code"76}7778declare_clippy_lint! {79    /// ### What it does80    /// Checks for usage of `unreachable!`.81    ///82    /// ### Why restrict this?83    /// This macro, or panics in general, may be unwanted in production code.84    ///85    /// ### Example86    /// ```no_run87    /// unreachable!();88    /// ```89    #[clippy::version = "1.40.0"]90    pub UNREACHABLE,91    restriction,92    "usage of the `unreachable!` macro"93}9495impl_lint_pass!(PanicUnimplemented => [PANIC, TODO, UNIMPLEMENTED, UNREACHABLE]);9697impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {98    fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {99        if let Some(macro_call) = root_macro_call_first_node(cx, expr) {100            if is_panic(cx, macro_call.def_id) {101                if is_inside_always_const_context(cx.tcx, expr.hir_id)102                    || self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)103                {104                    return;105                }106107                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.get_diagnostic_name(macro_call.def_id) {116                Some(sym::todo_macro) => {117                    span_lint(118                        cx,119                        TODO,120                        macro_call.span,121                        "`todo` should not be present in production code",122                    );123                },124                Some(sym::unimplemented_macro) => {125                    span_lint(126                        cx,127                        UNIMPLEMENTED,128                        macro_call.span,129                        "`unimplemented` should not be present in production code",130                    );131                },132                Some(sym::unreachable_macro) => {133                    span_lint(cx, UNREACHABLE, macro_call.span, "usage of the `unreachable!` macro");134                },135                _ => {},136            }137        } else if let ExprKind::Call(func, [_]) = expr.kind138            && let ExprKind::Path(QPath::Resolved(None, expr_path)) = func.kind139            && let Res::Def(DefKind::Fn, def_id) = expr_path.res140            && cx.tcx.is_diagnostic_item(sym::panic_any, def_id)141        {142            if is_inside_always_const_context(cx.tcx, expr.hir_id)143                || self.allow_panic_in_tests && is_in_test(cx.tcx, expr.hir_id)144            {145                return;146            }147148            span_lint(149                cx,150                PANIC,151                expr.span,152                "`panic_any` should not be present in production code",153            );154        }155    }156}

Code quality findings 8

Maintainability Info: `todo!()` or `unimplemented!()` macros indicate incomplete code paths that will panic at runtime if reached. Ensure these are replaced with actual logic before production use.
info correctness todo-unimplemented
/// Checks for usage of `todo!`.
Maintainability Info: `todo!()` or `unimplemented!()` macros indicate incomplete code paths that will panic at runtime if reached. Ensure these are replaced with actual logic before production use.
info correctness todo-unimplemented
/// The `todo!` macro indicates the presence of unfinished code,
Maintainability Info: `todo!()` or `unimplemented!()` macros indicate incomplete code paths that will panic at runtime if reached. Ensure these are replaced with actual logic before production use.
info correctness todo-unimplemented
/// todo!();
Maintainability Info: `todo!()` or `unimplemented!()` macros indicate incomplete code paths that will panic at runtime if reached. Ensure these are replaced with actual logic before production use.
info correctness todo-unimplemented
/// unimplemented!();
Maintainability Info: `todo!()` or `unimplemented!()` macros indicate incomplete code paths that will panic at runtime if reached. Ensure these are replaced with actual logic before production use.
info correctness todo-unimplemented
"`todo!` should not be present in production code"
Maintainability Info: `todo!()` or `unimplemented!()` macros indicate incomplete code paths that will panic at runtime if reached. Ensure these are replaced with actual logic before production use.
info correctness todo-unimplemented
/// Checks for usage of `unimplemented!`.
Maintainability Info: `todo!()` or `unimplemented!()` macros indicate incomplete code paths that will panic at runtime if reached. Ensure these are replaced with actual logic before production use.
info correctness todo-unimplemented
/// unimplemented!();
Maintainability Info: `todo!()` or `unimplemented!()` macros indicate incomplete code paths that will panic at runtime if reached. Ensure these are replaced with actual logic before production use.
info correctness todo-unimplemented
"`unimplemented!` should not be present in production code"

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.