Skip to content

Commit

Permalink
Implement the suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Jun 12, 2023
1 parent 21b88ce commit daf6197
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
8 changes: 4 additions & 4 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3664,8 +3664,8 @@ impl Methods {
Some(("err", recv, [], err_span, _)) => err_expect::check(cx, expr, recv, span, err_span, &self.msrv),
_ => expect_used::check(cx, expr, recv, false, self.allow_expect_in_tests),
}
if let ExprKind::Call(recv, _) = recv.kind {
unnecessary_literal_unwrap::check(cx, expr, recv, name);
if let ExprKind::Call(recv, [arg]) = recv.kind {
unnecessary_literal_unwrap::check(cx, expr, recv, arg, name);
}
},
("expect_err", [_]) => expect_used::check(cx, expr, recv, true, self.allow_expect_in_tests),
Expand Down Expand Up @@ -3873,8 +3873,8 @@ impl Methods {
},
_ => {},
}
if let ExprKind::Call(recv, _) = recv.kind {
unnecessary_literal_unwrap::check(cx, expr, recv, name);
if let ExprKind::Call(recv, [arg]) = recv.kind {
unnecessary_literal_unwrap::check(cx, expr, recv, arg, name);
}
unwrap_used::check(cx, expr, recv, false, self.allow_unwrap_in_tests);
},
Expand Down
22 changes: 16 additions & 6 deletions clippy_lints/src/methods/unnecessary_literal_unwrap.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
use clippy_utils::{diagnostics::span_lint_and_help, is_res_lang_ctor, path_res};
use clippy_utils::{diagnostics::span_lint_and_then, is_res_lang_ctor, path_res};
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;

use super::UNNECESSARY_LITERAL_UNWRAP;

pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, name: &str) {
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>, name: &str) {
let mess = if is_res_lang_ctor(cx, path_res(cx, recv), hir::LangItem::OptionSome) {
Some((UNNECESSARY_LITERAL_UNWRAP, "Some"))
} else {
None
};

if let Some((lint, constructor)) = mess {
let help = String::new();
span_lint_and_help(
span_lint_and_then(
cx,
lint,
expr.span,
&format!("used `{name}()` on `{constructor}` value"),
None,
&help,
|diag| {
let suggestions = vec![
(recv.span.with_hi(arg.span.lo()), String::new()),
(expr.span.with_lo(arg.span.hi()), String::new()),
];

diag.multipart_suggestion(
format!("remove the `{constructor}` and `{name}()`"),
suggestions,
Applicability::MachineApplicable,
);
},
);
}
}
11 changes: 11 additions & 0 deletions tests/ui/unnecessary_literal_unwrap.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//run-rustfix
#![warn(clippy::unnecessary_literal_unwrap)]

fn unwrap_option() {
let val = 1;
let val = 1;
}

fn main() {
unwrap_option();
}
1 change: 1 addition & 0 deletions tests/ui/unnecessary_literal_unwrap.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//run-rustfix
#![warn(clippy::unnecessary_literal_unwrap)]

fn unwrap_option() {
Expand Down
16 changes: 12 additions & 4 deletions tests/ui/unnecessary_literal_unwrap.stderr
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
error: used `unwrap()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:4:15
--> $DIR/unnecessary_literal_unwrap.rs:5:15
|
LL | let val = Some(1).unwrap();
| ^^^^^^^^^^^^^^^^
|
= help:
= note: `-D clippy::unnecessary-literal-unwrap` implied by `-D warnings`
help: remove the `Some` and `unwrap()`
|
LL - let val = Some(1).unwrap();
LL + let val = 1;
|

error: used `expect()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:5:15
--> $DIR/unnecessary_literal_unwrap.rs:6:15
|
LL | let val = Some(1).expect("this never happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help:
help: remove the `Some` and `expect()`
|
LL - let val = Some(1).expect("this never happens");
LL + let val = 1;
|

error: aborting due to 2 previous errors

0 comments on commit daf6197

Please sign in to comment.