-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add option_and_then_some lint #4386
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,25 @@ | ||
// run-rustfix | ||
#![deny(clippy::option_and_then_some)] | ||
|
||
// need a main anyway, use it get rid of unused warnings too | ||
pub fn main() { | ||
let x = Some(5); | ||
// the easiest cases | ||
let _ = x; | ||
let _ = x.map(|o| o + 1); | ||
// and an easy counter-example | ||
let _ = x.and_then(|o| if o < 32 { Some(o) } else { None }); | ||
|
||
// Different type | ||
let x: Result<u32, &str> = Ok(1); | ||
let _ = x.and_then(Ok); | ||
} | ||
|
||
pub fn foo() -> Option<String> { | ||
let x = Some(String::from("hello")); | ||
Some("hello".to_owned()).and_then(|s| Some(format!("{}{}", s, x?))) | ||
} | ||
|
||
pub fn example2(x: bool) -> Option<&'static str> { | ||
Some("a").and_then(|s| Some(if x { s } else { return None })) | ||
} |
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,25 @@ | ||
// run-rustfix | ||
#![deny(clippy::option_and_then_some)] | ||
|
||
// need a main anyway, use it get rid of unused warnings too | ||
pub fn main() { | ||
let x = Some(5); | ||
// the easiest cases | ||
let _ = x.and_then(Some); | ||
let _ = x.and_then(|o| Some(o + 1)); | ||
// and an easy counter-example | ||
let _ = x.and_then(|o| if o < 32 { Some(o) } else { None }); | ||
|
||
// Different type | ||
let x: Result<u32, &str> = Ok(1); | ||
let _ = x.and_then(Ok); | ||
} | ||
|
||
pub fn foo() -> Option<String> { | ||
let x = Some(String::from("hello")); | ||
Some("hello".to_owned()).and_then(|s| Some(format!("{}{}", s, x?))) | ||
} | ||
|
||
pub fn example2(x: bool) -> Option<&'static str> { | ||
Some("a").and_then(|s| Some(if x { s } else { return None })) | ||
} |
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,20 @@ | ||
error: using `Option.and_then(Some)`, which is a no-op | ||
--> $DIR/option_and_then_some.rs:8:13 | ||
| | ||
LL | let _ = x.and_then(Some); | ||
| ^^^^^^^^^^^^^^^^ help: use the expression directly: `x` | ||
| | ||
note: lint level defined here | ||
--> $DIR/option_and_then_some.rs:2:9 | ||
| | ||
LL | #![deny(clippy::option_and_then_some)] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: using `Option.and_then(|x| Some(y))`, which is more succinctly expressed as `map(|x| y)` | ||
--> $DIR/option_and_then_some.rs:9:13 | ||
| | ||
LL | let _ = x.and_then(|o| Some(o + 1)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `x.map(|o| o + 1)` | ||
|
||
error: aborting due to 2 previous errors | ||
|
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
// run-rustfix | ||
|
||
#![allow(clippy::option_and_then_some)] | ||
|
||
fn main() { | ||
let opt = 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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
// run-rustfix | ||
|
||
#![allow(clippy::option_and_then_some)] | ||
|
||
fn main() { | ||
let opt = 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't be the lint be applied to
Result
as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should open a different issue and PR.