Skip to content

Commit 630aa37

Browse files
authored
ci: Enforce and apply clippy rules (#388)
Our CI step `Run clippy` did not treat warnings as errors. Therefore, the process terminated with exit code zero even when the clippy rules were not followed. This PR ensures the clippy rules are enforced by our CI. While at it, this PR fixes the clippy rules by `--fix` and manually fixes bits where needed. Since the code inside tokio::select and logs is not formatted by rustfmt, I've only paid attention to our production code. There might be a few places in our tests where these lines are long or not properly formatted. Detected by: #384 --------- Signed-off-by: Alexandru Vasile <[email protected]>
1 parent cf83fbe commit 630aa37

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+282
-408
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ jobs:
162162
cache-all-crates: true
163163

164164
- name: Run clippy
165-
run: cargo clippy --all-features
165+
run: cargo clippy --all-features -- -D warnings
166166

167167
test:
168168
name: Test

examples/custom_executor.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,10 @@ async fn main() {
144144
tokio::select! {
145145
_ = executor2.next() => {}
146146
_ = litep2p2.next_event() => {},
147-
event = ping_event_stream2.next() => match event {
148-
Some(PingEvent::Ping { peer, ping }) => tracing::info!(
149-
"ping time with {peer:?}: {ping:?}"
150-
),
151-
_ => {}
152-
}
147+
event = ping_event_stream2.next() =>
148+
if let Some(PingEvent::Ping { peer, ping }) = event {
149+
tracing::info!("ping time with {peer:?}: {ping:?}")
150+
}
153151
}
154152
}
155153
}

examples/echo_notification.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,21 @@ async fn client_event_loop(mut litep2p: Litep2p, mut handle: NotificationHandle,
5050
loop {
5151
tokio::select! {
5252
_ = litep2p.next_event() => {}
53-
event = handle.next() => match event.unwrap() {
54-
NotificationEvent::NotificationStreamOpened { .. } => break,
55-
_ => {},
56-
}
53+
event = handle.next() =>
54+
if let NotificationEvent::NotificationStreamOpened { .. } = event.unwrap() {
55+
break
56+
}
5757
}
5858
}
5959

6060
// after the substream is open, send notification to server and print the response to stdout
6161
loop {
6262
tokio::select! {
6363
_ = litep2p.next_event() => {}
64-
event = handle.next() => match event.unwrap() {
65-
NotificationEvent::NotificationReceived { peer, notification } => {
64+
event = handle.next() =>
65+
if let NotificationEvent::NotificationReceived { peer, notification } = event.unwrap() {
6666
println!("received response from server ({peer:?}): {notification:?}");
67-
}
68-
_ => {},
69-
},
67+
},
7068
_ = tokio::time::sleep(Duration::from_secs(3)) => {
7169
handle.send_sync_notification(peer, vec![1, 3, 3, 7]).unwrap();
7270
}

src/addresses.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ use crate::PeerId;
3333
///
3434
/// # Note
3535
///
36-
/// - The addresses are reported to the identify protocol and are used by other nodes
37-
/// to establish a connection with the local node.
36+
/// - The addresses are reported to the identify protocol and are used by other nodes to establish a
37+
/// connection with the local node.
3838
///
3939
/// - Users must ensure that the addresses are reachable from the network.
4040
#[derive(Debug, Clone)]

src/codec/identity.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ mod tests {
9999
#[test]
100100
fn decoding_smaller_payloads() {
101101
let mut codec = Identity::new(100);
102-
let bytes = vec![3u8; 64];
102+
let bytes = [3u8; 64];
103103
let mut bytes = BytesMut::from(&bytes[..]);
104104

105105
let decoded = codec.decode(&mut bytes);

src/crypto/noise/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,9 +679,8 @@ fn parse_and_verify_peer_id(
679679
let identity = payload.identity_key.ok_or(NegotiationError::PeerIdMissing)?;
680680
let remote_public_key = PublicKey::from_protobuf_encoding(&identity)?;
681681
let remote_key_signature =
682-
payload.identity_sig.ok_or(NegotiationError::BadSignature).map_err(|err| {
682+
payload.identity_sig.ok_or(NegotiationError::BadSignature).inspect_err(|_err| {
683683
tracing::debug!(target: LOG_TARGET, "payload without signature");
684-
err
685684
})?;
686685

687686
let peer_id = PeerId::from_public_key(&remote_public_key);

src/mock/substream.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,38 +102,29 @@ impl DummySubstream {
102102
impl Sink<bytes::Bytes> for DummySubstream {
103103
type Error = SubstreamError;
104104

105-
fn poll_ready<'a>(
106-
self: Pin<&mut Self>,
107-
_cx: &mut Context<'a>,
108-
) -> Poll<Result<(), SubstreamError>> {
105+
fn poll_ready(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), SubstreamError>> {
109106
Poll::Pending
110107
}
111108

112109
fn start_send(self: Pin<&mut Self>, _item: bytes::Bytes) -> Result<(), SubstreamError> {
113110
Ok(())
114111
}
115112

116-
fn poll_flush<'a>(
117-
self: Pin<&mut Self>,
118-
_cx: &mut Context<'a>,
119-
) -> Poll<Result<(), SubstreamError>> {
113+
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), SubstreamError>> {
120114
Poll::Pending
121115
}
122116

123-
fn poll_close<'a>(
124-
self: Pin<&mut Self>,
125-
_cx: &mut Context<'a>,
126-
) -> Poll<Result<(), SubstreamError>> {
117+
fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<(), SubstreamError>> {
127118
Poll::Ready(Ok(()))
128119
}
129120
}
130121

131122
impl Stream for DummySubstream {
132123
type Item = Result<BytesMut, SubstreamError>;
133124

134-
fn poll_next<'a>(
125+
fn poll_next(
135126
self: Pin<&mut Self>,
136-
_cx: &mut Context<'a>,
127+
_cx: &mut Context<'_>,
137128
) -> Poll<Option<Result<BytesMut, SubstreamError>>> {
138129
Poll::Pending
139130
}

src/multistream_select/dialer_select.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ mod tests {
557557
futures_ringbuf::Endpoint::pair(100, 100);
558558

559559
let server = tokio::spawn(async move {
560-
let protos = vec!["/proto2"];
560+
let protos = ["/proto2"];
561561

562562
let multistream = b"/multistream/1.0.0\n";
563563
let len = multistream.len();
@@ -638,7 +638,7 @@ mod tests {
638638
let (client_connection, mut server_connection) = futures_ringbuf::Endpoint::pair(100, 100);
639639

640640
let server = tokio::spawn(async move {
641-
let protos = vec!["/proto2"];
641+
let protos = ["/proto2"];
642642

643643
let multistream = b"/multistream/1.0.0\n";
644644
let len = multistream.len();
@@ -695,7 +695,7 @@ mod tests {
695695
let (client_connection, mut server_connection) = futures_ringbuf::Endpoint::pair(100, 100);
696696

697697
let server = tokio::spawn(async move {
698-
let protos = vec!["/proto2"];
698+
let protos = ["/proto2"];
699699

700700
let multistream = b"/multistream/1.0.0\n";
701701
let len = multistream.len();
@@ -809,7 +809,7 @@ mod tests {
809809
// send only header line
810810
let mut bytes = BytesMut::with_capacity(32);
811811
let message = Message::Header(HeaderLine::V1);
812-
let _ = message.encode(&mut bytes).map_err(|_| Error::InvalidData).unwrap();
812+
message.encode(&mut bytes).map_err(|_| Error::InvalidData).unwrap();
813813

814814
let (mut dialer_state, _message) =
815815
WebRtcDialerState::propose(ProtocolName::from("/13371338/proto/1"), vec![]).unwrap();
@@ -828,7 +828,7 @@ mod tests {
828828
Protocol::try_from(&b"/13371338/proto/1"[..]).unwrap(),
829829
Protocol::try_from(&b"/sup/proto/1"[..]).unwrap(),
830830
]);
831-
let _ = message.encode(&mut bytes).map_err(|_| Error::InvalidData).unwrap();
831+
message.encode(&mut bytes).map_err(|_| Error::InvalidData).unwrap();
832832

833833
let (mut dialer_state, _message) =
834834
WebRtcDialerState::propose(ProtocolName::from("/13371338/proto/1"), vec![]).unwrap();
@@ -841,12 +841,9 @@ mod tests {
841841

842842
#[test]
843843
fn negotiate_main_protocol() {
844-
let message = webrtc_encode_multistream_message(
845-
vec![Message::Protocol(
846-
Protocol::try_from(&b"/13371338/proto/1"[..]).unwrap(),
847-
)]
848-
.into_iter(),
849-
)
844+
let message = webrtc_encode_multistream_message(vec![Message::Protocol(
845+
Protocol::try_from(&b"/13371338/proto/1"[..]).unwrap(),
846+
)])
850847
.unwrap()
851848
.freeze();
852849

@@ -857,20 +854,18 @@ mod tests {
857854
.unwrap();
858855

859856
match dialer_state.register_response(message.to_vec()) {
860-
Ok(HandshakeResult::Succeeded(negotiated)) =>
861-
assert_eq!(negotiated, ProtocolName::from("/13371338/proto/1")),
857+
Ok(HandshakeResult::Succeeded(negotiated)) => {
858+
assert_eq!(negotiated, ProtocolName::from("/13371338/proto/1"))
859+
}
862860
event => panic!("invalid event {event:?}"),
863861
}
864862
}
865863

866864
#[test]
867865
fn negotiate_fallback_protocol() {
868-
let message = webrtc_encode_multistream_message(
869-
vec![Message::Protocol(
870-
Protocol::try_from(&b"/sup/proto/1"[..]).unwrap(),
871-
)]
872-
.into_iter(),
873-
)
866+
let message = webrtc_encode_multistream_message(vec![Message::Protocol(
867+
Protocol::try_from(&b"/sup/proto/1"[..]).unwrap(),
868+
)])
874869
.unwrap()
875870
.freeze();
876871

@@ -881,8 +876,9 @@ mod tests {
881876
.unwrap();
882877

883878
match dialer_state.register_response(message.to_vec()) {
884-
Ok(HandshakeResult::Succeeded(negotiated)) =>
885-
assert_eq!(negotiated, ProtocolName::from("/sup/proto/1")),
879+
Ok(HandshakeResult::Succeeded(negotiated)) => {
880+
assert_eq!(negotiated, ProtocolName::from("/sup/proto/1"))
881+
}
886882
_ => panic!("invalid event"),
887883
}
888884
}

src/multistream_select/listener_select.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -405,20 +405,17 @@ mod tests {
405405

406406
#[test]
407407
fn webrtc_listener_negotiate_works() {
408-
let mut local_protocols = vec![
408+
let mut local_protocols = [
409409
ProtocolName::from("/13371338/proto/1"),
410410
ProtocolName::from("/sup/proto/1"),
411411
ProtocolName::from("/13371338/proto/2"),
412412
ProtocolName::from("/13371338/proto/3"),
413413
ProtocolName::from("/13371338/proto/4"),
414414
];
415-
let message = webrtc_encode_multistream_message(
416-
vec![
417-
Message::Protocol(Protocol::try_from(&b"/13371338/proto/1"[..]).unwrap()),
418-
Message::Protocol(Protocol::try_from(&b"/sup/proto/1"[..]).unwrap()),
419-
]
420-
.into_iter(),
421-
)
415+
let message = webrtc_encode_multistream_message(vec![
416+
Message::Protocol(Protocol::try_from(&b"/13371338/proto/1"[..]).unwrap()),
417+
Message::Protocol(Protocol::try_from(&b"/sup/proto/1"[..]).unwrap()),
418+
])
422419
.unwrap()
423420
.freeze();
424421

@@ -433,7 +430,7 @@ mod tests {
433430

434431
#[test]
435432
fn invalid_message() {
436-
let mut local_protocols = vec![
433+
let mut local_protocols = [
437434
ProtocolName::from("/13371338/proto/1"),
438435
ProtocolName::from("/sup/proto/1"),
439436
ProtocolName::from("/13371338/proto/2"),
@@ -455,7 +452,7 @@ mod tests {
455452

456453
#[test]
457454
fn only_header_line_received() {
458-
let mut local_protocols = vec![
455+
let mut local_protocols = [
459456
ProtocolName::from("/13371338/proto/1"),
460457
ProtocolName::from("/sup/proto/1"),
461458
ProtocolName::from("/13371338/proto/2"),
@@ -466,7 +463,7 @@ mod tests {
466463
// send only header line
467464
let mut bytes = BytesMut::with_capacity(32);
468465
let message = Message::Header(HeaderLine::V1);
469-
let _ = message.encode(&mut bytes).map_err(|_| Error::InvalidData).unwrap();
466+
message.encode(&mut bytes).map_err(|_| Error::InvalidData).unwrap();
470467

471468
match webrtc_listener_negotiate(&mut local_protocols.iter(), bytes.freeze()) {
472469
Err(error) => assert!(std::matches!(
@@ -481,7 +478,7 @@ mod tests {
481478

482479
#[test]
483480
fn header_line_missing() {
484-
let mut local_protocols = vec![
481+
let mut local_protocols = [
485482
ProtocolName::from("/13371338/proto/1"),
486483
ProtocolName::from("/sup/proto/1"),
487484
ProtocolName::from("/13371338/proto/2"),
@@ -495,7 +492,7 @@ mod tests {
495492
Protocol::try_from(&b"/13371338/proto/1"[..]).unwrap(),
496493
Protocol::try_from(&b"/sup/proto/1"[..]).unwrap(),
497494
]);
498-
let _ = message.encode(&mut bytes).map_err(|_| Error::InvalidData).unwrap();
495+
message.encode(&mut bytes).map_err(|_| Error::InvalidData).unwrap();
499496

500497
match webrtc_listener_negotiate(&mut local_protocols.iter(), bytes.freeze()) {
501498
Err(error) => assert!(std::matches!(
@@ -510,19 +507,16 @@ mod tests {
510507

511508
#[test]
512509
fn protocol_not_supported() {
513-
let mut local_protocols = vec![
510+
let mut local_protocols = [
514511
ProtocolName::from("/13371338/proto/1"),
515512
ProtocolName::from("/sup/proto/1"),
516513
ProtocolName::from("/13371338/proto/2"),
517514
ProtocolName::from("/13371338/proto/3"),
518515
ProtocolName::from("/13371338/proto/4"),
519516
];
520-
let message = webrtc_encode_multistream_message(
521-
vec![Message::Protocol(
522-
Protocol::try_from(&b"/13371339/proto/1"[..]).unwrap(),
523-
)]
524-
.into_iter(),
525-
)
517+
let message = webrtc_encode_multistream_message(vec![Message::Protocol(
518+
Protocol::try_from(&b"/13371339/proto/1"[..]).unwrap(),
519+
)])
526520
.unwrap()
527521
.freeze();
528522

src/peer_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl<'de> Deserialize<'de> for PeerId {
204204

205205
struct PeerIdVisitor;
206206

207-
impl<'de> Visitor<'de> for PeerIdVisitor {
207+
impl Visitor<'_> for PeerIdVisitor {
208208
type Value = PeerId;
209209

210210
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {

0 commit comments

Comments
 (0)