]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/methods/expect_used.rs
Auto merge of #7546 - mgeier:patch-1, r=giraffate
[rust.git] / clippy_lints / src / methods / expect_used.rs
1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::ty::is_type_diagnostic_item;
3 use rustc_hir as hir;
4 use rustc_lint::LateContext;
5 use rustc_span::sym;
6
7 use super::EXPECT_USED;
8
9 /// lint use of `expect()` for `Option`s and `Result`s
10 pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) {
11     let obj_ty = cx.typeck_results().expr_ty(recv).peel_refs();
12
13     let mess = if is_type_diagnostic_item(cx, obj_ty, sym::option_type) {
14         Some((EXPECT_USED, "an Option", "None"))
15     } else if is_type_diagnostic_item(cx, obj_ty, sym::result_type) {
16         Some((EXPECT_USED, "a Result", "Err"))
17     } else {
18         None
19     };
20
21     if let Some((lint, kind, none_value)) = mess {
22         span_lint_and_help(
23             cx,
24             lint,
25             expr.span,
26             &format!("used `expect()` on `{}` value", kind,),
27             None,
28             &format!("if this value is an `{}`, it will panic", none_value,),
29         );
30     }
31 }