-
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
Simplify if let statements #8356
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @camsteffen (or someone else) soon. Please see the contribution instructions for more information. |
if_let
as [
question_mark
]
extension
if_let
as [
question_mark
]
extensionif_let
as `[
question_mark]
` extension
if_let
as `[
question_mark]
` extensionif_let
as [
question_mark
]
extension
if_let
as [
question_mark
]
extensionif_let
as question_mark
extension
7c034ec
to
03b2fdc
Compare
59863c8
to
ea4db3a
Compare
Upon attempting to fix this issue, I found a new problem here, maybe I'm just a little bit confused. After removing this line below if path_to_local_id(peel_blocks(if_then), bind_id); This lint will also triggered by: fn check_option(x: Option<i32>) -> Option<i32> {
do_something();
if let Some(a) = x {
Some(a)
} else {
None
}
} which is already triggering shouldn't a |
@J-ZhengLi Sorry for the delay. I'll take a look at this soon. It looks like some internal lints are failing. Try running |
That is a case where the |
ef4c941
to
5d73a2b
Compare
@camsteffen I hope it's good now, let me know if anything comes up |
if_let
as question_mark
extension
clippy_lints/src/question_mark.rs
Outdated
nested_expr: &Expr<'_>, | ||
pat_symbol: Option<Symbol>, | ||
) -> bool { | ||
Self::is_result(cx, expr) && Self::expression_returns_unmodified_err(cx, nested_expr, expr, pat_symbol) |
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 could use some refactoring to avoid code repetition in a lot of these utils. For example, don't get the expression type in all the is_*
functions. And just look for ExprKind::Ret
in one place rather than having a bunch of *_and_early_return
utils. Maybe you can have a refactor commit before other changes?
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 tried refactoring it, and it still looks like speghatti unfortunatly @camsteffen
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.
Instead of is_early_return(..) || is_early_return(..)
, try doing the following. I think that will make things a lot simpler.
if let Some(adt) = caller_ty.ty_adt_ty();
if let Some(name) = cx.tcx.get_diagnostic_name(adt.did);
if matches!(name, sym::Option | sym::Result);
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.
Thanks for that suggestion, it looks pretty handy. However, don't I still need to separate Option and Result as different cases for return check to prevent things like this from happening:
if let Ok(a) = result {
Some(a)
} else {
None
}
edit: I think I know how to do the refractor now nvm, I forgor what I had in mind
f6ac5a5
to
7d9eb34
Compare
7aed5f0
to
2c98b63
Compare
0ed2eda
to
10f0b21
Compare
☔ The latest upstream changes (presumably #8422) made this pull request unmergeable. Please resolve the merge conflicts. |
10f0b21
to
fb94992
Compare
☔ The latest upstream changes (presumably #8549) made this pull request unmergeable. Please resolve the merge conflicts. |
r? @dswij |
c35130a
to
1462f84
Compare
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.
Apologies that this got sidelined for a while. Thanks for the PR and for being patient!
The change itself looks great to me, just some small comments and I after that I think it's ready to be merged
clippy_lints/src/question_mark.rs
Outdated
_ => false, | ||
} | ||
fn offer_suggestion(cx: &LateContext<'_>, expr: &Expr<'_>, suggestion: String, applicability: Applicability) { | ||
if !suggestion.is_empty() { |
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.
Let's use Option<String>
rather than check for emptiness here.
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 can even remove this check, I don't see a scenario where suggestion
should be empty.
1462f84
to
bc11103
Compare
Thanks so much for this amazing work and refactors! |
Simplify if let statements fixes: #8288 --- changelog: Allowing [`qustion_mark`] lint to check `if let` expressions that immediatly return unwrapped value
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
It looks like bors simply forgot to merge/close this PR. There is a commit for this on master and the issue has been closed. I'll try to ping bors and otherwise close this PR manually. Thank you for the contribution 🙃 @bors ping |
😪 I'm awake I'm awake |
fixes: #8288
changelog: Allowing [
qustion_mark
] lint to checkif let
expressions that immediatly return unwrapped value