]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/methods/expect_used.rs
New upstream version 1.52.1+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / methods / expect_used.rs
CommitLineData
f20569fa
XL
1use crate::utils::{is_type_diagnostic_item, span_lint_and_help};
2use rustc_hir as hir;
3use rustc_lint::LateContext;
4use rustc_span::sym;
5
6use super::EXPECT_USED;
7
8/// lint use of `expect()` for `Option`s and `Result`s
9pub(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}