Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
result_map_or_into_option: add lint to catch manually adpating Result…
… -> Option Result<T, E> has an `ok()` method that adapts a Result<T,E> into an Option<T>. It's possible to get around this adapter by writing Result<T,E>.map_or(None, Some). This lint is implemented as a new variant of the existing [`option_map_none` lint](rust-lang/rust-clippy#2128)
- Loading branch information
1 parent
7907abe
commit 91d8a80
Showing
7 changed files
with
131 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::result_map_or_into_option)] | ||
|
||
fn main() { | ||
let opt: Result<u32, &str> = Ok(1); | ||
let _ = opt.ok(); | ||
|
||
let rewrap = |s: u32| -> Option<u32> { | ||
Some(s) | ||
}; | ||
|
||
// A non-Some `f` arg should not emit the lint | ||
let opt: Result<u32, &str> = Ok(1); | ||
let _ = opt.map_or(None, rewrap); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::result_map_or_into_option)] | ||
|
||
fn main() { | ||
let opt: Result<u32, &str> = Ok(1); | ||
let _ = opt.map_or(None, Some); | ||
|
||
let rewrap = |s: u32| -> Option<u32> { Some(s) }; | ||
|
||
// A non-Some `f` arg should not emit the lint | ||
let opt: Result<u32, &str> = Ok(1); | ||
let _ = opt.map_or(None, rewrap); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
error: called `map_or(None, Some)` on a `Result` value. This can be done more directly by calling `ok()` instead | ||
--> $DIR/result_map_or_into_option.rs:7:13 | ||
| | ||
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` | ||
|
||
error: aborting due to previous error | ||
|