Skip to content

Avoid wrong unwrap suggestions for never option value - #162276

Open
chenyukang wants to merge 3 commits into
rust-lang:mainfrom
chenyukang:yukang-fix-162241-never-option-expect-suggestion
Open

Avoid wrong unwrap suggestions for never option value#162276
chenyukang wants to merge 3 commits into
rust-lang:mainfrom
chenyukang:yukang-fix-162241-never-option-expect-suggestion

Conversation

@chenyukang

@chenyukang chenyukang commented Sep 4, 2026

Copy link
Copy Markdown
Member

Fixes #162241

@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 Sep 4, 2026
@rustbot

rustbot commented Sep 4, 2026

Copy link
Copy Markdown
Collaborator

r? @tiif

rustbot has assigned @tiif.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 20 candidates

@hanna-kruppe hanna-kruppe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Mostly LGTM, but I'm not sure it makes sense to target only one of the two possible suggestions that this code can emit.

r? me @rustbot author

View changes since this review

Comment on lines 2378 to 2379
"use the `?` operator to extract the `{found}` value, propagating \
{article} `{kind}::{variant}` value to the caller"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does this suggestion make sense when inner_ty is ! or should we suppress the suggestion regardless of sugg_operator? If x: Option<!> then x? must be an unconditional early return, which seems dubious and certainly doesn't contribute to actually getting a value of the expected type.

(If we should suppress this suggestion to, then the control flow could also be a little simpler.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, I added a test and updated the code to remove the invalid suggestion.
This makes me thinking it's a more general issue because a lot of suggestion code are using may_coerce here https://github.com/rust-lang/rust/pull/162276/changes#diff-a188fa3dba59dd225d4a65e08961d2937f6418bc6126716e001303bf946d6ee3L2370, and it may not right for never type in suggestion scenario.

@chenyukang chenyukang Sep 5, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

added may_coerce_for_suggestion for more scenarios 2ec5549#r3939584890

@rustbot rustbot assigned hanna-kruppe and unassigned tiif Sep 4, 2026
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 4, 2026
@chenyukang
chenyukang force-pushed the yukang-fix-162241-never-option-expect-suggestion branch from cc277a8 to 34a7b47 Compare September 4, 2026 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

Trying to assign Option<!> to Option<u32> recommends calling .expect()

4 participants