rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.3k stars 1.52k forks source link

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

Open juliusl opened 3 months ago

juliusl commented 3 months ago

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

rustbot commented 3 months ago

Error: Parsing relabel command in comment failed: ...' label +' | error: empty label at >| ' `l-sugges'...

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

Alexendoo commented 3 months ago

The suggestion here seems correct https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=79443d91515a74b5e3e6083941febfc9

help: try dereferencing it: `*VAL.write().unwrap()`