]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/methods/unwrap_used.rs
Auto merge of #7546 - mgeier:patch-1, r=giraffate
[rust.git] / clippy_lints / src / methods / unwrap_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::UNWRAP_USED;
8
9 /// lint use of `unwrap()` 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((UNWRAP_USED, "an Option", "None"))
15     } else if is_type_diagnostic_item(cx, obj_ty, sym::result_type) {
16         Some((UNWRAP_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 `unwrap()` on `{}` value", kind,),
27             None,
28             &format!(
29                 "if you don't want to handle the `{}` case gracefully, consider \
30                 using `expect()` to provide a better panic message",
31                 none_value,
32             ),
33         );
34     }
35 }