forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#5415 - nickrtorres:master, r=flip1995
Add new lint for `Result<T, E>.map_or(None, Some(T))` Fixes rust-lang#5414 PR Checklist --- - [x] Followed lint naming conventions (the name is a bit awkward, but it seems to conform) - [x] Added passing UI tests (including committed .stderr file) - [x] cargo test passes locally - [x] Executed cargo dev update_lints - [x] Added lint documentation - [x] Run cargo dev fmt `Result<T, E>` has an [`ok()`](https://doc.rust-lang.org/std/result/enum.Result.html#method.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
Showing
7 changed files
with
142 additions
and
16 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,19 @@ | ||
// 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); | ||
|
||
// A non-Some `f` closure where the argument is not used as the | ||
// return should not emit the lint | ||
let opt: Result<u32, &str> = Ok(1); | ||
opt.map_or(None, |_x| Some(1)); | ||
} |
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,19 @@ | ||
// 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); | ||
|
||
// A non-Some `f` closure where the argument is not used as the | ||
// return should not emit the lint | ||
let opt: Result<u32, &str> = Ok(1); | ||
opt.map_or(None, |_x| Some(1)); | ||
} |
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 | ||
|