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

Add ignore value suggestion in closure body #135562

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

chenyukang
Copy link
Member

Fixes #128561

r? @estebank

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 16, 2025
@chenyukang
Copy link
Member Author

dbg! is a macro, which may not make a machine-applicable suggestion, #81943 (comment)

I choose to make a Applicability::MaybeIncorrect.

Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

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

I would be concerned with this suggestion triggering for something like map, where should be suggesting removing the call entirely instead of ignoring the result. for for_each it is trickier because it is called for its side-effects, but again if the closure doesn't have any... I'm conflicted, not with the implementation but with the possibility of misleading users accidentally.

r=me after addressing the wording nitpick if I don't come up with actual reasons not to do this before this week is through.

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs Outdated Show resolved Hide resolved
compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs Outdated Show resolved Hide resolved
Comment on lines 1 to 8
error[E0308]: mismatched types
--> $DIR/closure-ty-mismatch-issue-128561.rs:2:32
|
LL | b"abc".iter().for_each(|x| x);
| ^
| |
| expected `()`, found `&u8`
| help: consider ignore the value here: `_ =`
Copy link
Contributor

Choose a reason for hiding this comment

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

I could have sworn that this || _ = x syntax didn't work 👀

Comment on lines +10 to +16
error[E0308]: mismatched types
--> $DIR/closure-ty-mismatch-issue-128561.rs:4:32
|
LL | b"abc".iter().for_each(|x| dbg!(x));
| ^^^^^^^ expected `()`, found `&u8`
|
= note: this error originates in the macro `dbg` (in Nightly builds, run with -Z macro-backtrace for more info)
Copy link
Contributor

Choose a reason for hiding this comment

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

I notice that the suggestion didn't trigger for the macro case, which is what the original report wanted. I think that in order to sidestep the issues of the macro not being visible in the HIR, to get the span by looking at the closure's tail expression (instead of expression, get the parent node and get the span from there).

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

There are two options that might or might not work: from the expression, climb up its parents until you get a node with a span with root context (hence, not pointing at a macro). That runs the risk of giving you the node of an expression still surrounded by a macro, which isn't great. I think this suggestion is only relevant for the tail expression of closures, right? The second option would be to get the parent closure by looping on getting the parent until you hit one (with get_closure_node, as you point out), and then look down from there. A macro that uses closures internally can still mess your suggestion up though, so you likely still need a check for "what's this span's context", regardless of what you do :-/


let parent_id = fcx.tcx.parent_hir_id(block_or_return_id);
let parent = fcx.tcx.hir_node(parent_id);
let parent = fcx.tcx.parent_hir_node(block_or_return_id);
Copy link
Contributor

Choose a reason for hiding this comment

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

If you change this for get_closure_node(block_or_return_id)...

Comment on lines 1894 to 1897
&& let hir::Node::Expr(hir::Expr {
kind: hir::ExprKind::Closure(&hir::Closure { body, .. }),
..
}) = parent
Copy link
Contributor

Choose a reason for hiding this comment

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

...and change the pattern here to surround it with Some. What do you get? It might be interesting to change the last argument to pass in an option of the closure tail expr, instead of a boolean, so that the suggestion can use that.

It is a shame how much macros in general complicate this kind of logic :(

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. 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.

E0308 hint suggests adding a return type to a closure
3 participants