1 use crate::utils::{is_expn_of, is_type_diagnostic_item, return_ty, span_lint_and_then};
3 use rustc_hir::intravisit::{self, FnKind, NestedVisitorMap, Visitor};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_middle::hir::map::Map;
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
8 use rustc_span::{sym, Span};
10 declare_clippy_lint! {
11 /// **What it does:** Checks for usage of `panic!`, `unimplemented!`, `todo!` or `unreachable!` in a function of type result.
13 /// **Why is this bad?** For some codebases, it is desirable for functions of type result to return an error instead of crashing. Hence unimplemented, panic and unreachable should be avoided.
15 /// **Known problems:** None.
20 /// fn result_with_panic() -> Result<bool, String>
25 pub PANIC_IN_RESULT_FN,
27 "functions of type `Result<..>` that contain `panic!()`, `todo!()` or `unreachable()` or `unimplemented()` "
30 declare_lint_pass!(PanicInResultFn => [PANIC_IN_RESULT_FN]);
32 impl<'tcx> LateLintPass<'tcx> for PanicInResultFn {
35 cx: &LateContext<'tcx>,
36 fn_kind: FnKind<'tcx>,
37 _: &'tcx hir::FnDecl<'tcx>,
38 body: &'tcx hir::Body<'tcx>,
42 if !matches!(fn_kind, FnKind::Closure(_))
43 && is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym::result_type)
45 lint_impl_body(cx, span, body);
50 struct FindPanicUnimplementedUnreachable {
54 impl<'tcx> Visitor<'tcx> for FindPanicUnimplementedUnreachable {
57 fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
58 if ["unimplemented", "unreachable", "panic", "todo"]
60 .any(|fun| is_expn_of(expr.span, fun).is_some())
62 self.result.push(expr.span);
64 // and check sub-expressions
65 intravisit::walk_expr(self, expr);
68 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
69 NestedVisitorMap::None
73 fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, body: &'tcx hir::Body<'tcx>) {
74 let mut panics = FindPanicUnimplementedUnreachable { result: Vec::new() };
75 panics.visit_expr(&body.value);
76 if !panics.result.is_empty() {
81 "used `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` in a function that returns `Result`",
84 "`unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` should not be used in a function that returns `Result` as `Result` is expected to return an error instead of crashing",
86 diag.span_note(panics.result, "return Err() instead of panicking");