Skip to content

manual_option_as_slice: ignore by-reference bindings - #17560

Open
cuishuang wants to merge 1 commit into
rust-lang:masterfrom
cuishuang:fix-manual-option-as-slice-ref-binding
Open

manual_option_as_slice: ignore by-reference bindings#17560
cuishuang wants to merge 1 commit into
rust-lang:masterfrom
cuishuang:fix-manual-option-as-slice-ref-binding

Conversation

@cuishuang

Copy link
Copy Markdown
Contributor

changelog: [manual_option_as_slice]: avoid incorrect suggestions for by-reference Some bindings

extract_ident_from_some_pat previously ignored the effective binding mode of the inner Some pattern.

For Some(ref value) matched against option.as_ref(), value has an additional reference layer. As a result, slice::from_ref(value) can produce a &[&T], while the suggested option.as_slice() produces a &[T]. Applying the machine-applicable suggestion can therefore cause a type error.

Check the binding mode computed by type checking and only lint by-value bindings. Add regression tests for ref and ref mut bindings.

@rustbot rustbot added the S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. label Aug 14, 2026
@rustbot

rustbot commented Aug 14, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request. A reviewer will take a look after it receives 2 community reviews.

In the meantime, we would highly appreciate if you could try to review any of PRs waiting on community reviews.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 14, 2026
@cuishuang
cuishuang force-pushed the fix-manual-option-as-slice-ref-binding branch from e430fce to 470c5b2 Compare August 16, 2026 00:34
@cuishuang

Copy link
Copy Markdown
Contributor Author

@blyxyas Would you mind taking a look at this when you have a chance? This fixes an incorrect suggestion in manual_option_as_slice. Thanks!

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

Community review:
The actual lint change makes sense to me as far as I can see.

🤔 I don't know if is_empty_slice should be moved above perf wise due to this change though. Have you checked this?

View changes since this review

@cuishuang

Copy link
Copy Markdown
Contributor Author

Community review: The actual lint change makes sense to me as far as I can see.

🤔 I don't know if is_empty_slice should be moved above perf wise due to this change though. Have you checked this?

View changes since this review

Yes, is_empty_slice is already checked before extract_ident_from_some_pat. Since the && chain short-circuits, the new extract_binding_mode call is only made after the None arm is confirmed to be an empty slice, so I think the current ordering is appropriate.

Comment thread clippy_lints/src/manual_option_as_slice.rs Outdated
@CommanderStorm

Copy link
Copy Markdown
Contributor

(make sure to remember to squash+force push your commits 😉)

Comment thread tests/ui/manual_option_as_slice.rs
@cuishuang
cuishuang force-pushed the fix-manual-option-as-slice-ref-binding branch from 7e7591d to 22a1665 Compare August 23, 2026 12:32
@cuishuang

Copy link
Copy Markdown
Contributor Author

could you also add test coverage for the second path to this code (if-let)?

Squshed.

@rustbot

This comment has been minimized.

@cuishuang
cuishuang force-pushed the fix-manual-option-as-slice-ref-binding branch from 22a1665 to 0c3622a Compare August 24, 2026 02:04
@rustbot

rustbot commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@cuishuang

cuishuang commented Aug 24, 2026

Copy link
Copy Markdown
Contributor Author

Resolved the merge conflicts. @CommanderStorm Please review it again.

@CommanderStorm

Copy link
Copy Markdown
Contributor

no need. my review is already an +1.
See #17560 (comment) for our process regarding reviews if you want to also help other people move their PRs move forward 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. 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.

3 participants