Recognize unwrap_or_else method

This commit is contained in:
Pavan Kumar Sunkara 2023-02-16 15:05:56 +00:00
parent 8f83502989
commit 7ed7283e0f
5 changed files with 50 additions and 16 deletions

View File

@ -5248,6 +5248,7 @@ Released 2018-09-13
[`unnecessary_fold`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_fold
[`unnecessary_join`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_join
[`unnecessary_lazy_evaluations`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations
[`unnecessary_literal_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_literal_unwrap
[`unnecessary_mut_passed`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_mut_passed
[`unnecessary_operation`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation
[`unnecessary_owned_empty_strings`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_owned_empty_strings

View File

@ -3893,13 +3893,16 @@ fn check_methods<'tcx>(&self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
("unwrap_or_default", []) => {
unnecessary_literal_unwrap::check(cx, expr, recv, name);
}
("unwrap_or_else", [u_arg]) => match method_call(recv) {
("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) => {},
_ => {
unwrap_or_else_default::check(cx, expr, recv, u_arg);
unnecessary_lazy_eval::check(cx, expr, recv, u_arg, "unwrap_or");
},
}
unnecessary_literal_unwrap::check(cx, expr, recv, name);
},
("zip", [arg]) => {
if let ExprKind::MethodCall(name, iter_recv, [], _) = recv.kind

View File

@ -1,5 +1,6 @@
//run-rustfix
#![warn(clippy::unnecessary_literal_unwrap)]
#![allow(clippy::unnecessary_lazy_evaluations)]
fn unwrap_option() {
let _val = 1;
@ -15,11 +16,13 @@ fn unwrap_result() {
fn unwrap_methods_option() {
let _val = 1;
let _val = 1;
let _val = 1;
}
fn unwrap_methods_result() {
let _val = 1;
let _val = 1;
let _val = 1;
}
fn main() {

View File

@ -1,5 +1,6 @@
//run-rustfix
#![warn(clippy::unnecessary_literal_unwrap)]
#![allow(clippy::unnecessary_lazy_evaluations)]
fn unwrap_option() {
let _val = Some(1).unwrap();
@ -15,11 +16,13 @@ fn unwrap_result() {
fn unwrap_methods_option() {
let _val = Some(1).unwrap_or(2);
let _val = Some(1).unwrap_or_default();
let _val = Some(1).unwrap_or_else(|| _val);
}
fn unwrap_methods_result() {
let _val = Ok::<usize, ()>(1).unwrap_or(2);
let _val = Ok::<usize, ()>(1).unwrap_or_default();
let _val = Ok::<usize, ()>(1).unwrap_or_else(|()| _val);
}
fn main() {

View File

@ -1,5 +1,5 @@
error: used `unwrap()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:5:16
--> $DIR/unnecessary_literal_unwrap.rs:6:16
|
LL | let _val = Some(1).unwrap();
| ^^^^^^^^^^^^^^^^
@ -12,7 +12,7 @@ LL + let _val = 1;
|
error: used `expect()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:6:16
--> $DIR/unnecessary_literal_unwrap.rs:7:16
|
LL | let _val = Some(1).expect("this never happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -24,7 +24,7 @@ LL + let _val = 1;
|
error: used `unwrap()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:10:16
--> $DIR/unnecessary_literal_unwrap.rs:11:16
|
LL | let _val = Ok::<usize, ()>(1).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -36,7 +36,7 @@ LL + let _val = 1;
|
error: used `expect()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:11:16
--> $DIR/unnecessary_literal_unwrap.rs:12:16
|
LL | let _val = Ok::<usize, ()>(1).expect("this never happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -48,7 +48,7 @@ LL + let _val = 1;
|
error: used `unwrap_or()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:16:16
--> $DIR/unnecessary_literal_unwrap.rs:17:16
|
LL | let _val = Some(1).unwrap_or(2);
| ^^^^^^^^^^^^^^^^^^^^
@ -60,7 +60,7 @@ LL + let _val = 1;
|
error: used `unwrap_or_default()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:17:16
--> $DIR/unnecessary_literal_unwrap.rs:18:16
|
LL | let _val = Some(1).unwrap_or_default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -71,8 +71,20 @@ LL - let _val = Some(1).unwrap_or_default();
LL + let _val = 1;
|
error: used `unwrap_or_else()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:19:16
|
LL | let _val = Some(1).unwrap_or_else(|| _val);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `Some` and `unwrap_or_else()`
|
LL - let _val = Some(1).unwrap_or_else(|| _val);
LL + let _val = 1;
|
error: used `unwrap_or()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:21:16
--> $DIR/unnecessary_literal_unwrap.rs:23:16
|
LL | let _val = Ok::<usize, ()>(1).unwrap_or(2);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -84,7 +96,7 @@ LL + let _val = 1;
|
error: used `unwrap_or_default()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:22:16
--> $DIR/unnecessary_literal_unwrap.rs:24:16
|
LL | let _val = Ok::<usize, ()>(1).unwrap_or_default();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -95,5 +107,17 @@ LL - let _val = Ok::<usize, ()>(1).unwrap_or_default();
LL + let _val = 1;
|
error: aborting due to 8 previous errors
error: used `unwrap_or_else()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:25:16
|
LL | let _val = Ok::<usize, ()>(1).unwrap_or_else(|()| _val);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `Ok` and `unwrap_or_else()`
|
LL - let _val = Ok::<usize, ()>(1).unwrap_or_else(|()| _val);
LL + let _val = 1;
|
error: aborting due to 10 previous errors