Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.cargo: Run clippy on ALL the source files #2949

Merged
merged 9 commits into from
Oct 4, 2022
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[alias]
# Temporary solution to have clippy config in a single place until https://github.com/rust-lang/rust-clippy/blob/master/doc/roadmap-2021.md#lintstoml-configuration is shipped.
custom-clippy = "clippy --all-features --all-targets -- -A clippy::type_complexity -A clippy::pedantic -D warnings"
custom-clippy = "clippy --workspace --all-features --all-targets -- -A clippy::type_complexity -A clippy::pedantic -D warnings"
2 changes: 1 addition & 1 deletion core/benches/peer_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn clone(c: &mut Criterion) {

c.bench_function("clone", |b| {
b.iter(|| {
black_box(peer_id.clone());
black_box(peer_id);
})
});
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/identity/rsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ mod tests {
use super::*;
use quickcheck::*;

const KEY1: &'static [u8] = include_bytes!("test/rsa-2048.pk8");
const KEY2: &'static [u8] = include_bytes!("test/rsa-3072.pk8");
const KEY3: &'static [u8] = include_bytes!("test/rsa-4096.pk8");
const KEY1: &[u8] = include_bytes!("test/rsa-2048.pk8");
const KEY2: &[u8] = include_bytes!("test/rsa-3072.pk8");
const KEY3: &[u8] = include_bytes!("test/rsa-4096.pk8");

#[derive(Clone, Debug)]
struct SomeKeypair(Keypair);
Expand Down
10 changes: 5 additions & 5 deletions core/src/peer_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,10 @@ mod tests {

#[test]
fn extract_peer_id_from_multi_address() {
let address =
format!("/memory/1234/p2p/12D3KooWGQmdpzHXCqLno4mMxWXKNFQHASBeF99gTm2JR8Vu5Bdc")
.parse()
.unwrap();
let address = "/memory/1234/p2p/12D3KooWGQmdpzHXCqLno4mMxWXKNFQHASBeF99gTm2JR8Vu5Bdc"
.to_string()
.parse()
.unwrap();

let peer_id = PeerId::try_from_multiaddr(&address).unwrap();

Expand All @@ -303,7 +303,7 @@ mod tests {

#[test]
fn no_panic_on_extract_peer_id_from_multi_address_if_not_present() {
let address = format!("/memory/1234").parse().unwrap();
let address = "/memory/1234".to_string().parse().unwrap();

let maybe_empty = PeerId::try_from_multiaddr(&address);

Expand Down
2 changes: 1 addition & 1 deletion misc/multistream-select/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ mod tests {
fn prop(msg: Message) {
let mut buf = BytesMut::new();
msg.encode(&mut buf)
.expect(&format!("Encoding message failed: {:?}", msg));
.unwrap_or_else(|_| panic!("Encoding message failed: {:?}", msg));
match Message::decode(buf.freeze()) {
Ok(m) => assert_eq!(m, msg),
Err(e) => panic!("Decoding failed: {:?}", e),
Expand Down
7 changes: 4 additions & 3 deletions misc/multistream-select/tests/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ fn transport_upgrade() {
let addr = addr_receiver.await.unwrap();
dialer.dial(addr).unwrap();
futures::future::poll_fn(move |cx| loop {
match ready!(dialer.poll_next_unpin(cx)).unwrap() {
SwarmEvent::ConnectionEstablished { .. } => return Poll::Ready(()),
_ => {}
if let SwarmEvent::ConnectionEstablished { .. } =
ready!(dialer.poll_next_unpin(cx)).unwrap()
{
return Poll::Ready(());
}
})
.await
Expand Down
2 changes: 1 addition & 1 deletion muxers/mplex/benches/split_send_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const BENCH_SIZES: [usize; 8] = [
fn prepare(c: &mut Criterion) {
let _ = env_logger::try_init();

let payload: Vec<u8> = vec![1; 1024 * 1024 * 1];
let payload: Vec<u8> = vec![1; 1024 * 1024];

let mut tcp = c.benchmark_group("tcp");
let tcp_addr = multiaddr![Ip4(std::net::Ipv4Addr::new(127, 0, 0, 1)), Tcp(0u16)];
Expand Down
2 changes: 1 addition & 1 deletion muxers/mplex/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,7 @@ mod tests {
w_buf: BytesMut::new(),
eof: false,
};
let mut m = Multiplexed::new(conn, cfg.clone());
let mut m = Multiplexed::new(conn, cfg);

// Run the test.
let mut opened = HashSet::new();
Expand Down
11 changes: 5 additions & 6 deletions muxers/mplex/tests/two_peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,10 @@ fn protocol_not_match() {
let mut transport = TcpTransport::default()
.and_then(move |c, e| upgrade::apply(c, mplex, e, upgrade::Version::V1))
.boxed();
match transport.dial(rx.await.unwrap()).unwrap().await {
Ok(_) => {
assert!(false, "Dialing should fail here as protocols do not match")
}
_ => {}
}

assert!(
transport.dial(rx.await.unwrap()).unwrap().await.is_err(),
"Dialing should fail here as protocols do not match"
);
});
}
1 change: 1 addition & 0 deletions protocols/autonat/examples/autonat_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ impl Behaviour {
}

#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
enum Event {
AutoNat(autonat::Event),
Identify(identify::Event),
Expand Down
1 change: 1 addition & 0 deletions protocols/autonat/examples/autonat_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ impl Behaviour {
}

#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
enum Event {
AutoNat(autonat::Event),
Identify(identify::Event),
Expand Down
42 changes: 17 additions & 25 deletions protocols/autonat/tests/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ async fn spawn_server(kill: oneshot::Receiver<()>) -> (PeerId, Multiaddr) {
.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap())
.unwrap();
let addr = loop {
match server.select_next_some().await {
SwarmEvent::NewListenAddr { address, .. } => break address,
_ => {}
if let SwarmEvent::NewListenAddr { address, .. } = server.select_next_some().await {
break address;
};
};
tx.send((peer_id, addr)).unwrap();
Expand All @@ -78,11 +77,8 @@ async fn spawn_server(kill: oneshot::Receiver<()>) -> (PeerId, Multiaddr) {

async fn next_event(swarm: &mut Swarm<Behaviour>) -> Event {
loop {
match swarm.select_next_some().await {
SwarmEvent::Behaviour(event) => {
break event;
}
_ => {}
if let SwarmEvent::Behaviour(event) = swarm.select_next_some().await {
break event;
}
}
}
Expand Down Expand Up @@ -177,9 +173,8 @@ async fn test_auto_probe() {
.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap())
.unwrap();
loop {
match client.select_next_some().await {
SwarmEvent::NewListenAddr { .. } => break,
_ => {}
if let SwarmEvent::NewListenAddr { .. } = client.select_next_some().await {
break;
}
}

Expand Down Expand Up @@ -269,14 +264,13 @@ async fn test_confidence() {
.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap())
.unwrap();
loop {
match client.select_next_some().await {
SwarmEvent::NewListenAddr { .. } => break,
_ => {}
if let SwarmEvent::NewListenAddr { .. } = client.select_next_some().await {
break;
}
}
} else {
let unreachable_addr: Multiaddr = "/ip4/127.0.0.1/tcp/42".parse().unwrap();
client.add_external_address(unreachable_addr.clone(), AddressScore::Infinite);
client.add_external_address(unreachable_addr, AddressScore::Infinite);
}

for i in 0..MAX_CONFIDENCE + 1 {
Expand Down Expand Up @@ -357,9 +351,8 @@ async fn test_throttle_server_period() {
.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap())
.unwrap();
loop {
match client.select_next_some().await {
SwarmEvent::NewListenAddr { .. } => break,
_ => {}
if let SwarmEvent::NewListenAddr { .. } = client.select_next_some().await {
break;
}
}

Expand Down Expand Up @@ -477,9 +470,8 @@ async fn test_outbound_failure() {
.unwrap();

loop {
match client.select_next_some().await {
SwarmEvent::NewListenAddr { .. } => break,
_ => {}
if let SwarmEvent::NewListenAddr { .. } = client.select_next_some().await {
break;
}
}
// First probe should be successful and flip status to public.
Expand All @@ -497,7 +489,8 @@ async fn test_outbound_failure() {
}

let inactive = servers.split_off(1);
// Drop the handles of the inactive servers to kill them.

#[allow(clippy::needless_collect)] // Drop the handles of the inactive servers to kill them.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

out of curiosity to understand in this case why clippy was complaining I commented the allow and ran the cargo +nightly clippy --all-features --all-targets -- -A clippy::type_complexity -A clippy::pedantic -D warnings command. But clippy didn't complain, is it not required?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need the --workspace flag! My best guess is that this is because our root Cargo.toml is a crate and a workspace declaration.

I wonder whether we could avoid this if we put the meta crate in a directory too.

Thoughts @mxinden?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, was running outdated rust version 😅 sorry. Btw with the latest nightly there are new lints triggered for large_enum_variant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, was running outdated rust version 😅 sorry. Btw with the latest nightly there are new lints triggered for large_enum_variant.

Yeah I know :)

I opened #2951 for this recently!

let inactive_ids: Vec<_> = inactive.into_iter().map(|(id, _handle)| id).collect();

// Expect to retry on outbound failure
Expand Down Expand Up @@ -541,9 +534,8 @@ async fn test_global_ips_config() {
.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap())
.unwrap();
loop {
match client.select_next_some().await {
SwarmEvent::NewListenAddr { .. } => break,
_ => {}
if let SwarmEvent::NewListenAddr { .. } = client.select_next_some().await {
break;
}
}

Expand Down
26 changes: 9 additions & 17 deletions protocols/autonat/tests/test_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ async fn init_server(config: Option<Config>) -> (Swarm<Behaviour>, PeerId, Multi
.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap())
.unwrap();
let addr = loop {
match server.select_next_some().await {
SwarmEvent::NewListenAddr { address, .. } => break address,
_ => {}
if let SwarmEvent::NewListenAddr { address, .. } = server.select_next_some().await {
break address;
};
};
(server, peer_id, addr)
Expand Down Expand Up @@ -91,12 +90,9 @@ async fn spawn_client(
.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap())
.unwrap();
loop {
match client.select_next_some().await {
SwarmEvent::NewListenAddr { address, .. } => {
addr = Some(address);
break;
}
_ => {}
if let SwarmEvent::NewListenAddr { address, .. } = client.select_next_some().await {
addr = Some(address);
break;
};
}
}
Expand All @@ -119,11 +115,8 @@ async fn spawn_client(

async fn next_event(swarm: &mut Swarm<Behaviour>) -> Event {
loop {
match swarm.select_next_some().await {
SwarmEvent::Behaviour(event) => {
break event;
}
_ => {}
if let SwarmEvent::Behaviour(event) = swarm.select_next_some().await {
break event;
}
}
}
Expand Down Expand Up @@ -161,9 +154,8 @@ async fn test_dial_back() {
} => {
assert_eq!(peer_id, client_id);
let observed_client_ip = loop {
match send_back_addr.pop().unwrap() {
Protocol::Ip4(ip4_addr) => break ip4_addr,
_ => {}
if let Protocol::Ip4(ip4_addr) = send_back_addr.pop().unwrap() {
break ip4_addr;
}
};
break observed_client_ip;
Expand Down
1 change: 1 addition & 0 deletions protocols/dcutr/examples/dcutr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ fn main() -> Result<(), Box<dyn Error>> {
}

#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
enum Event {
Ping(ping::Event),
Identify(identify::Event),
Expand Down
11 changes: 4 additions & 7 deletions protocols/dcutr/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ fn connect() {
let mut dst = build_client();
let dst_peer_id = *dst.local_peer_id();
let dst_relayed_addr = relay_addr
.clone()
.with(Protocol::P2p(relay_peer_id.into()))
.with(Protocol::P2pCircuit)
.with(Protocol::P2p(dst_peer_id.into()));
Expand Down Expand Up @@ -96,7 +95,7 @@ fn connect() {
fn build_relay() -> Swarm<relay::Relay> {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let local_peer_id = local_public_key.clone().to_peer_id();
let local_peer_id = local_public_key.to_peer_id();

let transport = build_transport(MemoryTransport::default().boxed(), local_public_key);

Expand All @@ -116,7 +115,7 @@ fn build_relay() -> Swarm<relay::Relay> {
fn build_client() -> Swarm<Client> {
let local_key = identity::Keypair::generate_ed25519();
let local_public_key = local_key.public();
let local_peer_id = local_public_key.clone().to_peer_id();
let local_peer_id = local_public_key.to_peer_id();

let (relay_transport, behaviour) = client::Client::new_transport_and_behaviour(local_peer_id);
let transport = build_transport(
Expand All @@ -141,13 +140,11 @@ fn build_transport<StreamSink>(
where
StreamSink: AsyncRead + AsyncWrite + Send + Unpin + 'static,
{
let transport = transport
transport
.upgrade(Version::V1)
.authenticate(PlainText2Config { local_public_key })
.multiplex(libp2p::yamux::YamuxConfig::default())
.boxed();

transport
.boxed()
}

#[derive(NetworkBehaviour)]
Expand Down
Loading