Skip to content

Suggest unwrap_or_else when a closure is given #102441

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Oct 4, 2022

Conversation

chenyukang
Copy link
Member

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Sep 28, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 28, 2022
@chenyukang
Copy link
Member Author

May need more work to expand to suggest map_or_else, etc.

@chenyukang chenyukang force-pushed the fix-102320-unwrap_or_else branch from fcc5a23 to 5c3a8e2 Compare September 28, 2022 22:09
@bors
Copy link
Collaborator

bors commented Oct 1, 2022

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

@chenyukang chenyukang force-pushed the fix-102320-unwrap_or_else branch from 5c3a8e2 to aebc5e1 Compare October 2, 2022 06:01
@rust-log-analyzer

This comment has been minimized.

@chenyukang chenyukang force-pushed the fix-102320-unwrap_or_else branch from aebc5e1 to 5d69f3a Compare October 2, 2022 09:26
… to unwrap_or instead of suggesting calling it
@chenyukang chenyukang force-pushed the fix-102320-unwrap_or_else branch from 5d69f3a to f712c41 Compare October 2, 2022 14:22
err: &mut Diagnostic,
expr: &hir::Expr<'_>,
found: Ty<'tcx>,
can_satisfy: impl FnOnce(Ty<'tcx>) -> bool,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to be an argument. Can you inline the can_coerce call here by passing in expected: Ty<'tcx>?

..,
) = call_expr.kind
{
let self_ty = self.typeck_results.borrow().expr_ty(self_expr);
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 there is an expr_ty_opt version of this that doesn't ICE if the node is missing a type If so, then please use that, just in case.

let fn_ty = self.tcx.type_of(pick.item.def_id) &&
let fn_sig = fn_ty.fn_sig(self.tcx) &&
let fn_args = fn_sig.skip_binder().inputs() &&
fn_args.len() == args.len() +1 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn_args.len() == args.len() +1 {
fn_args.len() == args.len() + 1 {

Comment on lines 2366 to 2367
let fn_ty = self.tcx.type_of(pick.item.def_id) &&
let fn_sig = fn_ty.fn_sig(self.tcx) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let fn_ty = self.tcx.type_of(pick.item.def_id) &&
let fn_sig = fn_ty.fn_sig(self.tcx) &&
let fn_sig = self.tcx.fn_sig(pick.item.def_id) &&

@compiler-errors
Copy link
Member

Few more nits

@chenyukang chenyukang force-pushed the fix-102320-unwrap_or_else branch from f712c41 to 0188273 Compare October 3, 2022 06:01
@chenyukang
Copy link
Member Author

Few more nits

Fixed, thanks!

@compiler-errors
Copy link
Member

Hm, this suggestion might have some false positives still, but this is probably ok...

@bors r+

@bors
Copy link
Collaborator

bors commented Oct 4, 2022

📌 Commit 0188273 has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 4, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 4, 2022
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#102441 (Suggest unwrap_or_else when a closure is given)
 - rust-lang#102547 (Migrate CSS theme for search results)
 - rust-lang#102567 (Delay evaluating lint primary message until after it would be suppressed)
 - rust-lang#102624 (rustdoc: remove font family CSS on `.rustdoc-toggle summary::before`)
 - rust-lang#102628 (Change the parameter name of From::from to `value`)
 - rust-lang#102637 (Ignore fuchsia on two compiler tests)
 - rust-lang#102639 (Improve spans when splitting multi-char operator tokens for proc macros.)

Failed merges:

 - rust-lang#102496 (Suggest `.into()` when all other coercion suggestions fail)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit a2126e7 into rust-lang:master Oct 4, 2022
@rustbot rustbot added this to the 1.66.0 milestone Oct 4, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggest unwrap_or_else when a closure is passed to unwrap_or instead of suggesting calling it
6 participants