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

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?

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

Lintcheck changes for 23ee56d

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unnecessary_unwrap false negative of Option variable in struct

3 participants