Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created June 2, 2023 21:46
Show Gist options
  • Save rust-play/2d80187d376090c9ed59960cd485431b to your computer and use it in GitHub Desktop.
Save rust-play/2d80187d376090c9ed59960cd485431b to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::sync::{atomic::AtomicU32, atomic::Ordering, Arc, Mutex, RwLock};
// Reproduction derived from indicatif::MultiProgress and ProgressBar where this Clippy suggestion was found.
// With Clippy 0.1.71 (2023-06-01 d59363a)
// With Rust 1.70.0
#[derive(Clone)]
struct MultiProgress {
state: Arc<RwLock<State>>,
}
impl Default for MultiProgress {
fn default() -> Self {
Self {
state: Arc::new(RwLock::new(State)),
}
}
}
impl MultiProgress {
fn add(&self, pb: ProgressBar) -> ProgressBar {
let state = self.state.write().unwrap();
drop(state);
pb.set_remote(self.state.clone());
pb
}
}
#[derive(Clone)]
struct ProgressBar {
value: Arc<AtomicU32>,
remote: Arc<Mutex<Arc<RwLock<State>>>>,
}
impl Default for ProgressBar {
fn default() -> Self {
Self {
value: Arc::new(AtomicU32::new(0)),
remote: Arc::new(Mutex::new(Arc::new(RwLock::new(State)))),
}
}
}
impl ProgressBar {
fn set_remote(&self, state: Arc<RwLock<State>>) {
*self.remote.lock().unwrap() = state;
}
fn inc(&self, delta: u32) {
self.value.fetch_add(delta, Ordering::SeqCst);
}
fn reset(&self) {
self.value.fetch_add(0, Ordering::SeqCst);
}
}
struct State;
fn main() {
let pb = {
let m = MultiProgress::default();
m.add(ProgressBar::default())
// m is dropped here
};
{
// Clippy faults here: it suggests to remove the clone.
let pb2 = pb.clone();
for _ in 0..10 {
pb2.inc(1);
}
}
pb.reset();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment