diff --git a/clippy_lints/src/methods/waker_clone_wake.rs b/clippy_lints/src/methods/waker_clone_wake.rs index db13266db80..da66632d55f 100644 --- a/clippy_lints/src/methods/waker_clone_wake.rs +++ b/clippy_lints/src/methods/waker_clone_wake.rs @@ -1,6 +1,6 @@ use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::source::snippet_with_applicability; -use clippy_utils::{match_def_path, paths}; +use clippy_utils::{is_trait_method, match_def_path, paths}; use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::LateContext; @@ -13,10 +13,11 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &' if let Some(did) = ty.ty_adt_def() && match_def_path(cx, did.did(), &paths::WAKER) - && let ExprKind::MethodCall(func, waker_ref, &[], _) = recv.kind - && func.ident.name == sym::clone + && let ExprKind::MethodCall(_, waker_ref, &[], _) = recv.kind + && is_trait_method(cx, recv, sym::Clone) { let mut applicability = Applicability::MachineApplicable; + let snippet = snippet_with_applicability(cx, waker_ref.span.source_callsite(), "..", &mut applicability); span_lint_and_sugg( cx, @@ -24,10 +25,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &' expr.span, "cloning a `Waker` only to wake it", "replace with", - format!( - "{}.wake_by_ref()", - snippet_with_applicability(cx, waker_ref.span, "..", &mut applicability) - ), + format!("{snippet}.wake_by_ref()"), applicability, ); } diff --git a/tests/ui/waker_clone_wake.fixed b/tests/ui/waker_clone_wake.fixed index 2df52f57d65..9c02b9a90fd 100644 --- a/tests/ui/waker_clone_wake.fixed +++ b/tests/ui/waker_clone_wake.fixed @@ -5,18 +5,25 @@ 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(); - // We don't do that for now + 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(); } -pub fn no_lint(c: &Custom) { - c.clone().wake() -} - fn main() {} diff --git a/tests/ui/waker_clone_wake.rs b/tests/ui/waker_clone_wake.rs index 4fe354b0ef1..edc3bbd8fc0 100644 --- a/tests/ui/waker_clone_wake.rs +++ b/tests/ui/waker_clone_wake.rs @@ -5,18 +5,25 @@ 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(); - // We don't do that for now + 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(); } -pub fn no_lint(c: &Custom) { - c.clone().wake() -} - fn main() {} diff --git a/tests/ui/waker_clone_wake.stderr b/tests/ui/waker_clone_wake.stderr index 426a577e620..f1abf4d9cb0 100644 --- a/tests/ui/waker_clone_wake.stderr +++ b/tests/ui/waker_clone_wake.stderr @@ -1,5 +1,5 @@ error: cloning a `Waker` only to wake it - --> $DIR/waker_clone_wake.rs:9:5 + --> $DIR/waker_clone_wake.rs:15:5 | LL | cx.waker().clone().wake(); | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `cx.waker().wake_by_ref()` @@ -7,5 +7,11 @@ LL | cx.waker().clone().wake(); = 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 +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