Skip to content

Commit daf6197

Browse files
committed
Implement the suggestion
1 parent 21b88ce commit daf6197

File tree

5 files changed

+44
-14
lines changed

5 files changed

+44
-14
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3664,8 +3664,8 @@ impl Methods {
36643664
Some(("err", recv, [], err_span, _)) => err_expect::check(cx, expr, recv, span, err_span, &self.msrv),
36653665
_ => expect_used::check(cx, expr, recv, false, self.allow_expect_in_tests),
36663666
}
3667-
if let ExprKind::Call(recv, _) = recv.kind {
3668-
unnecessary_literal_unwrap::check(cx, expr, recv, name);
3667+
if let ExprKind::Call(recv, [arg]) = recv.kind {
3668+
unnecessary_literal_unwrap::check(cx, expr, recv, arg, name);
36693669
}
36703670
},
36713671
("expect_err", [_]) => expect_used::check(cx, expr, recv, true, self.allow_expect_in_tests),
@@ -3873,8 +3873,8 @@ impl Methods {
38733873
},
38743874
_ => {},
38753875
}
3876-
if let ExprKind::Call(recv, _) = recv.kind {
3877-
unnecessary_literal_unwrap::check(cx, expr, recv, name);
3876+
if let ExprKind::Call(recv, [arg]) = recv.kind {
3877+
unnecessary_literal_unwrap::check(cx, expr, recv, arg, name);
38783878
}
38793879
unwrap_used::check(cx, expr, recv, false, self.allow_unwrap_in_tests);
38803880
},
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
1-
use clippy_utils::{diagnostics::span_lint_and_help, is_res_lang_ctor, path_res};
1+
use clippy_utils::{diagnostics::span_lint_and_then, is_res_lang_ctor, path_res};
2+
use rustc_errors::Applicability;
23
use rustc_hir as hir;
34
use rustc_lint::LateContext;
45

56
use super::UNNECESSARY_LITERAL_UNWRAP;
67

7-
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, name: &str) {
8+
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>, name: &str) {
89
let mess = if is_res_lang_ctor(cx, path_res(cx, recv), hir::LangItem::OptionSome) {
910
Some((UNNECESSARY_LITERAL_UNWRAP, "Some"))
1011
} else {
1112
None
1213
};
1314

1415
if let Some((lint, constructor)) = mess {
15-
let help = String::new();
16-
span_lint_and_help(
16+
span_lint_and_then(
1717
cx,
1818
lint,
1919
expr.span,
2020
&format!("used `{name}()` on `{constructor}` value"),
21-
None,
22-
&help,
21+
|diag| {
22+
let suggestions = vec![
23+
(recv.span.with_hi(arg.span.lo()), String::new()),
24+
(expr.span.with_lo(arg.span.hi()), String::new()),
25+
];
26+
27+
diag.multipart_suggestion(
28+
format!("remove the `{constructor}` and `{name}()`"),
29+
suggestions,
30+
Applicability::MachineApplicable,
31+
);
32+
},
2333
);
2434
}
2535
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//run-rustfix
2+
#![warn(clippy::unnecessary_literal_unwrap)]
3+
4+
fn unwrap_option() {
5+
let val = 1;
6+
let val = 1;
7+
}
8+
9+
fn main() {
10+
unwrap_option();
11+
}

tests/ui/unnecessary_literal_unwrap.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//run-rustfix
12
#![warn(clippy::unnecessary_literal_unwrap)]
23

34
fn unwrap_option() {
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
error: used `unwrap()` on `Some` value
2-
--> $DIR/unnecessary_literal_unwrap.rs:4:15
2+
--> $DIR/unnecessary_literal_unwrap.rs:5:15
33
|
44
LL | let val = Some(1).unwrap();
55
| ^^^^^^^^^^^^^^^^
66
|
7-
= help:
87
= note: `-D clippy::unnecessary-literal-unwrap` implied by `-D warnings`
8+
help: remove the `Some` and `unwrap()`
9+
|
10+
LL - let val = Some(1).unwrap();
11+
LL + let val = 1;
12+
|
913

1014
error: used `expect()` on `Some` value
11-
--> $DIR/unnecessary_literal_unwrap.rs:5:15
15+
--> $DIR/unnecessary_literal_unwrap.rs:6:15
1216
|
1317
LL | let val = Some(1).expect("this never happens");
1418
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1519
|
16-
= help:
20+
help: remove the `Some` and `expect()`
21+
|
22+
LL - let val = Some(1).expect("this never happens");
23+
LL + let val = 1;
24+
|
1725

1826
error: aborting due to 2 previous errors
1927

0 commit comments

Comments
 (0)