Track init and unwrap of expr
This commit is contained in:
parent
7ed7283e0f
commit
6e0e09c8f7
|
@ -6,7 +6,9 @@ use rustc_lint::LateContext;
|
|||
use super::UNNECESSARY_LITERAL_UNWRAP;
|
||||
|
||||
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, name: &str) {
|
||||
if let hir::ExprKind::Call(call, [arg]) = recv.kind {
|
||||
let init = clippy_utils::expr_or_init(cx, recv);
|
||||
|
||||
if let hir::ExprKind::Call(call, [arg]) = init.kind {
|
||||
let mess = if is_res_lang_ctor(cx, path_res(cx, call), hir::LangItem::OptionSome) {
|
||||
Some("Some")
|
||||
} else if is_res_lang_ctor(cx, path_res(cx, call), hir::LangItem::ResultOk) {
|
||||
|
@ -15,7 +17,11 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
|
|||
None
|
||||
};
|
||||
|
||||
if let Some(constructor) = mess {
|
||||
let Some(constructor) = mess else {
|
||||
return;
|
||||
};
|
||||
|
||||
if init.span == recv.span {
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
UNNECESSARY_LITERAL_UNWRAP,
|
||||
|
@ -23,7 +29,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
|
|||
&format!("used `{name}()` on `{constructor}` value"),
|
||||
|diag| {
|
||||
let suggestions = vec![
|
||||
(call.span.with_hi(arg.span.lo()), String::new()),
|
||||
(recv.span.with_hi(arg.span.lo()), String::new()),
|
||||
(expr.span.with_lo(arg.span.hi()), String::new()),
|
||||
];
|
||||
|
||||
|
@ -34,6 +40,16 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr
|
|||
);
|
||||
},
|
||||
);
|
||||
} else {
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
UNNECESSARY_LITERAL_UNWRAP,
|
||||
expr.span,
|
||||
&format!("used `{name}()` on `{constructor}` value"),
|
||||
|diag| {
|
||||
diag.span_help(init.span, format!("remove the `{constructor}` and `{name}()`"));
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
#![warn(clippy::unnecessary_literal_unwrap)]
|
||||
|
||||
fn main() {
|
||||
let val = Some(1);
|
||||
let _val2 = val.unwrap();
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
error: used `unwrap()` on `Some` value
|
||||
--> $DIR/unnecessary_literal_unwrap_unfixable.rs:5:17
|
||||
|
|
||||
LL | let _val2 = val.unwrap();
|
||||
| ^^^^^^^^^^^^
|
||||
|
|
||||
help: remove the `Some` and `unwrap()`
|
||||
--> $DIR/unnecessary_literal_unwrap_unfixable.rs:4:15
|
||||
|
|
||||
LL | let val = Some(1);
|
||||
| ^^^^^^^
|
||||
= note: `-D clippy::unnecessary-literal-unwrap` implied by `-D warnings`
|
||||
|
||||
error: aborting due to previous error
|
||||
|
Loading…
Reference in New Issue