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
This commit is contained in:
commit
2f0f4ddcf7
8 changed files with 137 additions and 0 deletions
29
tests/ui/waker_clone_wake.fixed
Normal file
29
tests/ui/waker_clone_wake.fixed
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#[derive(Clone)]
|
||||
pub struct Custom;
|
||||
|
||||
impl Custom {
|
||||
pub fn wake(self) {}
|
||||
}
|
||||
|
||||
macro_rules! mac {
|
||||
($cx:ident) => {
|
||||
$cx.waker()
|
||||
};
|
||||
}
|
||||
|
||||
pub fn wake(cx: &mut std::task::Context) {
|
||||
cx.waker().wake_by_ref();
|
||||
|
||||
mac!(cx).wake_by_ref();
|
||||
}
|
||||
|
||||
pub fn no_lint(cx: &mut std::task::Context, c: &Custom) {
|
||||
c.clone().wake();
|
||||
|
||||
let w = cx.waker().clone();
|
||||
w.wake();
|
||||
|
||||
cx.waker().clone().wake_by_ref();
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
29
tests/ui/waker_clone_wake.rs
Normal file
29
tests/ui/waker_clone_wake.rs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#[derive(Clone)]
|
||||
pub struct Custom;
|
||||
|
||||
impl Custom {
|
||||
pub fn wake(self) {}
|
||||
}
|
||||
|
||||
macro_rules! mac {
|
||||
($cx:ident) => {
|
||||
$cx.waker()
|
||||
};
|
||||
}
|
||||
|
||||
pub fn wake(cx: &mut std::task::Context) {
|
||||
cx.waker().clone().wake();
|
||||
|
||||
mac!(cx).clone().wake();
|
||||
}
|
||||
|
||||
pub fn no_lint(cx: &mut std::task::Context, c: &Custom) {
|
||||
c.clone().wake();
|
||||
|
||||
let w = cx.waker().clone();
|
||||
w.wake();
|
||||
|
||||
cx.waker().clone().wake_by_ref();
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
17
tests/ui/waker_clone_wake.stderr
Normal file
17
tests/ui/waker_clone_wake.stderr
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
error: cloning a `Waker` only to wake it
|
||||
--> $DIR/waker_clone_wake.rs:15: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: cloning a `Waker` only to wake it
|
||||
--> $DIR/waker_clone_wake.rs:17:5
|
||||
|
|
||||
LL | mac!(cx).clone().wake();
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `mac!(cx).wake_by_ref()`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue