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