-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add option_and_then_some lint #4386
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
Conversation
67ac73e to
c7e703f
Compare
|
It seems that this lint would fit in the |
|
Looks like this lint conflicts with tests in rust-clippy/tests/ui/option_map_or_none.rs Lines 6 to 13 in 34457fb
Should I fix those test? |
|
2 step linting is totally fine. So I think adding an |
|
What about linting against |
|
That would also be possible. I don't really know how this lint is implemented, so this is a shot in the dark: My concern with this is, that the lint of What could be done is, to add a special case to |
|
☔ The latest upstream changes (presumably #4348) made this pull request unmergeable. Please resolve the merge conflicts. |
110dd53 to
e21a363
Compare
|
Blocked on #4395 |
9317ed9 to
af15539
Compare
|
Ready for another round of review! 🎉 |
af15539 to
3922094
Compare
|
|
||
| // Different type | ||
| let x: Result<u32, &str> = Ok(1); | ||
| let _ = x.and_then(Ok); |
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.
|
So I have a counter example which we do not want to lint: fn foo() -> Option<String> {
let x = Some(String::from("hello"));
Some("hello".to_owned()).and_then(|s| Some(format!("{}{}", s, x?)))
}How do I check if expression in |
|
You can write a visitor, that walks the expression inside the You can search the Clippy repo on how to implement the Iterator or have a look at the documentation: https://doc.rust-lang.org/nightly/nightly-rustc/rustc/hir/intravisit/trait.Visitor.html |
64bf2f1 to
1893e39
Compare
|
Tests passed locally. |
1893e39 to
8ba4af4
Compare
8ba4af4 to
50ecd59
Compare
|
I made a Visitor like that in #3427. I prefer this one but you should probably check for We should probably make a common function for this in |
2629cd7 to
64634b2
Compare
64634b2 to
41eba2f
Compare
|
Thanks! @bors r+ |
|
📌 Commit 41eba2f has been approved by |
Add option_and_then_some lint changelog: Add complexity lint to warn about `option.and_then(|o| Some(x))` and suggest replacing with `option.map(|o| x)`. Closes #4299
|
☀️ Test successful - checks-travis, status-appveyor |
changelog: Add complexity lint to warn about
option.and_then(|o| Some(x))and suggest replacing withoption.map(|o| x).Closes #4299