Skip to content

Fix suggestions for borrowed fields - #17661

Open
saberoueslati wants to merge 2 commits into
rust-lang:masterfrom
saberoueslati:fix/unnecessary-unwrap-borrowed-return
Open

Fix suggestions for borrowed fields#17661
saberoueslati wants to merge 2 commits into
rust-lang:masterfrom
saberoueslati:fix/unnecessary-unwrap-borrowed-return

Conversation

@saberoueslati

@saberoueslati saberoueslati commented Aug 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #16182.

unnecessary_unwrap suggests borrowing the scrutinee when it fires on a field access checked with is_some()/is_ok() and unwrapped through as_ref()/as_mut():

if let Some(<item>) = &self.option

Evaluating that scrutinee borrows self.option before matching. Under the legacy borrow checker, the borrow can remain live after the if and conflict with a later mutation of self.option, producing E0502.

Current nightly’s default Polonius mode accepts the old form, while -Zpolonius=off reproduces the reported error. The equivalent ref form works with both borrow checkers, so field accesses now get:

if let Some(ref <item>) = self.option

Plain locals retain the existing & and &mut suggestions. The conflicting &mut Option<_> or &mut Result<_, _> parameter shape is not linted because its checked receiver type is a reference rather than the Option or Result ADT.

The UI tests cover shared and mutable Option access, both Result variants, tuple fields, and multi-level field access.

changelog: Fix [unnecessary_unwrap]: use ref bindings when suggesting if let for borrowed fields

@rustbot rustbot added 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 labels Aug 30, 2026
@rustbot

rustbot commented Aug 30, 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.

Comment thread clippy_lints/src/unwrap.rs Outdated
Comment on lines +468 to +472
// For field access, borrowing the scrutinee (`&self.field`) can keep the borrow alive
// after the `if` under legacy borrow checking, conflicting with a later mutation
// (#16182). A `ref` or `ref mut` binding instead
// borrows only the matched field value.
let (binding_prefix, borrow_prefix) = match (as_ref_kind, &unwrappable.local) {

@CommanderStorm CommanderStorm Aug 31, 2026

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.

since there seems to be a different desired behaviour for the lint based on polonius being enabled or not, can we switch on this and provide the best possible lint output?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The suggestion now switches on sess.opts.unstable_opts.polonius. For a field-access scrutinee:

polonius (nightly default) -Zpolonius=off
if let Some(<item>) = &self.option if let Some(ref <item>) = self.option

&self.option evaluates the borrow before the match, so under NLL it can outlive the if and clash with a later mutation (#16182). Polonius doesn't have that limitation. Only the NLL path needs the ref fallback, and because the flag comes off the session, stable gets ref while nightly gets the nicer form. Plain locals are unchanged.

Tests are split the same way. simple_conditionals.rs asserts the & form; a new simple_conditionals_polonius_off.rs carries //@compile-flags: -Zpolonius=off and asserts ref. Both files also have companion methods written the way the lint suggests, so if a suggestion doesn't borrow-check under its own mode, the test file stops compiling.

@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 recieiew: Great work, lgtm

View changes since this review

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.

Clippy suggestion on unnecessary_unwrap not applicable in function taking &mut struct and returning reference to contained element

3 participants