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