Skip to content
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

Closed
wants to merge 3 commits into from

Conversation

J-ZhengLi
Copy link
Member

@J-ZhengLi J-ZhengLi commented Jan 27, 2022

fixes: #8288


changelog: Allowing [qustion_mark] lint to check if let expressions that immediatly return unwrapped value

@rust-highfive
Copy link

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.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 27, 2022
tests/ui/question_mark.rs Outdated Show resolved Hide resolved
tests/ui/question_mark.rs Show resolved Hide resolved
clippy_lints/src/question_mark.rs Outdated Show resolved Hide resolved
@J-ZhengLi J-ZhengLi changed the title attemp to fix #8288 #8288 simple if_let as [question_mark] extension Jan 28, 2022
@J-ZhengLi J-ZhengLi changed the title #8288 simple if_let as [question_mark] extension #8288 simple if_let as `[question_mark]` extension Jan 28, 2022
@J-ZhengLi J-ZhengLi changed the title #8288 simple if_let as `[question_mark]` extension #8288 simple if_let as [question_mark] extension Jan 28, 2022
@J-ZhengLi J-ZhengLi changed the title #8288 simple if_let as [question_mark] extension #8288 simple if_let as question_mark extension Jan 28, 2022
@J-ZhengLi J-ZhengLi marked this pull request as ready for review January 28, 2022 08:23
@J-ZhengLi J-ZhengLi closed this Jan 28, 2022
@J-ZhengLi J-ZhengLi reopened this Jan 29, 2022
@J-ZhengLi J-ZhengLi marked this pull request as draft January 29, 2022 01:12
@J-ZhengLi
Copy link
Member Author

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 [`manual_map`], and offered a suggestion of replacing the if let with x.map(|a| a)

shouldn't a x? be more intuitive in this case?

@camsteffen
Copy link
Contributor

@J-ZhengLi Sorry for the delay. I'll take a look at this soon. It looks like some internal lints are failing. Try running cargo test --features internal.

@camsteffen
Copy link
Contributor

shouldn't a x? be more intuitive in this case?

That is a case where the match doesn't do anything. There is a suggested lint for that at #7040. For now, I would just let it be linted by manual_map only.

@J-ZhengLi J-ZhengLi force-pushed the master-issue8288 branch 3 times, most recently from ef4c941 to 5d73a2b Compare February 10, 2022 10:02
@J-ZhengLi J-ZhengLi marked this pull request as ready for review February 10, 2022 10:14
@J-ZhengLi
Copy link
Member Author

J-ZhengLi commented Feb 14, 2022

@camsteffen I hope it's good now, let me know if anything comes up

@J-ZhengLi J-ZhengLi changed the title #8288 simple if_let as question_mark extension Simplify if let statements Feb 16, 2022
tests/ui/question_mark.rs Outdated Show resolved Hide resolved
clippy_lints/src/question_mark.rs Outdated Show resolved Hide resolved
clippy_lints/src/question_mark.rs Show resolved Hide resolved
clippy_lints/src/question_mark.rs Outdated Show resolved Hide resolved
clippy_lints/src/question_mark.rs Outdated Show resolved Hide resolved
nested_expr: &Expr<'_>,
pat_symbol: Option<Symbol>,
) -> bool {
Self::is_result(cx, expr) && Self::expression_returns_unmodified_err(cx, nested_expr, expr, pat_symbol)
Copy link
Contributor

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?

Copy link
Member Author

@J-ZhengLi J-ZhengLi Feb 24, 2022

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

Copy link
Contributor

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);

Copy link
Member Author

@J-ZhengLi J-ZhengLi Feb 26, 2022

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

@J-ZhengLi J-ZhengLi marked this pull request as draft February 21, 2022 12:29
@J-ZhengLi J-ZhengLi force-pushed the master-issue8288 branch 2 times, most recently from 7aed5f0 to 2c98b63 Compare February 21, 2022 13:11
@J-ZhengLi J-ZhengLi marked this pull request as ready for review February 23, 2022 04:19
@bors
Copy link
Collaborator

bors commented Mar 13, 2022

☔ The latest upstream changes (presumably #8422) made this pull request unmergeable. Please resolve the merge conflicts.

@bors
Copy link
Collaborator

bors commented Apr 6, 2022

☔ The latest upstream changes (presumably #8549) made this pull request unmergeable. Please resolve the merge conflicts.

@dswij
Copy link
Member

dswij commented Jun 12, 2022

r? @dswij

@rust-highfive rust-highfive assigned dswij and unassigned camsteffen Jun 12, 2022
@J-ZhengLi J-ZhengLi force-pushed the master-issue8288 branch 2 times, most recently from c35130a to 1462f84 Compare June 13, 2022 08:43
Copy link
Member

@dswij dswij left a 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

_ => false,
}
fn offer_suggestion(cx: &LateContext<'_>, expr: &Expr<'_>, suggestion: String, applicability: Applicability) {
if !suggestion.is_empty() {
Copy link
Member

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.

Copy link
Member

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.

clippy_lints/src/question_mark.rs Outdated Show resolved Hide resolved
@dswij
Copy link
Member

dswij commented Jul 8, 2022

Thanks so much for this amazing work and refactors!
@bors r+ squash

@bors
Copy link
Collaborator

bors commented Jul 8, 2022

📌 Commit 76a0553 has been approved by dswij

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Jul 8, 2022

⌛ Testing commit 76a0553 with merge 49f73e1...

bors added a commit that referenced this pull request Jul 8, 2022
Simplify if let statements

fixes: #8288

---

changelog: Allowing [`qustion_mark`] lint to check `if let` expressions that immediatly return unwrapped value
@bors
Copy link
Collaborator

bors commented Jul 8, 2022

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: dswij
Pushing 49f73e1 to master...

@xFrednet
Copy link
Member

xFrednet commented Jul 8, 2022

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

@bors
Copy link
Collaborator

bors commented Jul 8, 2022

😪 I'm awake I'm awake

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Simplify if let statements that immediately return unwrapped value
6 participants