]> git.feebdaed.xyz Git - 0xmirror/tokio.git/commitdiff
ci: fix clippy warnings triggered under specific cfg (#7495)
authorMotoyuki Kimura <moymoymox@gmail.com>
Mon, 4 Aug 2025 10:36:17 +0000 (19:36 +0900)
committerGitHub <noreply@github.com>
Mon, 4 Aug 2025 10:36:17 +0000 (10:36 +0000)
14 files changed:
.github/workflows/ci.yml
tokio/src/fs/file.rs
tokio/src/runtime/dump.rs
tokio/src/runtime/io/driver/uring.rs
tokio/src/runtime/metrics/histogram.rs
tokio/src/runtime/metrics/histogram/h2_histogram.rs
tokio/src/runtime/scheduler/multi_thread/handle/taskdump.rs
tokio/src/runtime/task/mod.rs
tokio/src/runtime/task/trace/tree.rs
tokio/tests/macros_select.rs
tokio/tests/task_hooks.rs
tokio/tests/task_join_set.rs
tokio/tests/task_trace_self.rs
tokio/tests/tracing_task.rs

index 71b3ff84e80e31e68dcf84fe4c06b1a1a05b9b73..e53d566dcf378faf4f7429789e30748b27ae1e13 100644 (file)
@@ -832,6 +832,11 @@ jobs:
   clippy:
     name: clippy
     runs-on: ubuntu-latest
+    strategy:
+      matrix:
+        rustflags:
+          - ""
+          - "--cfg tokio_unstable --cfg tokio_taskdump --cfg tokio_uring -Dwarnings"
     steps:
       - uses: actions/checkout@v4
       - name: Install Rust ${{ env.rust_clippy }}
@@ -841,9 +846,10 @@ jobs:
           components: clippy
       - uses: Swatinem/rust-cache@v2
       # Run clippy
-      - name: "clippy --all"
+      - name: "clippy --all ${{ matrix.rustflags }}"
         run: cargo clippy --all --tests --all-features --no-deps
-
+        env:
+          RUSTFLAGS: ${{ matrix.rustflags }}
   docs:
     name: docs
     runs-on: ${{ matrix.run.os }}
index 83bc985c11bb1187dd5c291d5a32435f8ef869da..8d8bc9fb5a2fdaaaff89898652ec602c038d96c0 100644 (file)
@@ -507,6 +507,7 @@ impl File {
     /// # Ok(())
     /// # }
     /// ```
+    #[allow(clippy::result_large_err)]
     pub fn try_into_std(mut self) -> Result<StdFile, Self> {
         match Arc::try_unwrap(self.std) {
             Ok(file) => Ok(file),
index 0a6adf979b7dee6b7a1d0b2c0031f2755641be5a..57df2ca446424321540a0020f4119743b6909b5e 100644 (file)
@@ -61,7 +61,7 @@ impl BacktraceSymbol {
         let name = sym.name();
         Self {
             name: name.as_ref().map(|name| name.as_bytes().into()),
-            name_demangled: name.map(|name| format!("{}", name).into()),
+            name_demangled: name.map(|name| format!("{name}").into()),
             addr: sym.addr().map(Address),
             filename: sym.filename().map(From::from),
             lineno: sym.lineno(),
index 3370164d1fe0ae9d0c813be52cf24a2d7d41e5b8..fc4a098bafd79ea7752db04ca8cc7748000407de 100644 (file)
@@ -14,7 +14,7 @@ use std::{io, mem, task::Waker};
 const DEFAULT_RING_SIZE: u32 = 256;
 
 #[repr(usize)]
-#[derive(Debug, PartialEq, Eq)]
+#[derive(Debug, PartialEq, Eq, Copy, Clone)]
 enum State {
     Uninitialized = 0,
     Initialized = 1,
@@ -22,8 +22,8 @@ enum State {
 }
 
 impl State {
-    fn as_usize(self) -> usize {
-        self as usize
+    fn as_usize(&self) -> usize {
+        *self as usize
     }
 
     fn from_usize(value: usize) -> Self {
@@ -96,10 +96,10 @@ impl UringContext {
                     ops.remove(idx);
                 }
                 Some(other) => {
-                    panic!("unexpected lifecycle for slot {}: {:?}", idx, other);
+                    panic!("unexpected lifecycle for slot {idx}: {other:?}");
                 }
                 None => {
-                    panic!("no op at index {}", idx);
+                    panic!("no op at index {idx}");
                 }
             }
         }
@@ -293,7 +293,7 @@ impl Handle {
                 // We can safely remove the entry from the slab, as it has already been completed.
                 ops.remove(index);
             }
-            prev => panic!("Unexpected state: {:?}", prev),
+            prev => panic!("Unexpected state: {prev:?}"),
         };
     }
 }
index 6581ef10afc2408bfdbeca06e3ab9b2cb0901eac..a384fc64a250f44dbdfb7cc6d238bafba66654f6 100644 (file)
@@ -243,7 +243,7 @@ impl HistogramBuilder {
     }
 
     pub(crate) fn legacy_mut(&mut self, f: impl Fn(&mut LegacyBuilder)) {
-        let legacy = self.legacy.get_or_insert_with(|| LegacyBuilder::default());
+        let legacy = self.legacy.get_or_insert_with(LegacyBuilder::default);
         f(legacy);
     }
 
index 4bbd74925481ca29a2aa5181ac54b3902ccf7798..87716f1c6c139c527a7ce4c28a1b05aec6d904b7 100644 (file)
@@ -50,7 +50,7 @@ impl LogHistogram {
     /// - If `bucket_offset` is greater than the specified number of buckets, `(n - p + 1) * 2^p`
     fn from_n_p(n: u32, p: u32, bucket_offset: usize) -> Self {
         assert!(n >= p, "{n} (n) must be at least as large as {p} (p)");
-        let num_buckets = ((n - p + 1) * 1 << p) as usize - bucket_offset;
+        let num_buckets = ((n - p + 1) << p) as usize - bucket_offset;
         Self {
             num_buckets,
             p,
@@ -59,7 +59,7 @@ impl LogHistogram {
     }
 
     fn truncate_to_max_value(&self, max_value: u64) -> LogHistogram {
-        let mut hist = self.clone();
+        let mut hist = *self;
         while hist.max_value() >= max_value {
             hist.num_buckets -= 1;
         }
@@ -79,11 +79,7 @@ impl LogHistogram {
 
     pub(crate) fn value_to_bucket(&self, value: u64) -> usize {
         let index = bucket_index(value, self.p);
-        let offset_bucket = if index < self.bucket_offset as u64 {
-            0
-        } else {
-            index - self.bucket_offset as u64
-        };
+        let offset_bucket = index.saturating_sub(self.bucket_offset as u64);
         let max = self.num_buckets - 1;
         offset_bucket.min(max as u64) as usize
     }
@@ -180,7 +176,7 @@ impl LogHistogramBuilder {
         assert!(max_error > 0.0, "max_error must be greater than 0");
         assert!(max_error < 1.0, "max_error must be less than 1");
         let mut p = 2;
-        while 2_f64.powf(-1.0 * p as f64) > max_error && p <= MAX_PRECISION {
+        while 2_f64.powf(-(p as f64)) > max_error && p <= MAX_PRECISION {
             p += 1;
         }
         self.precision = Some(p);
@@ -434,9 +430,7 @@ mod test {
             assert_eq!(
                 config.value_to_bucket(i as u64),
                 i,
-                "{} should be in bucket {}",
-                i,
-                i
+                "{i} should be in bucket {i}"
             );
         }
 
index 477d857d88f7843f526955a4a6fbd2ea640badbd..1263c56c680b08fd3b45710818933cf96260c9cc 100644 (file)
@@ -7,7 +7,7 @@ impl Handle {
         let trace_status = &self.shared.trace_status;
 
         // If a dump is in progress, block.
-        trace_status.start_trace_request(&self).await;
+        trace_status.start_trace_request(self).await;
 
         let result = loop {
             if let Some(result) = trace_status.take_result() {
@@ -19,7 +19,7 @@ impl Handle {
         };
 
         // Allow other queued dumps to proceed.
-        trace_status.end_trace_request(&self).await;
+        trace_status.end_trace_request(self).await;
 
         result
     }
index 608a7dfcdb980ff33e00123991fd789df1dce15f..6467372acdd6d6c9e5418854f8f301b97c5fa733 100644 (file)
@@ -249,7 +249,7 @@ pub(crate) struct Notified<S: 'static>(Task<S>);
 impl<S> Notified<S> {
     #[cfg(all(tokio_unstable, feature = "rt-multi-thread"))]
     #[inline]
-    pub(crate) fn task_meta<'task, 'meta>(&'task self) -> crate::runtime::TaskMeta<'meta> {
+    pub(crate) fn task_meta<'meta>(&self) -> crate::runtime::TaskMeta<'meta> {
         self.0.task_meta()
     }
 }
@@ -270,7 +270,7 @@ pub(crate) struct LocalNotified<S: 'static> {
 impl<S> LocalNotified<S> {
     #[cfg(tokio_unstable)]
     #[inline]
-    pub(crate) fn task_meta<'task, 'meta>(&'task self) -> crate::runtime::TaskMeta<'meta> {
+    pub(crate) fn task_meta<'meta>(&self) -> crate::runtime::TaskMeta<'meta> {
         self.task.task_meta()
     }
 }
@@ -441,7 +441,7 @@ impl<S: 'static> Task<S> {
     // the compiler infers the lifetimes to be the same, and considers the task
     // to be borrowed for the lifetime of the returned `TaskMeta`.
     #[cfg(tokio_unstable)]
-    pub(crate) fn task_meta<'task, 'meta>(&'task self) -> crate::runtime::TaskMeta<'meta> {
+    pub(crate) fn task_meta<'meta>(&self) -> crate::runtime::TaskMeta<'meta> {
         crate::runtime::TaskMeta {
             id: self.id(),
             spawned_at: self.spawned_at().into(),
index 7e6f8efeca952be3ae33d5663d54529c63340aab..e2e69cd0c005f54204d45fad98432ceda0451789 100644 (file)
@@ -56,17 +56,17 @@ impl Tree {
         is_last: bool,
         prefix: &str,
     ) -> fmt::Result {
-        let root_fmt = format!("{}", root);
+        let root_fmt = format!("{root}");
 
         let current;
         let next;
 
         if is_last {
             current = format!("{prefix}└╼\u{a0}{root_fmt}");
-            next = format!("{}\u{a0}\u{a0}\u{a0}", prefix);
+            next = format!("{prefix}\u{a0}\u{a0}\u{a0}");
         } else {
             current = format!("{prefix}├╼\u{a0}{root_fmt}");
-            next = format!("{}│\u{a0}\u{a0}", prefix);
+            next = format!("{prefix}│\u{a0}\u{a0}");
         }
 
         write!(f, "{}", {
index 704e61337d247cc9f9cf0833f358bfee33c40328..3b403e4ce27033ba349a89954f0feffe071b7803 100644 (file)
@@ -655,22 +655,40 @@ mod unstable {
     #[cfg(all(feature = "rt-multi-thread", not(target_os = "wasi")))]
     fn deterministic_select_multi_thread() {
         let seed = b"bytes used to generate seed";
+        let (tx, rx) = std::sync::mpsc::channel();
         let rt1 = tokio::runtime::Builder::new_multi_thread()
             .worker_threads(1)
+            .on_thread_park(move || tx.send(()).unwrap())
             .rng_seed(RngSeed::from_bytes(seed))
             .build()
             .unwrap();
+
+        // This makes sure that `enter_runtime()` from worker thread is called before the one from main thread,
+        // ensuring that the RNG state is consistent. See also https://github.com/tokio-rs/tokio/pull/7495.
+        rx.recv().unwrap();
+
         let rt1_values = rt1.block_on(async {
-            let _ = tokio::spawn(async { (select_0_to_9().await, select_0_to_9().await) }).await;
+            tokio::spawn(async { (select_0_to_9().await, select_0_to_9().await) })
+                .await
+                .unwrap()
         });
 
+        let (tx, rx) = std::sync::mpsc::channel();
         let rt2 = tokio::runtime::Builder::new_multi_thread()
             .worker_threads(1)
+            .on_thread_park(move || tx.send(()).unwrap())
             .rng_seed(RngSeed::from_bytes(seed))
             .build()
             .unwrap();
+
+        // This makes sure that `enter_runtime()` from worker thread is called before the one from main thread,
+        // ensuring that the RNG state is consistent. See also https://github.com/tokio-rs/tokio/pull/7495.
+        rx.recv().unwrap();
+
         let rt2_values = rt2.block_on(async {
-            let _ = tokio::spawn(async { (select_0_to_9().await, select_0_to_9().await) }).await;
+            tokio::spawn(async { (select_0_to_9().await, select_0_to_9().await) })
+                .await
+                .unwrap()
         });
 
         assert_eq!(rt1_values, rt2_values);
index 1c590e77db154c50610a9b4d9a78544fc8b8bfda..42bb3fd946ce42cc1d78ec689fe8bc293abda69e 100644 (file)
@@ -34,16 +34,14 @@ fn spawn_task_hook_fires() {
     let count_realized = count.load(Ordering::SeqCst);
     assert_eq!(
         TASKS, count_realized,
-        "Total number of spawned task hook invocations was incorrect, expected {TASKS}, got {}",
-        count_realized
+        "Total number of spawned task hook invocations was incorrect, expected {TASKS}, got {count_realized}"
     );
 
     let count_ids_realized = ids.lock().unwrap().len();
 
     assert_eq!(
         TASKS, count_ids_realized,
-        "Total number of spawned task hook invocations was incorrect, expected {TASKS}, got {}",
-        count_realized
+        "Total number of spawned task hook invocations was incorrect, expected {TASKS}, got {count_realized}"
     );
 }
 
@@ -180,7 +178,7 @@ fn mk_spawn_location_hook(
     event: &'static str,
     count: &Arc<AtomicUsize>,
 ) -> impl Fn(&tokio::runtime::TaskMeta<'_>) {
-    let count = Arc::clone(&count);
+    let count = Arc::clone(count);
     move |data| {
         eprintln!("{event} ({:?}): {:?}", data.id(), data.spawned_at());
         // Assert that the spawn location is in this file.
index f705fa507d7750a5ad41aaa4e253d34a89bfc3d0..027f475e25fa0ca3b5da5b89c79c369b92ab7ff3 100644 (file)
@@ -334,7 +334,7 @@ async fn try_join_next_with_id() {
                 count += 1;
                 joined.insert(id);
             }
-            Some(Err(err)) => panic!("failed: {}", err),
+            Some(Err(err)) => panic!("failed: {err}"),
             None => {
                 break;
             }
index a4dc1f37e9cffb7bc103ac6df1bebd68ad9ce3c2..60e4268f8d325a35edc65d10c27a5a20fd089155 100644 (file)
@@ -92,8 +92,8 @@ async fn task_trace_self() {
     for line in good_line {
         let s = format!("{}:{}:", file!(), line);
         assert!(log.lock().unwrap().iter().any(|x| {
-            eprintln!("{}", x);
-            format!("{}", x).contains(&s)
+            eprintln!("{x}");
+            format!("{x}").contains(&s)
         }));
     }
     for line in bad_line {
@@ -102,6 +102,6 @@ async fn task_trace_self() {
             .lock()
             .unwrap()
             .iter()
-            .any(|x| format!("{}", x).contains(&s)));
+            .any(|x| format!("{x}").contains(&s)));
     }
 }
index a9317bf5b12eca08ba478db35629cca58da6a962..0cc9666aff2f2eac58714e0773a0bae39a8bc79f 100644 (file)
@@ -181,6 +181,6 @@ fn expect_task_named(name: &str) -> NewSpan {
         .named("runtime.spawn")
         .with_target("tokio::task")
         .with_fields(
-            expect::field("task.name").with_value(&tracing::field::debug(format_args!("{}", name))),
+            expect::field("task.name").with_value(&tracing::field::debug(format_args!("{name}"))),
         )
 }