Skip to content

Conversation

@ada4a
Copy link
Contributor

@ada4a ada4a commented Oct 25, 2025

Resolves #15321

Unresolved questions:

  • I did implement support for nested field accesses, but I'm not sure if that's desired

    • It brings some additional complexity, though not too much
    • It might be surprising for the user to get a lint on a direct field access, but not a nested one
    • It's unclear how often such nested case comes up

    EDIT: Decided to keep this after discussion on Zulip

changelog: [unnecessary_unwrap]: lint field accesses
changelog: [panicking_unwrap]: lint field accesses

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 25, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 25, 2025

r? @llogiq

rustbot has assigned @llogiq.
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

Comment on lines +183 to +196
is_potentially_local_place(*local_id, place)
// If there were projections other than field projections, err on the side of caution and say that they
// _might_ be mutating something.
//
// The reason we use `<=` and not `==` is that a mutation of `struct` or `struct.field1` should count as
// mutation of the child fields such as `struct.field1.field2`
&& place.projections.len() <= field_indices.len()
&& iter::zip(&place.projections, field_indices.iter().copied().rev()).all(|(proj, field_idx)| {
match proj.kind {
ProjectionKind::Field(f_idx, _) => f_idx == field_idx,
// If this is a projection we don't expect, it _might_ be mutating something
_ => false,
}
})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This effectively extends is_potentially_local_place with support for locals with field projections -- maybe this change should be incorporated into is_potentially_local_place directly?

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree, it looks like it. However, I'm currently not sure whether this is (or will be) used elsewhere.

@ada4a ada4a force-pushed the unnecessary_unwrap branch from 0550663 to 23ee56d Compare October 25, 2025 11:35
@github-actions
Copy link

github-actions bot commented Oct 25, 2025

Lintcheck changes for 215cfed

Lint Added Removed Changed
clippy::unnecessary_unwrap 6 0 0

This comment will be updated if you push new changes

@ada4a
Copy link
Contributor Author

ada4a commented Oct 25, 2025

Oh hey look, the multi-step-field-access pattern does come up

@ada4a ada4a force-pushed the unnecessary_unwrap branch from 23ee56d to 176748c Compare November 9, 2025 20:03
@rustbot

This comment has been minimized.

@llogiq
Copy link
Contributor

llogiq commented Nov 11, 2025

I think the most interesting question is whether those newly limited cases are worth the additional complexity.

@ada4a
Copy link
Contributor Author

ada4a commented Nov 12, 2025

@llogiq
Copy link
Contributor

llogiq commented Nov 12, 2025

@Alexendoo noted that we'd need to account for partial moves. His example was:

if foo.bar.is_some() {
    f(&foo);
    foo.bar.unwrap();
}

We should have something like this in our tests.

@ada4a ada4a force-pushed the unnecessary_unwrap branch from 176748c to 215cfed Compare November 15, 2025 10:23
@rustbot
Copy link
Collaborator

rustbot commented Nov 15, 2025

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.

@llogiq llogiq added this pull request to the merge queue Nov 15, 2025
Merged via the queue into rust-lang:master with commit c8e5fff Nov 15, 2025
11 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Nov 15, 2025
@ada4a ada4a deleted the unnecessary_unwrap branch November 15, 2025 11:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unnecessary_unwrap false negative of Option variable in struct

3 participants