mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-14 17:07:17 +00:00
Implement the suggestion
This commit is contained in:
parent
21b88ce290
commit
daf6197481
5 changed files with 44 additions and 14 deletions
|
@ -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),
|
||||
|
@ -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);
|
||||
},
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
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 {
|
||||
|
@ -12,14 +13,23 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
|
|||
};
|
||||
|
||||
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
tests/ui/unnecessary_literal_unwrap.fixed
Normal file
11
tests/ui/unnecessary_literal_unwrap.fixed
Normal file
|
@ -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,3 +1,4 @@
|
|||
//run-rustfix
|
||||
#![warn(clippy::unnecessary_literal_unwrap)]
|
||||
|
||||
fn unwrap_option() {
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue