Recognize Ok

This commit is contained in:
rsdy 2023-02-16 14:37:18 +00:00 committed by Pavan Kumar Sunkara
parent daf6197481
commit 1d159e7d11
4 changed files with 58 additions and 18 deletions

View file

@ -7,15 +7,17 @@ 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((UNNECESSARY_LITERAL_UNWRAP, "Some"))
Some("Some")
} else if is_res_lang_ctor(cx, path_res(cx, recv), hir::LangItem::ResultOk) {
Some("Ok")
} else {
None
};
if let Some((lint, constructor)) = mess {
if let Some(constructor) = mess {
span_lint_and_then(
cx,
lint,
UNNECESSARY_LITERAL_UNWRAP,
expr.span,
&format!("used `{name}()` on `{constructor}` value"),
|diag| {

View file

@ -2,10 +2,17 @@
#![warn(clippy::unnecessary_literal_unwrap)]
fn unwrap_option() {
let val = 1;
let val = 1;
let _val = 1;
let _val = 1;
}
fn unwrap_result() {
let _val = 1;
let _val = 1;
// let val = Err(1).unwrap_err();
}
fn main() {
unwrap_option();
unwrap_result();
}

View file

@ -2,10 +2,17 @@
#![warn(clippy::unnecessary_literal_unwrap)]
fn unwrap_option() {
let val = Some(1).unwrap();
let val = Some(1).expect("this never happens");
let _val = Some(1).unwrap();
let _val = Some(1).expect("this never happens");
}
fn unwrap_result() {
let _val = Ok::<usize, ()>(1).unwrap();
let _val = Ok::<usize, ()>(1).expect("this never happens");
// let val = Err(1).unwrap_err();
}
fn main() {
unwrap_option();
unwrap_result();
}

View file

@ -1,27 +1,51 @@
error: used `unwrap()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:5:15
--> $DIR/unnecessary_literal_unwrap.rs:5:16
|
LL | let val = Some(1).unwrap();
| ^^^^^^^^^^^^^^^^
LL | let _val = Some(1).unwrap();
| ^^^^^^^^^^^^^^^^
|
= 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;
LL - let _val = Some(1).unwrap();
LL + let _val = 1;
|
error: used `expect()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:6:15
--> $DIR/unnecessary_literal_unwrap.rs:6:16
|
LL | let val = Some(1).expect("this never happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL | let _val = Some(1).expect("this never happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `Some` and `expect()`
|
LL - let val = Some(1).expect("this never happens");
LL + let val = 1;
LL - let _val = Some(1).expect("this never happens");
LL + let _val = 1;
|
error: aborting due to 2 previous errors
error: used `unwrap()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:10:16
|
LL | let _val = Ok::<usize, ()>(1).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `Ok` and `unwrap()`
|
LL - let _val = Ok::<usize, ()>(1).unwrap();
LL + let _val = 1;
|
error: used `expect()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:11:16
|
LL | let _val = Ok::<usize, ()>(1).expect("this never happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `Ok` and `expect()`
|
LL - let _val = Ok::<usize, ()>(1).expect("this never happens");
LL + let _val = 1;
|
error: aborting due to 4 previous errors