about summary refs log tree commit diff
path: root/clippy_lints/src/methods/mod.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-10-26 21:01:40 +0000
committerbors <bors@rust-lang.org>2023-10-26 21:01:40 +0000
commit2f0f4ddcf73a82115168d187a5c2121b413e34a2 (patch)
tree6905d68c743b60b0c4bef06be884084ae9d590e2 /clippy_lints/src/methods/mod.rs
parent392b2552806d2479cf73f519c0825c44c1fde0e3 (diff)
parentebf6667b571568e88e4b8934013515cb69db74e8 (diff)
Auto merge of #11698 - a1phyr:waker_clone_and_wake, r=y21
Add `waker_clone_and_wake` lint to check needless `Waker` clones

Check for patterns of `waker.clone().wake()` and replace them with `waker.wake_by_ref()`.

An alternative name could be `waker_clone_then_wake`

changelog: [ `waker_clone_wake`]: new lint
Diffstat (limited to 'clippy_lints/src/methods/mod.rs')
-rw-r--r--clippy_lints/src/methods/mod.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs
index 373d8606b1d..e1a14e29358 100644
--- a/clippy_lints/src/methods/mod.rs
+++ b/clippy_lints/src/methods/mod.rs
@@ -112,6 +112,7 @@ mod useless_asref;
 mod utils;
 mod vec_resize_to_zero;
 mod verbose_file_reads;
+mod waker_clone_wake;
 mod wrong_self_convention;
 mod zst_offset;
 
@@ -3632,6 +3633,28 @@ declare_clippy_lint! {
     "`as_str` used to call a method on `str` that is also available on `String`"
 }
 
+declare_clippy_lint! {
+    /// ### What it does
+    /// Checks for usage of `waker.clone().wake()`
+    ///
+    /// ### Why is this bad?
+    /// Cloning the waker is not necessary, `wake_by_ref()` enables the same operation
+    /// without extra cloning/dropping.
+    ///
+    /// ### Example
+    /// ```rust,ignore
+    /// waker.clone().wake();
+    /// ```
+    /// Should be written
+    /// ```rust,ignore
+    /// waker.wake_by_ref();
+    /// ```
+    #[clippy::version = "1.75.0"]
+    pub WAKER_CLONE_WAKE,
+    perf,
+    "cloning a `Waker` only to wake it"
+}
+
 pub struct Methods {
     avoid_breaking_exported_api: bool,
     msrv: Msrv,
@@ -3777,6 +3800,7 @@ impl_lint_pass!(Methods => [
     ITER_OUT_OF_BOUNDS,
     PATH_ENDS_WITH_EXT,
     REDUNDANT_AS_STR,
+    WAKER_CLONE_WAKE,
 ]);
 
 /// Extracts a method call name, args, and `Span` of the method name.
@@ -4365,6 +4389,9 @@ impl Methods {
                     }
                     unnecessary_literal_unwrap::check(cx, expr, recv, name, args);
                 },
+                ("wake", []) => {
+                    waker_clone_wake::check(cx, expr, recv);
+                }
                 ("write", []) => {
                     readonly_write_lock::check(cx, expr, recv);
                 }