rust-lang / rust-lang/rust-clippy

`clippy::clone_on_copy` following recommendation on lock guards can lead to dead locks

Open
#12,957 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

Consider a RwLock on a Clone and/or Copy type.

static VAL: std::sync::RwLock<bool> = std::sync::RwLock::new(false);

Typically, when acquiring a read lock the caller should try to drop the read guard as soon as possible. So on types that are cloneable it's straightforward to call .clone().

When fixing a bunch of lint issues and seeing the calling clone_on_copy lint, the easiest fix is to remove .clone(), which will compile. However, this can result in a deadlock since the previous guard will no longer be dropped without an explicit dereference.

Is it possible for the lint to skip types that are Drop + Copy or Drop + Clone?

Reproducer

No response

Version

No response

Additional Labels

@rustbot l-suggestion-causes-error

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

The issue names the clippy::clone_on_copy lint but provides no source files, tests, or reproducer. Start by locating that lint's implementation and existing tests, then evaluate the lock-guard scenario described in the issue; done means the recommendation no longer permits the reported deadlock risk, with regression coverage for the relevant case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.