Skip to content

Commit

Permalink
*: Fix clippy warnings (libp2p#2139)
Browse files Browse the repository at this point in the history
* Fix needless question mark operator

* Don't convert from u64 to u64

LocalStreamId is already a u64, no need to convert.

* Don't use `.into()` to convert to the same type

* Don't specify lifetime if it can be inferred

* Use `vec!` macro if we immediately push to it

This creates the vector with the appropriate capacity.

* Don't index array when taking a reference is enough

Co-authored-by: Max Inden <mail@max-inden.de>
  • Loading branch information
thomaseizinger and mxinden authored Jul 16, 2021
1 parent e1646a5 commit 20183c1
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 16 deletions.
4 changes: 2 additions & 2 deletions core/src/peer_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ impl PeerId {

/// Parses a `PeerId` from bytes.
pub fn from_bytes(data: &[u8]) -> Result<PeerId, Error> {
Ok(PeerId::from_multihash(Multihash::from_bytes(&data)?)
.map_err(|mh| Error::UnsupportedCode(mh.code()))?)
PeerId::from_multihash(Multihash::from_bytes(&data)?)
.map_err(|mh| Error::UnsupportedCode(mh.code()))
}

/// Tries to turn a `Multihash` into a `PeerId`.
Expand Down
14 changes: 7 additions & 7 deletions muxers/mplex/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,25 +244,25 @@ impl Encoder for Codec {
fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
let (header, data) = match item {
Frame::Open { stream_id } => {
(u64::from(stream_id.num) << 3, Bytes::new())
(stream_id.num << 3, Bytes::new())
},
Frame::Data { stream_id: LocalStreamId { num, role: Endpoint::Listener }, data } => {
(u64::from(num) << 3 | 1, data)
(num << 3 | 1, data)
},
Frame::Data { stream_id: LocalStreamId { num, role: Endpoint::Dialer }, data } => {
(u64::from(num) << 3 | 2, data)
(num << 3 | 2, data)
},
Frame::Close { stream_id: LocalStreamId { num, role: Endpoint::Listener } } => {
(u64::from(num) << 3 | 3, Bytes::new())
(num << 3 | 3, Bytes::new())
},
Frame::Close { stream_id: LocalStreamId { num, role: Endpoint::Dialer } } => {
(u64::from(num) << 3 | 4, Bytes::new())
(num << 3 | 4, Bytes::new())
},
Frame::Reset { stream_id: LocalStreamId { num, role: Endpoint::Listener } } => {
(u64::from(num) << 3 | 5, Bytes::new())
(num << 3 | 5, Bytes::new())
},
Frame::Reset { stream_id: LocalStreamId { num, role: Endpoint::Dialer } } => {
(u64::from(num) << 3 | 6, Bytes::new())
(num << 3 | 6, Bytes::new())
},
};

Expand Down
2 changes: 1 addition & 1 deletion protocols/identify/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ impl ProtocolsHandler for IdentifyHandler {
};
Poll::Ready(ev)
}
Poll::Ready(Err(err)) => Poll::Ready(ProtocolsHandlerEvent::Close(err.into()))
Poll::Ready(Err(err)) => Poll::Ready(ProtocolsHandlerEvent::Close(err))
}
}
}
2 changes: 1 addition & 1 deletion protocols/identify/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ where
Ok(v) => v,
Err(err) => {
debug!("Invalid message: {:?}", err);
return Err(err.into())
return Err(err)
}
};

Expand Down
2 changes: 1 addition & 1 deletion protocols/kad/src/kbucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ where
///
/// The buckets are ordered by proximity to the `local_key`, i.e. the first
/// bucket is the closest bucket (containing at most one key).
pub fn iter<'a>(&'a mut self) -> impl Iterator<Item = KBucketRef<'a, TKey, TVal>> + 'a {
pub fn iter(&mut self) -> impl Iterator<Item = KBucketRef<'_, TKey, TVal>> + '_ {
let applied_pending = &mut self.applied_pending;
self.buckets.iter_mut().enumerate().map(move |(i, b)| {
if let Some(applied) = b.apply_pending() {
Expand Down
3 changes: 1 addition & 2 deletions protocols/mdns/src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,7 @@ fn append_txt_record(
if value.len() > MAX_TXT_VALUE_LENGTH {
return Err(MdnsResponseError::TxtRecordTooLong);
}
let mut buffer = Vec::new();
buffer.push(value.len() as u8);
let mut buffer = vec![value.len() as u8];
append_character_string(&mut buffer, value)?;

append_u16(out, buffer.len() as u16);
Expand Down
2 changes: 1 addition & 1 deletion transports/noise/src/protocol/x25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl snow::types::Dh for Keypair<X25519> {

fn set(&mut self, sk: &[u8]) {
let mut secret = [0u8; 32];
secret.copy_from_slice(&sk[..]);
secret.copy_from_slice(&sk);
self.secret = SecretKey(X25519(secret)); // Copy
self.public = PublicKey(X25519(x25519(secret, X25519_BASEPOINT_BYTES)));
secret.zeroize();
Expand Down
2 changes: 1 addition & 1 deletion transports/noise/src/protocol/x25519_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl snow::types::Dh for Keypair<X25519Spec> {

fn set(&mut self, sk: &[u8]) {
let mut secret = [0u8; 32];
secret.copy_from_slice(&sk[..]);
secret.copy_from_slice(&sk);
self.secret = SecretKey(X25519Spec(secret)); // Copy
self.public = PublicKey(X25519Spec(x25519(secret, X25519_BASEPOINT_BYTES)));
secret.zeroize();
Expand Down

0 comments on commit 20183c1

Please sign in to comment.