Skip to content

Commit

Permalink
Add new test for result_map_or_into_option extension
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 22, 2023
1 parent 6d66ae0 commit 773db59
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
8 changes: 7 additions & 1 deletion tests/ui/result_map_or_into_option.fixed
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#![warn(clippy::result_map_or_into_option)]
#![deny(clippy::result_map_or_into_option)]

fn main() {
let opt: Result<u32, &str> = Ok(1);
let _ = opt.ok();
//~^ ERROR: called `map_or(None, Some)` on a `Result` value.
let _ = opt.ok();
//~^ ERROR: called `map_or_else(|_| None, Some)` on a `Result` value
let _ = opt.ok();
//~^ ERROR: called `map_or_else(|_| None, Some)` on a `Result` value

let rewrap = |s: u32| -> Option<u32> { Some(s) };

Expand All @@ -14,4 +19,5 @@ fn main() {
// return should not emit the lint
let opt: Result<u32, &str> = Ok(1);
_ = opt.map_or(None, |_x| Some(1));
let _ = opt.map_or_else(|a| a.parse::<u32>().ok(), Some);
}
8 changes: 7 additions & 1 deletion tests/ui/result_map_or_into_option.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
#![warn(clippy::result_map_or_into_option)]
#![deny(clippy::result_map_or_into_option)]

fn main() {
let opt: Result<u32, &str> = Ok(1);
let _ = opt.map_or(None, Some);
//~^ ERROR: called `map_or(None, Some)` on a `Result` value.
let _ = opt.map_or_else(|_| None, Some);
//~^ ERROR: called `map_or_else(|_| None, Some)` on a `Result` value
let _ = opt.map_or_else(|_| None, Some);
//~^ ERROR: called `map_or_else(|_| None, Some)` on a `Result` value

let rewrap = |s: u32| -> Option<u32> { Some(s) };

Expand All @@ -14,4 +19,5 @@ fn main() {
// return should not emit the lint
let opt: Result<u32, &str> = Ok(1);
_ = opt.map_or(None, |_x| Some(1));
let _ = opt.map_or_else(|a| a.parse::<u32>().ok(), Some);
}
21 changes: 18 additions & 3 deletions tests/ui/result_map_or_into_option.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,23 @@ error: called `map_or(None, Some)` on a `Result` value. This can be done more di
LL | let _ = opt.map_or(None, Some);
| ^^^^^^^^^^^^^^^^^^^^^^ help: try using `ok` instead: `opt.ok()`
|
= note: `-D clippy::result-map-or-into-option` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::result_map_or_into_option)]`
note: the lint level is defined here
--> $DIR/result_map_or_into_option.rs:1:9
|
LL | #![deny(clippy::result_map_or_into_option)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: called `map_or_else(|_| None, Some)` on a `Result` value
--> $DIR/result_map_or_into_option.rs:7:13
|
LL | let _ = opt.map_or_else(|_| None, Some);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using `ok` instead: `opt.ok()`

error: called `map_or_else(|_| None, Some)` on a `Result` value
--> $DIR/result_map_or_into_option.rs:9:13
|
LL | let _ = opt.map_or_else(|_| { None }, Some);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using `ok` instead: `opt.ok()`

error: aborting due to previous error
error: aborting due to 3 previous errors

0 comments on commit 773db59

Please sign in to comment.