]>
Commit | Line | Data |
---|---|---|
f20569fa XL |
1 | use crate::utils::{is_type_diagnostic_item, span_lint_and_help}; |
2 | use rustc_hir as hir; | |
3 | use rustc_lint::LateContext; | |
4 | use rustc_span::sym; | |
5 | ||
6 | use super::EXPECT_USED; | |
7 | ||
8 | /// lint use of `expect()` for `Option`s and `Result`s | |
9 | pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, expect_args: &[hir::Expr<'_>]) { | |
10 | let obj_ty = cx.typeck_results().expr_ty(&expect_args[0]).peel_refs(); | |
11 | ||
12 | let mess = if is_type_diagnostic_item(cx, obj_ty, sym::option_type) { | |
13 | Some((EXPECT_USED, "an Option", "None")) | |
14 | } else if is_type_diagnostic_item(cx, obj_ty, sym::result_type) { | |
15 | Some((EXPECT_USED, "a Result", "Err")) | |
16 | } else { | |
17 | None | |
18 | }; | |
19 | ||
20 | if let Some((lint, kind, none_value)) = mess { | |
21 | span_lint_and_help( | |
22 | cx, | |
23 | lint, | |
24 | expr.span, | |
25 | &format!("used `expect()` on `{}` value", kind,), | |
26 | None, | |
27 | &format!("if this value is an `{}`, it will panic", none_value,), | |
28 | ); | |
29 | } | |
30 | } |