Apply suggestions

This commit is contained in:
Benoît du Garreau 2023-10-25 15:15:29 +02:00
parent f8790963d9
commit ebf6667b57
4 changed files with 37 additions and 19 deletions

View file

@ -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,
);
}

View file

@ -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() {}

View file

@ -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() {}

View file

@ -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