Recognize unwrap_or methods

This commit is contained in:
Pavan Kumar Sunkara 2023-02-16 14:43:41 +00:00
parent 1d159e7d11
commit 8f83502989
5 changed files with 120 additions and 44 deletions

View File

@ -3664,9 +3664,7 @@ fn check_methods<'tcx>(&self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
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, [arg]) = recv.kind {
unnecessary_literal_unwrap::check(cx, expr, recv, arg, name);
}
unnecessary_literal_unwrap::check(cx, expr, recv, name);
},
("expect_err", [_]) => expect_used::check(cx, expr, recv, true, self.allow_expect_in_tests),
("extend", [arg]) => {
@ -3873,24 +3871,28 @@ fn check_methods<'tcx>(&self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
},
_ => {},
}
if let ExprKind::Call(recv, [arg]) = recv.kind {
unnecessary_literal_unwrap::check(cx, expr, recv, arg, name);
}
unnecessary_literal_unwrap::check(cx, expr, recv, name);
unwrap_used::check(cx, expr, recv, false, self.allow_unwrap_in_tests);
},
("unwrap_err", []) => unwrap_used::check(cx, expr, recv, true, self.allow_unwrap_in_tests),
("unwrap_or", [u_arg]) => match method_call(recv) {
Some((arith @ ("checked_add" | "checked_sub" | "checked_mul"), lhs, [rhs], _, _)) => {
manual_saturating_arithmetic::check(cx, expr, lhs, rhs, u_arg, &arith["checked_".len()..]);
},
Some(("map", m_recv, [m_arg], span, _)) => {
option_map_unwrap_or::check(cx, expr, m_recv, m_arg, recv, u_arg, span);
},
Some(("then_some", t_recv, [t_arg], _, _)) => {
obfuscated_if_else::check(cx, expr, t_recv, t_arg, u_arg);
},
_ => {},
("unwrap_or", [u_arg]) => {
match method_call(recv) {
Some((arith @ ("checked_add" | "checked_sub" | "checked_mul"), lhs, [rhs], _, _)) => {
manual_saturating_arithmetic::check(cx, expr, lhs, rhs, u_arg, &arith["checked_".len()..]);
},
Some(("map", m_recv, [m_arg], span, _)) => {
option_map_unwrap_or::check(cx, expr, m_recv, m_arg, recv, u_arg, span);
},
Some(("then_some", t_recv, [t_arg], _, _)) => {
obfuscated_if_else::check(cx, expr, t_recv, t_arg, u_arg);
},
_ => {},
}
unnecessary_literal_unwrap::check(cx, expr, recv, name);
},
("unwrap_or_default", []) => {
unnecessary_literal_unwrap::check(cx, expr, recv, name);
}
("unwrap_or_else", [u_arg]) => match method_call(recv) {
Some(("map", recv, [map_arg], _, _))
if map_unwrap_or::check(cx, expr, recv, map_arg, u_arg, &self.msrv) => {},

View File

@ -5,33 +5,35 @@
use super::UNNECESSARY_LITERAL_UNWRAP;
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("Some")
} else if is_res_lang_ctor(cx, path_res(cx, recv), hir::LangItem::ResultOk) {
Some("Ok")
} else {
None
};
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, name: &str) {
if let hir::ExprKind::Call(call, [arg]) = recv.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) {
Some("Ok")
} else {
None
};
if let Some(constructor) = mess {
span_lint_and_then(
cx,
UNNECESSARY_LITERAL_UNWRAP,
expr.span,
&format!("used `{name}()` on `{constructor}` value"),
|diag| {
let suggestions = vec![
(recv.span.with_hi(arg.span.lo()), String::new()),
(expr.span.with_lo(arg.span.hi()), String::new()),
];
if let Some(constructor) = mess {
span_lint_and_then(
cx,
UNNECESSARY_LITERAL_UNWRAP,
expr.span,
&format!("used `{name}()` on `{constructor}` value"),
|diag| {
let suggestions = vec![
(call.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,
);
},
);
diag.multipart_suggestion(
format!("remove the `{constructor}` and `{name}()`"),
suggestions,
Applicability::MachineApplicable,
);
},
);
}
}
}

View File

@ -12,7 +12,19 @@ fn unwrap_result() {
// let val = Err(1).unwrap_err();
}
fn unwrap_methods_option() {
let _val = 1;
let _val = 1;
}
fn unwrap_methods_result() {
let _val = 1;
let _val = 1;
}
fn main() {
unwrap_option();
unwrap_result();
unwrap_methods_option();
unwrap_methods_result();
}

View File

@ -12,7 +12,19 @@ fn unwrap_result() {
// let val = Err(1).unwrap_err();
}
fn unwrap_methods_option() {
let _val = Some(1).unwrap_or(2);
let _val = Some(1).unwrap_or_default();
}
fn unwrap_methods_result() {
let _val = Ok::<usize, ()>(1).unwrap_or(2);
let _val = Ok::<usize, ()>(1).unwrap_or_default();
}
fn main() {
unwrap_option();
unwrap_result();
unwrap_methods_option();
unwrap_methods_result();
}

View File

@ -47,5 +47,53 @@ LL - let _val = Ok::<usize, ()>(1).expect("this never happens");
LL + let _val = 1;
|
error: aborting due to 4 previous errors
error: used `unwrap_or()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:16:16
|
LL | let _val = Some(1).unwrap_or(2);
| ^^^^^^^^^^^^^^^^^^^^
|
help: remove the `Some` and `unwrap_or()`
|
LL - let _val = Some(1).unwrap_or(2);
LL + let _val = 1;
|
error: used `unwrap_or_default()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:17:16
|
LL | let _val = Some(1).unwrap_or_default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `Some` and `unwrap_or_default()`
|
LL - let _val = Some(1).unwrap_or_default();
LL + let _val = 1;
|
error: used `unwrap_or()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:21:16
|
LL | let _val = Ok::<usize, ()>(1).unwrap_or(2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `Ok` and `unwrap_or()`
|
LL - let _val = Ok::<usize, ()>(1).unwrap_or(2);
LL + let _val = 1;
|
error: used `unwrap_or_default()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:22:16
|
LL | let _val = Ok::<usize, ()>(1).unwrap_or_default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `Ok` and `unwrap_or_default()`
|
LL - let _val = Ok::<usize, ()>(1).unwrap_or_default();
LL + let _val = 1;
|
error: aborting due to 8 previous errors