Skip to content

Commit 0b1bb5f

Browse files
committed
Implement the lint
1 parent 2902359 commit 0b1bb5f

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3866,11 +3866,11 @@ impl Methods {
38663866
Some(("or", recv, [or_arg], or_span, _)) => {
38673867
or_then_unwrap::check(cx, expr, recv, or_arg, or_span);
38683868
},
3869-
Some((constructor @ "Some", _, _, _, _)) => {
3870-
unnecessary_literal_unwrap::check(cx, expr, recv, constructor);
3871-
}
38723869
_ => {},
38733870
}
3871+
if let ExprKind::Call(recv, _) = recv.kind {
3872+
unnecessary_literal_unwrap::check(cx, expr, recv, name);
3873+
}
38743874
unwrap_used::check(cx, expr, recv, false, self.allow_unwrap_in_tests);
38753875
},
38763876
("unwrap_err", []) => unwrap_used::check(cx, expr, recv, true, self.allow_unwrap_in_tests),

clippy_lints/src/methods/unnecessary_literal_unwrap.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
1-
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::ty::is_type_diagnostic_item;
1+
use clippy_utils::{diagnostics::span_lint_and_help, is_res_lang_ctor, path_res};
32
use rustc_hir as hir;
43
use rustc_lint::LateContext;
5-
use rustc_span::sym;
64

75
use super::UNNECESSARY_LITERAL_UNWRAP;
86

9-
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, constructor: &str) {
10-
let obj_ty = cx.typeck_results().expr_ty(recv).peel_refs();
11-
12-
let mess = if is_type_diagnostic_item(cx, obj_ty, sym::Option) {
13-
Some((UNNECESSARY_LITERAL_UNWRAP, "an `Option`", "None", ""))
7+
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, name: &str) {
8+
let mess = if is_res_lang_ctor(cx, path_res(cx, recv), hir::LangItem::OptionSome) {
9+
Some((UNNECESSARY_LITERAL_UNWRAP, "Some"))
1410
} else {
1511
None
1612
};
1713

18-
if let Some((lint, kind, none_value, none_prefix)) = mess {
19-
let help = format!("if this value is {none_prefix}`{none_value}`, it will panic");
20-
14+
if let Some((lint, constructor)) = mess {
15+
let help = String::new();
2116
span_lint_and_help(
2217
cx,
2318
lint,
2419
expr.span,
25-
&format!("used `unwrap()` on {kind} value"),
20+
&format!("used `{name}()` on `{constructor}` value"),
2621
None,
2722
&help,
2823
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: used `unwrap()` on `Some` value
2+
--> $DIR/unnecessary_literal_unwrap.rs:4:15
3+
|
4+
LL | let val = Some(1).unwrap();
5+
| ^^^^^^^^^^^^^^^^
6+
|
7+
= help:
8+
= note: `-D clippy::unnecessary-literal-unwrap` implied by `-D warnings`
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)