Skip to content

Fix false positive of unused_braces with Mutex lock().unwrap() chains #143574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

xizheyin
Copy link
Contributor

@xizheyin xizheyin commented Jul 7, 2025

Fixes #143562

Add side effect detection to prevent linting braces around expressions that contain method call chains like test.lock().unwrap().field, which require braces for proper scoping to avoid deadlocks. The current method is to match the call chain, and I am not sure if a more precise match is needed.

r? compiler

xizheyin added 2 commits July 7, 2025 17:37
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
…ains

Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 7, 2025
match &expr.kind {
// Check for patterns like `test.lock().unwrap().field` that might cause deadlocks
ast::ExprKind::Field(base, _) => {
Self::matches_method_chain(base, vec!["lock", "unwrap"])
Copy link
Contributor

Choose a reason for hiding this comment

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

This misses every other function that could possibly have this problem and theoretically stops linting on functions that don't have this problem.

the root problem is lifetime extension, not locks. Any kind of guard that gets dropped at the end of the main expression unless wrapped in a block expression will want this. I'm not sure how to check this on the HIR, but I expect this has been a problem elsewhere before, likely in match scrutinees, so maybe you can find some logic there that handles this generally.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this is a hack method, I will investigate it later.

I forgot to link the #143562 to this PR.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we could reuse the same logic about significant drops as the tail_expr_drop_order lint: extract_component_with_significant_dtor

Copy link
Member

Choose a reason for hiding this comment

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

In any case the .unwrap() shouldn't have any effect on if this lint triggers or not.

Copy link
Contributor Author

@xizheyin xizheyin Jul 8, 2025

Choose a reason for hiding this comment

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

This Lint appears to require total refactoring. To obtain this information, we need tcx in LateContext, but the current UnusedBraces implementation is EarlyLintPass.

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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

False positive of "unused_braces" when combined with Mutex
4 participants