From 4e7ff3ebf27336f2f014067f33437c428c81f5e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Oliveira?= Date: Sat, 26 Oct 2024 20:10:25 +0100 Subject: [PATCH] chore(ci): address clippy beta lints (#5649) Co-authored-by: Darius Clark --- Cargo.lock | 1 + identity/src/peer_id.rs | 2 +- identity/src/rsa.rs | 4 ++-- misc/quick-protobuf-codec/src/lib.rs | 2 +- muxers/test-harness/src/lib.rs | 2 +- .../autonat/src/v1/behaviour/as_client.rs | 4 ++-- .../autonat/src/v1/behaviour/as_server.rs | 4 ++-- protocols/gossipsub/src/backoff.rs | 3 +-- protocols/kad/src/behaviour.rs | 4 ++-- protocols/ping/src/protocol.rs | 1 - swarm/src/connection.rs | 2 +- swarm/src/handler.rs | 6 ++--- transports/noise/src/io/handshake.rs | 1 - transports/websocket-websys/src/lib.rs | 4 ++-- transports/websocket/src/framed.rs | 2 +- transports/webtransport-websys/Cargo.toml | 1 + transports/webtransport-websys/src/utils.rs | 22 +++++++------------ 17 files changed, 29 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3b983a80d00..f0c36291839 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3484,6 +3484,7 @@ dependencies = [ "multiaddr", "multibase", "multihash", + "once_cell", "send_wrapper 0.6.0", "thiserror", "tracing", diff --git a/identity/src/peer_id.rs b/identity/src/peer_id.rs index 7b3f799f612..8ae6d99ae32 100644 --- a/identity/src/peer_id.rs +++ b/identity/src/peer_id.rs @@ -191,7 +191,7 @@ impl<'de> Deserialize<'de> for PeerId { struct PeerIdVisitor; - impl<'de> Visitor<'de> for PeerIdVisitor { + impl Visitor<'_> for PeerIdVisitor { type Value = PeerId; fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { diff --git a/identity/src/rsa.rs b/identity/src/rsa.rs index cbfe3c1b919..5eb78a4af75 100644 --- a/identity/src/rsa.rs +++ b/identity/src/rsa.rs @@ -149,7 +149,7 @@ struct Asn1RawOid<'a> { object: DerObject<'a>, } -impl<'a> Asn1RawOid<'a> { +impl Asn1RawOid<'_> { /// The underlying OID as byte literal. pub(crate) fn oid(&self) -> &[u8] { self.object.value() @@ -169,7 +169,7 @@ impl<'a> DerTypeView<'a> for Asn1RawOid<'a> { } } -impl<'a> DerEncodable for Asn1RawOid<'a> { +impl DerEncodable for Asn1RawOid<'_> { fn encode(&self, sink: &mut S) -> Result<(), Asn1DerError> { self.object.encode(sink) } diff --git a/misc/quick-protobuf-codec/src/lib.rs b/misc/quick-protobuf-codec/src/lib.rs index 32cee8eccac..c57b7da7db8 100644 --- a/misc/quick-protobuf-codec/src/lib.rs +++ b/misc/quick-protobuf-codec/src/lib.rs @@ -120,7 +120,7 @@ impl<'a> BytesMutWriterBackend<'a> { } } -impl<'a> WriterBackend for BytesMutWriterBackend<'a> { +impl WriterBackend for BytesMutWriterBackend<'_> { fn pb_write_u8(&mut self, x: u8) -> quick_protobuf::Result<()> { self.dst.put_u8(x); diff --git a/muxers/test-harness/src/lib.rs b/muxers/test-harness/src/lib.rs index 16c71f414f0..d03bdbdfed7 100644 --- a/muxers/test-harness/src/lib.rs +++ b/muxers/test-harness/src/lib.rs @@ -206,7 +206,7 @@ enum Event { ProtocolComplete, } -impl<'m, M> Stream for Harness<'m, M> +impl Stream for Harness<'_, M> where M: StreamMuxer + Unpin, { diff --git a/protocols/autonat/src/v1/behaviour/as_client.rs b/protocols/autonat/src/v1/behaviour/as_client.rs index 8960163ccb3..385dee50ee1 100644 --- a/protocols/autonat/src/v1/behaviour/as_client.rs +++ b/protocols/autonat/src/v1/behaviour/as_client.rs @@ -98,7 +98,7 @@ pub(crate) struct AsClient<'a> { pub(crate) other_candidates: &'a HashSet, } -impl<'a> HandleInnerEvent for AsClient<'a> { +impl HandleInnerEvent for AsClient<'_> { fn handle_event( &mut self, event: request_response::Event, @@ -179,7 +179,7 @@ impl<'a> HandleInnerEvent for AsClient<'a> { } } -impl<'a> AsClient<'a> { +impl AsClient<'_> { pub(crate) fn poll_auto_probe(&mut self, cx: &mut Context<'_>) -> Poll { match self.schedule_probe.poll_unpin(cx) { Poll::Ready(()) => { diff --git a/protocols/autonat/src/v1/behaviour/as_server.rs b/protocols/autonat/src/v1/behaviour/as_server.rs index 1289bd53d24..01148add6e8 100644 --- a/protocols/autonat/src/v1/behaviour/as_server.rs +++ b/protocols/autonat/src/v1/behaviour/as_server.rs @@ -91,7 +91,7 @@ pub(crate) struct AsServer<'a> { >, } -impl<'a> HandleInnerEvent for AsServer<'a> { +impl HandleInnerEvent for AsServer<'_> { fn handle_event( &mut self, event: request_response::Event, @@ -208,7 +208,7 @@ impl<'a> HandleInnerEvent for AsServer<'a> { } } -impl<'a> AsServer<'a> { +impl AsServer<'_> { pub(crate) fn on_outbound_connection( &mut self, peer: &PeerId, diff --git a/protocols/gossipsub/src/backoff.rs b/protocols/gossipsub/src/backoff.rs index b24da318582..4414ffb00e6 100644 --- a/protocols/gossipsub/src/backoff.rs +++ b/protocols/gossipsub/src/backoff.rs @@ -48,8 +48,7 @@ pub(crate) struct BackoffStorage { impl BackoffStorage { fn heartbeats(d: &Duration, heartbeat_interval: &Duration) -> usize { - ((d.as_nanos() + heartbeat_interval.as_nanos() - 1) / heartbeat_interval.as_nanos()) - as usize + d.as_nanos().div_ceil(heartbeat_interval.as_nanos()) as usize } pub(crate) fn new( diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 84133d31acb..f577971167f 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -3361,7 +3361,7 @@ pub struct QueryMut<'a> { query: &'a mut Query, } -impl<'a> QueryMut<'a> { +impl QueryMut<'_> { pub fn id(&self) -> QueryId { self.query.id() } @@ -3391,7 +3391,7 @@ pub struct QueryRef<'a> { query: &'a Query, } -impl<'a> QueryRef<'a> { +impl QueryRef<'_> { pub fn id(&self) -> QueryId { self.query.id() } diff --git a/protocols/ping/src/protocol.rs b/protocols/ping/src/protocol.rs index 566e5e258e2..6e3f06d0498 100644 --- a/protocols/ping/src/protocol.rs +++ b/protocols/ping/src/protocol.rs @@ -44,7 +44,6 @@ pub const PROTOCOL_NAME: StreamProtocol = StreamProtocol::new("/ipfs/ping/1.0.0" /// > Nagle's algorithm, delayed acks and similar configuration options /// > which can affect latencies especially on otherwise low-volume /// > connections. - const PING_SIZE: usize = 32; /// Sends a ping and waits for the pong. diff --git a/swarm/src/connection.rs b/swarm/src/connection.rs index 2f9afc38418..859d138b83a 100644 --- a/swarm/src/connection.rs +++ b/swarm/src/connection.rs @@ -516,7 +516,7 @@ pub(crate) struct IncomingInfo<'a> { pub(crate) send_back_addr: &'a Multiaddr, } -impl<'a> IncomingInfo<'a> { +impl IncomingInfo<'_> { /// Builds the [`ConnectedPoint`] corresponding to the incoming connection. pub(crate) fn create_connected_point(&self) -> ConnectedPoint { ConnectedPoint::Listener { diff --git a/swarm/src/handler.rs b/swarm/src/handler.rs index 610b95b8cf1..9e31592d68d 100644 --- a/swarm/src/handler.rs +++ b/swarm/src/handler.rs @@ -226,7 +226,7 @@ pub enum ConnectionEvent<'a, IP: InboundUpgradeSend, OP: OutboundUpgradeSend, IO RemoteProtocolsChange(ProtocolsChange<'a>), } -impl<'a, IP, OP, IOI, OOI> fmt::Debug for ConnectionEvent<'a, IP, OP, IOI, OOI> +impl fmt::Debug for ConnectionEvent<'_, IP, OP, IOI, OOI> where IP: InboundUpgradeSend + fmt::Debug, IP::Output: fmt::Debug, @@ -262,8 +262,8 @@ where } } -impl<'a, IP: InboundUpgradeSend, OP: OutboundUpgradeSend, IOI, OOI> - ConnectionEvent<'a, IP, OP, IOI, OOI> +impl + ConnectionEvent<'_, IP, OP, IOI, OOI> { /// Whether the event concerns an outbound stream. pub fn is_outbound(&self) -> bool { diff --git a/transports/noise/src/io/handshake.rs b/transports/noise/src/io/handshake.rs index 5c1fa806b6d..8993a5795b6 100644 --- a/transports/noise/src/io/handshake.rs +++ b/transports/noise/src/io/handshake.rs @@ -73,7 +73,6 @@ where /// will be sent and received on the given I/O resource and using the /// provided session for cryptographic operations according to the chosen /// Noise handshake pattern. - pub(crate) fn new( io: T, session: snow::HandshakeState, diff --git a/transports/websocket-websys/src/lib.rs b/transports/websocket-websys/src/lib.rs index f353d92b204..3467e802bc5 100644 --- a/transports/websocket-websys/src/lib.rs +++ b/transports/websocket-websys/src/lib.rs @@ -96,8 +96,8 @@ impl libp2p_core::Transport for Transport { return Err(TransportError::MultiaddrNotSupported(addr)); } - let url = extract_websocket_url(&addr) - .ok_or_else(|| TransportError::MultiaddrNotSupported(addr))?; + let url = + extract_websocket_url(&addr).ok_or(TransportError::MultiaddrNotSupported(addr))?; Ok(async move { let socket = match WebSocket::new(&url) { diff --git a/transports/websocket/src/framed.rs b/transports/websocket/src/framed.rs index a547aea21ef..198443508d9 100644 --- a/transports/websocket/src/framed.rs +++ b/transports/websocket/src/framed.rs @@ -442,7 +442,7 @@ pub(crate) enum WsListenProto<'a> { TlsWs(Cow<'a, str>), } -impl<'a> WsListenProto<'a> { +impl WsListenProto<'_> { pub(crate) fn append_on_addr(&self, addr: &mut Multiaddr) { match self { WsListenProto::Ws(path) => { diff --git a/transports/webtransport-websys/Cargo.toml b/transports/webtransport-websys/Cargo.toml index 9541c49b737..eeb474d4a63 100644 --- a/transports/webtransport-websys/Cargo.toml +++ b/transports/webtransport-websys/Cargo.toml @@ -21,6 +21,7 @@ libp2p-identity = { workspace = true } libp2p-noise = { workspace = true } multiaddr = { workspace = true } multihash = { workspace = true } +once_cell = "1.19.0" send_wrapper = { version = "0.6.0", features = ["futures"] } thiserror = "1.0.61" tracing = { workspace = true } diff --git a/transports/webtransport-websys/src/utils.rs b/transports/webtransport-websys/src/utils.rs index 55bad08e00c..0b3550e5b5b 100644 --- a/transports/webtransport-websys/src/utils.rs +++ b/transports/webtransport-websys/src/utils.rs @@ -1,10 +1,17 @@ use js_sys::{Promise, Reflect}; +use once_cell::sync::Lazy; use send_wrapper::SendWrapper; use std::io; use wasm_bindgen::{JsCast, JsValue}; use crate::Error; +type Closure = wasm_bindgen::closure::Closure; +static DO_NOTHING: Lazy> = Lazy::new(|| { + let cb = Closure::new(|_| {}); + SendWrapper::new(cb) +}); + /// Properly detach a promise. /// /// A promise always runs in the background, however if you don't await it, @@ -13,22 +20,9 @@ use crate::Error; // // Ref: https://github.com/typescript-eslint/typescript-eslint/blob/391a6702c0a9b5b3874a7a27047f2a721f090fb6/packages/eslint-plugin/docs/rules/no-floating-promises.md pub(crate) fn detach_promise(promise: Promise) { - type Closure = wasm_bindgen::closure::Closure; - static mut DO_NOTHING: Option> = None; - - // Allocate Closure only once and reuse it - let do_nothing = unsafe { - if DO_NOTHING.is_none() { - let cb = Closure::new(|_| {}); - DO_NOTHING = Some(SendWrapper::new(cb)); - } - - DO_NOTHING.as_deref().unwrap() - }; - // Avoid having "floating" promise and ignore any errors. // After `catch` promise is allowed to be dropped. - let _ = promise.catch(do_nothing); + let _ = promise.catch(&DO_NOTHING); } /// Typecasts a JavaScript type.