From 3193fde77adf1473c4124adfa6ef8234659589c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=8C=E6=89=8B=E6=8E=89=E5=8C=85=E5=B7=A5=E7=A8=8B?= =?UTF-8?q?=E5=B8=88?= Date: Mon, 22 Apr 2024 21:35:21 +0800 Subject: [PATCH] chore: run clippy against all targets (#546) This would help us to check if there are any clippy warning in the tests. --- .github/workflows/ci.yml | 2 +- console-subscriber/examples/app.rs | 3 +-- console-subscriber/tests/spawn.rs | 3 +++ console-subscriber/tests/support/mod.rs | 4 ++-- console-subscriber/tests/support/subscriber.rs | 7 ++++--- console-subscriber/tests/support/task.rs | 15 ++------------- console-subscriber/tests/wake.rs | 2 -- 7 files changed, 13 insertions(+), 23 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8c56fdc58..51c6f7b26 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -100,7 +100,7 @@ jobs: run: cargo fmt --all -- --check - name: Run cargo clippy - run: cargo clippy -- -D warnings + run: cargo clippy --workspace --all-targets --no-deps -- -D warnings grpc_web: name: gRPC-web Example diff --git a/console-subscriber/examples/app.rs b/console-subscriber/examples/app.rs index 7fdfe2a57..3b533f565 100644 --- a/console-subscriber/examples/app.rs +++ b/console-subscriber/examples/app.rs @@ -145,7 +145,6 @@ async fn no_yield(seconds: u64) { #[tracing::instrument] async fn spawn_blocking(seconds: u64) { loop { - let seconds = seconds; _ = tokio::task::spawn_blocking(move || { std::thread::sleep(Duration::from_secs(seconds)); }) @@ -165,7 +164,7 @@ fn self_wake() -> impl Future { mut self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>, ) -> Poll { - if self.yielded == true { + if self.yielded { return Poll::Ready(()); } diff --git a/console-subscriber/tests/spawn.rs b/console-subscriber/tests/spawn.rs index 21ecaad41..67dc3abb3 100644 --- a/console-subscriber/tests/spawn.rs +++ b/console-subscriber/tests/spawn.rs @@ -10,6 +10,9 @@ use support::{assert_tasks, spawn_named, ExpectedTask}; /// spawned the child task). In this scenario, that would result in the parent /// having 3 polls counted, when it should really be 1. #[test] +// The child task is polled explicitly to control its execution. +// As a result, the clippy warning is ignored. +#[allow(clippy::async_yields_async)] fn child_polls_dont_count_towards_parent_polls() { let expected_tasks = vec![ ExpectedTask::default() diff --git a/console-subscriber/tests/support/mod.rs b/console-subscriber/tests/support/mod.rs index 460406a65..cee0027ce 100644 --- a/console-subscriber/tests/support/mod.rs +++ b/console-subscriber/tests/support/mod.rs @@ -60,7 +60,7 @@ where tokio::task::Builder::new() .name(name) .spawn(f) - .expect(&format!("spawning task '{name}' failed")) + .unwrap_or_else(|_| panic!("spawning task '{name}' failed")) } /// Wakes itself from within this task. @@ -91,7 +91,7 @@ pub(crate) fn self_wake() -> impl Future { mut self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>, ) -> Poll { - if self.yielded == true { + if self.yielded { return Poll::Ready(()); } diff --git a/console-subscriber/tests/support/subscriber.rs b/console-subscriber/tests/support/subscriber.rs index 54e5c995c..ba1aac489 100644 --- a/console-subscriber/tests/support/subscriber.rs +++ b/console-subscriber/tests/support/subscriber.rs @@ -23,9 +23,9 @@ struct TestFailure { impl fmt::Display for TestFailure { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Task validation failed:\n")?; + writeln!(f, "Task validation failed:")?; for failure in &self.failures { - write!(f, " - {failure}\n")?; + writeln!(f, " - {failure}")?; } Ok(()) } @@ -318,10 +318,11 @@ fn validate_expected_tasks( if failures.is_empty() { Ok(()) } else { - Err(TestFailure { failures: failures }) + Err(TestFailure { failures }) } } +#[allow(clippy::result_large_err)] fn validate_expected_task( expected: &ExpectedTask, actual_tasks: &Vec, diff --git a/console-subscriber/tests/support/task.rs b/console-subscriber/tests/support/task.rs index 593d45f97..6dfffaec1 100644 --- a/console-subscriber/tests/support/task.rs +++ b/console-subscriber/tests/support/task.rs @@ -84,7 +84,7 @@ impl fmt::Debug for TaskValidationFailure { /// This struct contains the fields that an expected task will attempt to match /// actual tasks on, as well as the expectations that will be used to validate /// which the actual task is as expected. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Default)] pub(crate) struct ExpectedTask { match_name: Option, expect_present: Option, @@ -93,18 +93,7 @@ pub(crate) struct ExpectedTask { expect_polls: Option, } -impl Default for ExpectedTask { - fn default() -> Self { - Self { - match_name: None, - expect_present: None, - expect_wakes: None, - expect_self_wakes: None, - expect_polls: None, - } - } -} - +#[allow(clippy::result_large_err)] impl ExpectedTask { /// Returns whether or not an actual task matches this expected task. /// diff --git a/console-subscriber/tests/wake.rs b/console-subscriber/tests/wake.rs index 65fa196c8..1b6d256dc 100644 --- a/console-subscriber/tests/wake.rs +++ b/console-subscriber/tests/wake.rs @@ -1,8 +1,6 @@ mod support; -use std::time::Duration; use support::{assert_task, ExpectedTask}; -use tokio::time::sleep; #[test] fn self_wake() {