mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 15:14:29 +00:00
Add ui test for unnecessary_result_map_or_else
This commit is contained in:
parent
3b8f62f85e
commit
32bbeba16b
3 changed files with 165 additions and 0 deletions
61
tests/ui/unnecessary_result_map_or_else.fixed
Normal file
61
tests/ui/unnecessary_result_map_or_else.fixed
Normal file
|
@ -0,0 +1,61 @@
|
|||
#![warn(clippy::unnecessary_result_map_or_else)]
|
||||
#![allow(clippy::unnecessary_literal_unwrap, clippy::let_and_return, clippy::let_unit_value)]
|
||||
|
||||
fn main() {
|
||||
let x: Result<(), ()> = Ok(());
|
||||
x.unwrap_or_else(|err| err); //~ ERROR: unused "map closure" when calling
|
||||
|
||||
// Type ascribtion.
|
||||
let x: Result<(), ()> = Ok(());
|
||||
x.unwrap_or_else(|err: ()| err); //~ ERROR: unused "map closure" when calling
|
||||
|
||||
// Auto-deref.
|
||||
let y = String::new();
|
||||
let x: Result<&String, &String> = Ok(&y);
|
||||
let y: &str = x.unwrap_or_else(|err| err); //~ ERROR: unused "map closure" when calling
|
||||
|
||||
// Temporary variable.
|
||||
let x: Result<(), ()> = Ok(());
|
||||
x.unwrap_or_else(|err| err);
|
||||
|
||||
// Should not warn.
|
||||
let x: Result<usize, usize> = Ok(0);
|
||||
x.map_or_else(|err| err, |n| n + 1);
|
||||
|
||||
// Should not warn.
|
||||
let y = ();
|
||||
let x: Result<(), ()> = Ok(());
|
||||
x.map_or_else(|err| err, |_| y);
|
||||
|
||||
// Should not warn.
|
||||
let y = ();
|
||||
let x: Result<(), ()> = Ok(());
|
||||
x.map_or_else(
|
||||
|err| err,
|
||||
|_| {
|
||||
let tmp = y;
|
||||
tmp
|
||||
},
|
||||
);
|
||||
|
||||
// Should not warn.
|
||||
let x: Result<usize, usize> = Ok(1);
|
||||
x.map_or_else(
|
||||
|err| err,
|
||||
|n| {
|
||||
let tmp = n + 1;
|
||||
tmp
|
||||
},
|
||||
);
|
||||
|
||||
// Should not warn.
|
||||
let y = 0;
|
||||
let x: Result<usize, usize> = Ok(1);
|
||||
x.map_or_else(
|
||||
|err| err,
|
||||
|n| {
|
||||
let tmp = n;
|
||||
y
|
||||
},
|
||||
);
|
||||
}
|
69
tests/ui/unnecessary_result_map_or_else.rs
Normal file
69
tests/ui/unnecessary_result_map_or_else.rs
Normal file
|
@ -0,0 +1,69 @@
|
|||
#![warn(clippy::unnecessary_result_map_or_else)]
|
||||
#![allow(clippy::unnecessary_literal_unwrap, clippy::let_and_return, clippy::let_unit_value)]
|
||||
|
||||
fn main() {
|
||||
let x: Result<(), ()> = Ok(());
|
||||
x.map_or_else(|err| err, |n| n); //~ ERROR: unused "map closure" when calling
|
||||
|
||||
// Type ascribtion.
|
||||
let x: Result<(), ()> = Ok(());
|
||||
x.map_or_else(|err: ()| err, |n: ()| n); //~ ERROR: unused "map closure" when calling
|
||||
|
||||
// Auto-deref.
|
||||
let y = String::new();
|
||||
let x: Result<&String, &String> = Ok(&y);
|
||||
let y: &str = x.map_or_else(|err| err, |n| n); //~ ERROR: unused "map closure" when calling
|
||||
|
||||
// Temporary variable.
|
||||
let x: Result<(), ()> = Ok(());
|
||||
x.map_or_else(
|
||||
//~^ ERROR: unused "map closure" when calling
|
||||
|err| err,
|
||||
|n| {
|
||||
let tmp = n;
|
||||
let tmp2 = tmp;
|
||||
tmp2
|
||||
},
|
||||
);
|
||||
|
||||
// Should not warn.
|
||||
let x: Result<usize, usize> = Ok(0);
|
||||
x.map_or_else(|err| err, |n| n + 1);
|
||||
|
||||
// Should not warn.
|
||||
let y = ();
|
||||
let x: Result<(), ()> = Ok(());
|
||||
x.map_or_else(|err| err, |_| y);
|
||||
|
||||
// Should not warn.
|
||||
let y = ();
|
||||
let x: Result<(), ()> = Ok(());
|
||||
x.map_or_else(
|
||||
|err| err,
|
||||
|_| {
|
||||
let tmp = y;
|
||||
tmp
|
||||
},
|
||||
);
|
||||
|
||||
// Should not warn.
|
||||
let x: Result<usize, usize> = Ok(1);
|
||||
x.map_or_else(
|
||||
|err| err,
|
||||
|n| {
|
||||
let tmp = n + 1;
|
||||
tmp
|
||||
},
|
||||
);
|
||||
|
||||
// Should not warn.
|
||||
let y = 0;
|
||||
let x: Result<usize, usize> = Ok(1);
|
||||
x.map_or_else(
|
||||
|err| err,
|
||||
|n| {
|
||||
let tmp = n;
|
||||
y
|
||||
},
|
||||
);
|
||||
}
|
35
tests/ui/unnecessary_result_map_or_else.stderr
Normal file
35
tests/ui/unnecessary_result_map_or_else.stderr
Normal file
|
@ -0,0 +1,35 @@
|
|||
error: unused "map closure" when calling `Result::map_or_else` value
|
||||
--> $DIR/unnecessary_result_map_or_else.rs:6:5
|
||||
|
|
||||
LL | x.map_or_else(|err| err, |n| n);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `unwrap_or_else`: `x.unwrap_or_else(|err| err)`
|
||||
|
|
||||
= note: `-D clippy::unnecessary-result-map-or-else` implied by `-D warnings`
|
||||
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_result_map_or_else)]`
|
||||
|
||||
error: unused "map closure" when calling `Result::map_or_else` value
|
||||
--> $DIR/unnecessary_result_map_or_else.rs:10:5
|
||||
|
|
||||
LL | x.map_or_else(|err: ()| err, |n: ()| n);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `unwrap_or_else`: `x.unwrap_or_else(|err: ()| err)`
|
||||
|
||||
error: unused "map closure" when calling `Result::map_or_else` value
|
||||
--> $DIR/unnecessary_result_map_or_else.rs:15:19
|
||||
|
|
||||
LL | let y: &str = x.map_or_else(|err| err, |n| n);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `unwrap_or_else`: `x.unwrap_or_else(|err| err)`
|
||||
|
||||
error: unused "map closure" when calling `Result::map_or_else` value
|
||||
--> $DIR/unnecessary_result_map_or_else.rs:19:5
|
||||
|
|
||||
LL | / x.map_or_else(
|
||||
LL | |
|
||||
LL | | |err| err,
|
||||
LL | | |n| {
|
||||
... |
|
||||
LL | | },
|
||||
LL | | );
|
||||
| |_____^ help: consider using `unwrap_or_else`: `x.unwrap_or_else(|err| err)`
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
|
Loading…
Reference in a new issue