Skip to content

fix(mut_mutex_lock): don't lint on indexing and field accesses#16435

Open
ada4a wants to merge 4 commits into
rust-lang:masterfrom
ada4a:push-lwmnouxulwsy
Open

fix(mut_mutex_lock): don't lint on indexing and field accesses#16435
ada4a wants to merge 4 commits into
rust-lang:masterfrom
ada4a:push-lwmnouxulwsy

Conversation

@ada4a

@ada4a ada4a commented Jan 21, 2026

Copy link
Copy Markdown
Contributor

A v2 of #16261, due to #16261 (comment)

Fixes #16253

changelog: [mut_mutex_lock]: don't lint on indexing and field accesses

r? @Jarcho

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 21, 2026
@Jarcho

Jarcho commented Jan 22, 2026

Copy link
Copy Markdown
Contributor

Like in the other PR you are still going to need to find the projection base (i.e. walk deref calls) and check that all derefs can be switched to their mutable form. A "known problems" section needs to be added to the lint to explain how the lint doesn't take lifetimes into account as well.

@rustbot

This comment has been minimized.

@ada4a
ada4a force-pushed the push-lwmnouxulwsy branch from c457fb7 to 45dc752 Compare June 8, 2026 22:43
@ada4a
ada4a force-pushed the push-lwmnouxulwsy branch from 45dc752 to 65d0ab6 Compare July 11, 2026 13:47
@rustbot

This comment has been minimized.

@ada4a
ada4a force-pushed the push-lwmnouxulwsy branch from 65d0ab6 to d047b47 Compare July 11, 2026 13:56
@ada4a
ada4a requested a review from Jarcho July 11, 2026 13:57
//
// 1. Check that there are no custom deref adjustments turning the access immutable (e.g.
// `Arc<Mutex>` derefs to `&Mutex`)
if matches!(expr_custom_deref_adjustment(cx, recv), None | Some(Mutability::Mut))

@Jarcho Jarcho Jul 13, 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.

Why is this happening? Calling lock through a custom deref will never be mutable. The check you do after also makes this completely redundant.

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.

Apologies, this was left over from a previous version of the lint, as I honestly wasn't sure if it's still relevant. Removed it now

Comment on lines +84 to +93
&& cx
.qpath_res(p, r.hir_id)
.opt_def_id()
.and_then(|id| cx.tcx.static_mutability(id))
== Some(Mutability::Not)
{
// The mutex is stored in a `static`, and we don't want to suggest making that
// mutable
return;
}

@Jarcho Jarcho Jul 13, 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.

We don't want to lint any statics, mutable or not.

View changes since the review

// mutable one -- we need to check if a mutable deref would've been possible, i.e. if
// `ty: DerefMut<Target = target>` (we don't need to check the `Target` part, as `Deref` and
// `DerefMut` impls necessarily have the same one)
DerefAdjustKind::Overloaded(_) => impls_deref_mut(ty).then_some(target),

@Jarcho Jarcho Jul 13, 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.

Why allow custom derefs here, but not earlier.

View changes since the review

// Peel off one projection
match r.kind {
ExprKind::Unary(UnOp::Deref, base) => {
if impls_deref_mut(typeck.expr_ty_adjusted(base)) {

@Jarcho Jarcho Jul 13, 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.

Why allow custom derefs here, but not earlier.

View changes since the review

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 13, 2026
@rustbot

rustbot commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Jul 13, 2026
@rustbot

This comment has been minimized.

@ada4a
ada4a force-pushed the push-lwmnouxulwsy branch from d047b47 to f244b83 Compare July 24, 2026 14:31
@rustbot

rustbot commented Jul 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.

@ada4a

ada4a commented Jul 24, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the review comments in third commit. Also, extracted projection checking into a separate function, as that makes the lint structure a bit clearer imo -- that's a separate commit as well, so it can be easily removed if you disagree.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Jul 24, 2026
@ada4a
ada4a force-pushed the push-lwmnouxulwsy branch from f244b83 to a38440b Compare July 24, 2026 14:40
ada4a added 2 commits July 24, 2026 16:44
- remove the first adjustment check, as it's redundant
- don't lint on _any_ statics
makes the structure of the lint a bit clearer imo
@ada4a
ada4a force-pushed the push-lwmnouxulwsy branch from a38440b to 86e5428 Compare July 24, 2026 14:45
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.

mut_mutex_lock: FP on struct.mutex where struct is behind &

3 participants