Add a lint to check needless Waker clones

This commit is contained in:
Benoît du Garreau 2023-10-23 14:41:10 +02:00
parent 56c8235c99
commit f8790963d9
8 changed files with 119 additions and 0 deletions

View file

@ -0,0 +1,22 @@
#[derive(Clone)]
pub struct Custom;
impl Custom {
pub fn wake(self) {}
}
pub fn wake(cx: &mut std::task::Context) {
cx.waker().wake_by_ref();
// We don't do that for now
let w = cx.waker().clone();
w.wake();
cx.waker().clone().wake_by_ref();
}
pub fn no_lint(c: &Custom) {
c.clone().wake()
}
fn main() {}

View file

@ -0,0 +1,22 @@
#[derive(Clone)]
pub struct Custom;
impl Custom {
pub fn wake(self) {}
}
pub fn wake(cx: &mut std::task::Context) {
cx.waker().clone().wake();
// We don't do that for now
let w = cx.waker().clone();
w.wake();
cx.waker().clone().wake_by_ref();
}
pub fn no_lint(c: &Custom) {
c.clone().wake()
}
fn main() {}

View file

@ -0,0 +1,11 @@
error: cloning a `Waker` only to wake it
--> $DIR/waker_clone_wake.rs:9:5
|
LL | cx.waker().clone().wake();
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `cx.waker().wake_by_ref()`
|
= note: `-D clippy::waker-clone-wake` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::waker_clone_wake)]`
error: aborting due to previous error