rust-lang / rust-lang/rust-clippy

Add a lint for unbound mutex guards

Open
#13,869 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

When working with various synchronization primitives, such as Mutex and RwLock, it is easy to make the mistake of not binding the corresponding guard to the current scope. It would be helpful to assist.

Advantage

No response

Drawbacks

No response

Example
struct Worker {
    mutex: std::sync::Arc<std::sync::Mutex<()>>,
}

impl Worker {
    fn work(&self) {
        let _ = self.mutex.lock().unwrap();
        // Do some work with an external resource.
    }
}

struct AsyncWorker {
    mutex: std::sync::Arc<tokio::sync::Mutex<()>>,
}

impl AsyncWorker {
    async fn work(&self) {
        let _ = self.mutex.lock().await;
        // Do some work with an external resource.
    }
}

Could be written as:

struct Worker {
    mutex: std::sync::Arc<std::sync::Mutex<()>>,
}

impl Worker {
    fn work(&self) {
        let _guard = self.mutex.lock().unwrap();
        // Do some work with an external resource.
    }
}

struct AsyncWorker {
    mutex: std::sync::Arc<tokio::sync::Mutex<()>>,
}

impl AsyncWorker {
    async fn work(&self) {
        let _guard = self.mutex.lock().await;
        // Do some work with an external resource.
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the Mutex and RwLock examples in the issue and compare them with existing Clippy lint patterns. Define which discarded guard expressions and synchronization primitives should be covered, then add coverage for the supported cases and verify that intentional temporary locks are handled appropriately. Done means the lint reliably flags unbound guards without introducing false positives.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.