1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::ty::is_type_diagnostic_item;
3 use clippy_utils::{is_in_test_function, is_lint_allowed};
5 use rustc_lint::LateContext;
8 use super::{EXPECT_USED, UNWRAP_USED};
10 /// lint use of `unwrap()` or `unwrap_err` for `Result` and `unwrap()` for `Option`.
16 allow_unwrap_in_tests: bool,
18 let obj_ty = cx.typeck_results().expr_ty(recv).peel_refs();
20 let mess = if is_type_diagnostic_item(cx, obj_ty, sym::Option) && !is_err {
21 Some((UNWRAP_USED, "an Option", "None", ""))
22 } else if is_type_diagnostic_item(cx, obj_ty, sym::Result) {
23 Some((UNWRAP_USED, "a Result", if is_err { "Ok" } else { "Err" }, "an "))
28 let method_suffix = if is_err { "_err" } else { "" };
30 if allow_unwrap_in_tests && is_in_test_function(cx.tcx, expr.hir_id) {
34 if let Some((lint, kind, none_value, none_prefix)) = mess {
35 let help = if is_lint_allowed(cx, EXPECT_USED, expr.hir_id) {
37 "if you don't want to handle the `{none_value}` case gracefully, consider \
38 using `expect{method_suffix}()` to provide a better panic message"
41 format!("if this value is {none_prefix}`{none_value}`, it will panic")
48 &format!("used `unwrap{method_suffix}()` on `{kind}` value"),