mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 21:23:56 +00:00
Recognize Ok
This commit is contained in:
parent
daf6197481
commit
1d159e7d11
4 changed files with 58 additions and 18 deletions
|
@ -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| {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue