]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/panic_in_result_fn.rs
Rollup merge of #83092 - petrochenkov:qspan, r=estebank
[rust.git] / clippy_lints / src / panic_in_result_fn.rs
1 use crate::utils::{find_macro_calls, is_type_diagnostic_item, return_ty, span_lint_and_then};
2 use rustc_hir as hir;
3 use rustc_hir::intravisit::FnKind;
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
6 use rustc_span::{sym, Span};
7
8 declare_clippy_lint! {
9     /// **What it does:** Checks for usage of `panic!`, `unimplemented!`, `todo!`, `unreachable!` or assertions in a function of type result.
10     ///
11     /// **Why is this bad?** For some codebases, it is desirable for functions of type result to return an error instead of crashing. Hence panicking macros should be avoided.
12     ///
13     /// **Known problems:** Functions called from a function returning a `Result` may invoke a panicking macro. This is not checked.
14     ///
15     /// **Example:**
16     ///
17     /// ```rust
18     /// fn result_with_panic() -> Result<bool, String>
19     /// {
20     ///     panic!("error");
21     /// }
22     /// ```
23     /// Use instead:
24     /// ```rust
25     /// fn result_without_panic() -> Result<bool, String> {
26     ///     Err(String::from("error"))
27     /// }
28     /// ```
29     pub PANIC_IN_RESULT_FN,
30     restriction,
31     "functions of type `Result<..>` that contain `panic!()`, `todo!()`, `unreachable()`, `unimplemented()` or assertion"
32 }
33
34 declare_lint_pass!(PanicInResultFn  => [PANIC_IN_RESULT_FN]);
35
36 impl<'tcx> LateLintPass<'tcx> for PanicInResultFn {
37     fn check_fn(
38         &mut self,
39         cx: &LateContext<'tcx>,
40         fn_kind: FnKind<'tcx>,
41         _: &'tcx hir::FnDecl<'tcx>,
42         body: &'tcx hir::Body<'tcx>,
43         span: Span,
44         hir_id: hir::HirId,
45     ) {
46         if !matches!(fn_kind, FnKind::Closure) && is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym::result_type) {
47             lint_impl_body(cx, span, body);
48         }
49     }
50 }
51
52 fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, body: &'tcx hir::Body<'tcx>) {
53     let panics = find_macro_calls(
54         &[
55             "unimplemented",
56             "unreachable",
57             "panic",
58             "todo",
59             "assert",
60             "assert_eq",
61             "assert_ne",
62             "debug_assert",
63             "debug_assert_eq",
64             "debug_assert_ne",
65         ],
66         body,
67     );
68     if !panics.is_empty() {
69         span_lint_and_then(
70             cx,
71             PANIC_IN_RESULT_FN,
72             impl_span,
73             "used `unimplemented!()`, `unreachable!()`, `todo!()`, `panic!()` or assertion in a function that returns `Result`",
74             move |diag| {
75                 diag.help(
76                     "`unimplemented!()`, `unreachable!()`, `todo!()`, `panic!()` or assertions should not be used in a function that returns `Result` as `Result` is expected to return an error instead of crashing",
77                 );
78                 diag.span_note(panics, "return Err() instead of panicking");
79             },
80         );
81     }
82 }