From b3dd8e8a0245ca19c2af588f653a6e67c8e84c5e Mon Sep 17 00:00:00 2001 From: Ben Wallis Date: Sun, 27 Sep 2020 17:20:40 +0100 Subject: [PATCH] Added #![deny(clippy::clone_on_ref_ptr)] to all crates and fixed resulting lint errors --- chat-cli/src/main.rs | 1 + client/src/lib.rs | 1 + common/src/lib.rs | 3 ++- common/src/state.rs | 3 ++- common/src/volumes/vol_grid_2d.rs | 2 +- network/src/api.rs | 10 +++++----- network/src/lib.rs | 1 + network/src/participant.rs | 6 +++--- network/src/scheduler.rs | 24 ++++++++++++------------ server-cli/src/main.rs | 1 + server-cli/src/tui_runner.rs | 2 +- server/src/chunk_generator.rs | 2 +- server/src/lib.rs | 5 +++-- server/src/metrics.rs | 4 ++-- tools/src/main.rs | 2 ++ voxygen/src/audio/soundcache.rs | 2 +- voxygen/src/hud/mod.rs | 4 +++- voxygen/src/lib.rs | 4 +++- voxygen/src/menu/char_selection/mod.rs | 2 +- voxygen/src/session.rs | 6 +++--- voxygen/src/singleplayer.rs | 2 +- world/src/lib.rs | 1 + 22 files changed, 51 insertions(+), 37 deletions(-) diff --git a/chat-cli/src/main.rs b/chat-cli/src/main.rs index f833d5ce66..4306d18d65 100644 --- a/chat-cli/src/main.rs +++ b/chat-cli/src/main.rs @@ -1,5 +1,6 @@ #![deny(unsafe_code)] #![allow(clippy::option_map_unit_fn)] +#![deny(clippy::clone_on_ref_ptr)] use client::{Client, Event}; use common::{clock::Clock, comp}; diff --git a/client/src/lib.rs b/client/src/lib.rs index e74ed9bd9d..4103f5507b 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -1,4 +1,5 @@ #![deny(unsafe_code)] +#![deny(clippy::clone_on_ref_ptr)] #![feature(label_break_value, option_zip)] pub mod cmd; diff --git a/common/src/lib.rs b/common/src/lib.rs index a3899797bf..327d8268cb 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -1,7 +1,8 @@ #![deny(unsafe_code)] -#![allow(clippy::option_map_unit_fn)] #![allow(incomplete_features)] #![type_length_limit = "1664759"] +#![allow(clippy::option_map_unit_fn)] +#![deny(clippy::clone_on_ref_ptr)] #![feature( arbitrary_enum_discriminant, associated_type_defaults, diff --git a/common/src/state.rs b/common/src/state.rs index 9d871cae15..8b3af40967 100644 --- a/common/src/state.rs +++ b/common/src/state.rs @@ -371,7 +371,8 @@ impl State { // Run systems to update the world. // Create and run a dispatcher for ecs systems. - let mut dispatch_builder = DispatcherBuilder::new().with_pool(self.thread_pool.clone()); + let mut dispatch_builder = + DispatcherBuilder::new().with_pool(Arc::clone(&self.thread_pool)); sys::add_local_systems(&mut dispatch_builder); // TODO: Consider alternative ways to do this add_foreign_systems(&mut dispatch_builder); diff --git a/common/src/volumes/vol_grid_2d.rs b/common/src/volumes/vol_grid_2d.rs index 4808c94942..4432fc80a5 100644 --- a/common/src/volumes/vol_grid_2d.rs +++ b/common/src/volumes/vol_grid_2d.rs @@ -189,7 +189,7 @@ impl<'a, V: RectRasterableVol + ReadVol> CachedVolGrid2d<'a, V> { .get(&ck) .ok_or(VolGrid2dError::NoSuchChunk)?; // Store most recently looked up chunk in the cache - self.cache = Some((ck, chunk.clone())); + self.cache = Some((ck, Arc::clone(&chunk))); chunk }; let co = VolGrid2d::::chunk_offs(pos); diff --git a/network/src/api.rs b/network/src/api.rs index c77003a9a6..af171b7e1b 100644 --- a/network/src/api.rs +++ b/network/src/api.rs @@ -375,7 +375,7 @@ impl Network { self.participant_disconnect_sender .lock() .await - .insert(pid, participant.a2s_disconnect_s.clone()); + .insert(pid, Arc::clone(&participant.a2s_disconnect_s)); Ok(participant) } @@ -414,10 +414,10 @@ impl Network { /// [`listen`]: crate::api::Network::listen pub async fn connected(&self) -> Result { let participant = self.connected_receiver.lock().await.next().await?; - self.participant_disconnect_sender - .lock() - .await - .insert(participant.remote_pid, participant.a2s_disconnect_s.clone()); + self.participant_disconnect_sender.lock().await.insert( + participant.remote_pid, + Arc::clone(&participant.a2s_disconnect_s), + ); Ok(participant) } } diff --git a/network/src/lib.rs b/network/src/lib.rs index f6fcc4ecf2..611c4e6d54 100644 --- a/network/src/lib.rs +++ b/network/src/lib.rs @@ -1,6 +1,7 @@ #![deny(unsafe_code)] #![cfg_attr(test, deny(rust_2018_idioms))] #![cfg_attr(test, deny(warnings))] +#![deny(clippy::clone_on_ref_ptr)] #![feature(try_trait)] //! Crate to handle high level networking of messages with different diff --git a/network/src/participant.rs b/network/src/participant.rs index fd6748ec7e..a9af90834b 100644 --- a/network/src/participant.rs +++ b/network/src/participant.rs @@ -147,7 +147,7 @@ impl BParticipant { let (w2b_frames_s, w2b_frames_r) = mpsc::unbounded::(); let (prios, a2p_msg_s, b2p_notify_empty_stream_s) = PrioManager::new( #[cfg(feature = "metrics")] - self.metrics.clone(), + Arc::clone(&self.metrics), self.remote_pid_string.clone(), ); @@ -486,7 +486,7 @@ impl BParticipant { // This channel is now configured, and we are running it in scope of the // participant. let w2b_frames_s = w2b_frames_s.clone(); - let channels = self.channels.clone(); + let channels = Arc::clone(&self.channels); async move { let (channel, b2w_frame_s, b2r_read_shutdown) = Channel::new(cid); let mut lock = channels.write().await; @@ -772,7 +772,7 @@ impl BParticipant { self.streams.write().await.insert(sid, StreamInfo { prio, promises, - send_closed: send_closed.clone(), + send_closed: Arc::clone(&send_closed), b2a_msg_recv_s: Mutex::new(b2a_msg_recv_s), }); #[cfg(feature = "metrics")] diff --git a/network/src/scheduler.rs b/network/src/scheduler.rs index e4fb5a3b7e..5f3d418b3d 100644 --- a/network/src/scheduler.rs +++ b/network/src/scheduler.rs @@ -216,7 +216,7 @@ impl Scheduler { Protocols::Tcp(TcpProtocol::new( stream, #[cfg(feature = "metrics")] - self.metrics.clone(), + Arc::clone(&self.metrics), )), false, ) @@ -241,14 +241,14 @@ impl Scheduler { info!("Connecting Udp to: {}", addr); let (udp_data_sender, udp_data_receiver) = mpsc::unbounded::>(); let protocol = UdpProtocol::new( - socket.clone(), + Arc::clone(&socket), addr, #[cfg(feature = "metrics")] - self.metrics.clone(), + Arc::clone(&self.metrics), udp_data_receiver, ); self.pool.spawn_ok( - Self::udp_single_channel_connect(socket.clone(), udp_data_sender) + Self::udp_single_channel_connect(Arc::clone(&socket), udp_data_sender) .instrument(tracing::info_span!("udp", ?addr)), ); (Protocols::Udp(protocol), true) @@ -396,7 +396,7 @@ impl Scheduler { let protocol = TcpProtocol::new( stream, #[cfg(feature = "metrics")] - self.metrics.clone(), + Arc::clone(&self.metrics), ); self.init_protocol(Protocols::Tcp(protocol), None, true) .await; @@ -439,10 +439,10 @@ impl Scheduler { let (udp_data_sender, udp_data_receiver) = mpsc::unbounded::>(); listeners.insert(remote_addr, udp_data_sender); let protocol = UdpProtocol::new( - socket.clone(), + Arc::clone(&socket), remote_addr, #[cfg(feature = "metrics")] - self.metrics.clone(), + Arc::clone(&self.metrics), udp_data_receiver, ); self.init_protocol(Protocols::Udp(protocol), None, false) @@ -499,10 +499,10 @@ impl Scheduler { // the whole server easily for new clients UDP doesnt work at all, as // the UDP listening is done in another place. let cid = self.channel_ids.fetch_add(1, Ordering::Relaxed); - let participants = self.participants.clone(); + let participants = Arc::clone(&self.participants); #[cfg(feature = "metrics")] - let metrics = self.metrics.clone(); - let pool = self.pool.clone(); + let metrics = Arc::clone(&self.metrics); + let pool = Arc::clone(&self.pool); let local_pid = self.local_pid; let local_secret = self.local_secret; // this is necessary for UDP to work at all and to remove code duplication @@ -514,7 +514,7 @@ impl Scheduler { local_pid, local_secret, #[cfg(feature = "metrics")] - metrics.clone(), + Arc::clone(&metrics), send_handshake, ); match handshake @@ -541,7 +541,7 @@ impl Scheduler { pid, sid, #[cfg(feature = "metrics")] - metrics.clone(), + Arc::clone(&metrics), ); let participant = Participant::new( diff --git a/server-cli/src/main.rs b/server-cli/src/main.rs index bae9cc6a50..801ac8566d 100644 --- a/server-cli/src/main.rs +++ b/server-cli/src/main.rs @@ -1,4 +1,5 @@ #![deny(unsafe_code)] +#![deny(clippy::clone_on_ref_ptr)] mod tui_runner; mod tuilog; diff --git a/server-cli/src/tui_runner.rs b/server-cli/src/tui_runner.rs index 63f9cfd7dc..8a30367087 100644 --- a/server-cli/src/tui_runner.rs +++ b/server-cli/src/tui_runner.rs @@ -111,7 +111,7 @@ impl Tui { })); let mut msg_s = self.msg_s.take().unwrap(); - let running = self.running.clone(); + let running = Arc::clone(&self.running); if basic { std::thread::spawn(move || { diff --git a/server/src/chunk_generator.rs b/server/src/chunk_generator.rs index 83de4c879e..324f20042c 100644 --- a/server/src/chunk_generator.rs +++ b/server/src/chunk_generator.rs @@ -85,7 +85,7 @@ impl ChunkGenerator { } pub fn cancel_all(&mut self) { - let metrics = self.metrics.clone(); + let metrics = Arc::clone(&self.metrics); self.pending_chunks.drain().for_each(|(_, cancel)| { cancel.store(true, Ordering::Relaxed); metrics.chunks_canceled.inc(); diff --git a/server/src/lib.rs b/server/src/lib.rs index aeb887e6eb..0b0fa44c27 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -1,5 +1,6 @@ #![deny(unsafe_code)] #![allow(clippy::option_map_unit_fn)] +#![deny(clippy::clone_on_ref_ptr)] #![feature(bool_to_option, drain_filter, option_zip)] #![cfg_attr(not(feature = "worldgen"), feature(const_panic))] @@ -548,7 +549,7 @@ impl Server { None, pos, thread_pool, - world.clone(), + Arc::clone(&world), index.clone(), ); }); @@ -850,7 +851,7 @@ impl Server { Some(entity), key, &mut self.thread_pool, - self.world.clone(), + Arc::clone(&self.world), self.index.clone(), ); } diff --git a/server/src/metrics.rs b/server/src/metrics.rs index 91463251d4..c78639f609 100644 --- a/server/src/metrics.rs +++ b/server/src/metrics.rs @@ -329,7 +329,7 @@ impl ServerMetrics { pub fn run(&mut self, addr: SocketAddr) -> Result<(), Box> { self.running.store(true, Ordering::Relaxed); - let running2 = self.running.clone(); + let running2 = Arc::clone(&self.running); let registry = self .registry @@ -373,7 +373,7 @@ impl ServerMetrics { pub fn tick(&self) -> u64 { self.tick.fetch_add(1, Ordering::Relaxed) + 1 } - pub fn tick_clone(&self) -> Arc { self.tick.clone() } + pub fn tick_clone(&self) -> Arc { Arc::clone(&self.tick) } } impl Drop for ServerMetrics { diff --git a/tools/src/main.rs b/tools/src/main.rs index c5b2455f97..5ab27d8080 100644 --- a/tools/src/main.rs +++ b/tools/src/main.rs @@ -1,3 +1,5 @@ +#![deny(clippy::clone_on_ref_ptr)] + use std::error::Error; use structopt::StructOpt; diff --git a/voxygen/src/audio/soundcache.rs b/voxygen/src/audio/soundcache.rs index 61488e3e56..ea92f3e6d3 100644 --- a/voxygen/src/audio/soundcache.rs +++ b/voxygen/src/audio/soundcache.rs @@ -22,7 +22,7 @@ impl Sound { Ok(Sound(Arc::new(buf))) } - pub fn cursor(&self) -> io::Cursor { io::Cursor::new(Sound(self.0.clone())) } + pub fn cursor(&self) -> io::Cursor { io::Cursor::new(Sound(Arc::clone(&self.0))) } pub fn decoder(&self) -> rodio::Decoder> { rodio::Decoder::new(self.cursor()).unwrap() diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index e34d113292..69aa2eefe2 100644 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -65,9 +65,11 @@ use conrod_core::{ use specs::{Join, WorldExt}; use std::{ collections::{HashMap, VecDeque}, + sync::Arc, time::Instant, }; use vek::*; + const XP_COLOR: Color = Color::Rgba(0.59, 0.41, 0.67, 1.0); const TEXT_COLOR: Color = Color::Rgba(1.0, 1.0, 1.0, 1.0); const TEXT_GRAY_COLOR: Color = Color::Rgba(0.5, 0.5, 0.5, 1.0); @@ -591,7 +593,7 @@ impl Hud { // Load world map let world_map = ( ui.add_graphic_with_rotations(Graphic::Image( - client.world_map.0.clone(), + Arc::clone(&client.world_map.0), Some(water_color), )), client.world_map.1.map(u32::from), diff --git a/voxygen/src/lib.rs b/voxygen/src/lib.rs index ff26ee3f13..bb9acf6d99 100644 --- a/voxygen/src/lib.rs +++ b/voxygen/src/lib.rs @@ -1,5 +1,7 @@ #![deny(unsafe_code)] -#![allow(clippy::option_map_unit_fn, incomplete_features)] +#![allow(incomplete_features)] +#![allow(clippy::option_map_unit_fn)] +#![deny(clippy::clone_on_ref_ptr)] #![feature(array_map, bool_to_option, const_generics, drain_filter, or_patterns)] #![recursion_limit = "2048"] diff --git a/voxygen/src/menu/char_selection/mod.rs b/voxygen/src/menu/char_selection/mod.rs index 77179db056..1b9d85bf1f 100644 --- a/voxygen/src/menu/char_selection/mod.rs +++ b/voxygen/src/menu/char_selection/mod.rs @@ -111,7 +111,7 @@ impl PlayState for CharSelectionState { return PlayStateResult::Switch(Box::new(SessionState::new( global_state, - self.client.clone(), + Rc::clone(&self.client), ))); }, } diff --git a/voxygen/src/session.rs b/voxygen/src/session.rs index 29fa7ff3b6..23c3bc3c82 100644 --- a/voxygen/src/session.rs +++ b/voxygen/src/session.rs @@ -28,7 +28,7 @@ use common::{ vol::ReadVol, }; use specs::{Join, WorldExt}; -use std::{cell::RefCell, rc::Rc, time::Duration}; +use std::{cell::RefCell, rc::Rc, sync::Arc, time::Duration}; use tracing::{error, info}; use vek::*; @@ -981,7 +981,7 @@ impl PlayState for SessionState { ) .unwrap(); self.voxygen_i18n.log_missing_entries(); - self.hud.update_language(self.voxygen_i18n.clone()); + self.hud.update_language(Arc::clone(&self.voxygen_i18n)); }, HudEvent::ChangeFullscreenMode(new_fullscreen_settings) => { global_state @@ -1086,7 +1086,7 @@ impl PlayState for SessionState { } else if let ClientState::Registered = client_state { PlayStateResult::Switch(Box::new(CharSelectionState::new( global_state, - self.client.clone(), + Rc::clone(&self.client), ))) } else { error!("Client not in the expected state, exiting session play state"); diff --git a/voxygen/src/singleplayer.rs b/voxygen/src/singleplayer.rs index c9ad23334f..a9892cf594 100644 --- a/voxygen/src/singleplayer.rs +++ b/voxygen/src/singleplayer.rs @@ -46,7 +46,7 @@ impl Singleplayer { let settings2 = settings.clone(); let paused = Arc::new(AtomicBool::new(false)); - let paused1 = paused.clone(); + let paused1 = Arc::clone(&paused); let (result_sender, result_receiver) = bounded(1); diff --git a/world/src/lib.rs b/world/src/lib.rs index 823dcdf1d6..905575e8f0 100644 --- a/world/src/lib.rs +++ b/world/src/lib.rs @@ -1,6 +1,7 @@ #![deny(unsafe_code)] #![allow(incomplete_features)] #![allow(clippy::option_map_unit_fn)] +#![deny(clippy::clone_on_ref_ptr)] #![feature( arbitrary_enum_discriminant, bool_to_option,