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::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability; 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_errors::Applicability;
use rustc_hir::{Expr, ExprKind}; use rustc_hir::{Expr, ExprKind};
use rustc_lint::LateContext; 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() if let Some(did) = ty.ty_adt_def()
&& match_def_path(cx, did.did(), &paths::WAKER) && match_def_path(cx, did.did(), &paths::WAKER)
&& let ExprKind::MethodCall(func, waker_ref, &[], _) = recv.kind && let ExprKind::MethodCall(_, waker_ref, &[], _) = recv.kind
&& func.ident.name == sym::clone && is_trait_method(cx, recv, sym::Clone)
{ {
let mut applicability = Applicability::MachineApplicable; let mut applicability = Applicability::MachineApplicable;
let snippet = snippet_with_applicability(cx, waker_ref.span.source_callsite(), "..", &mut applicability);
span_lint_and_sugg( span_lint_and_sugg(
cx, cx,
@ -24,10 +25,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'
expr.span, expr.span,
"cloning a `Waker` only to wake it", "cloning a `Waker` only to wake it",
"replace with", "replace with",
format!( format!("{snippet}.wake_by_ref()"),
"{}.wake_by_ref()",
snippet_with_applicability(cx, waker_ref.span, "..", &mut applicability)
),
applicability, applicability,
); );
} }

View File

@ -5,18 +5,25 @@ impl Custom {
pub fn wake(self) {} pub fn wake(self) {}
} }
macro_rules! mac {
($cx:ident) => {
$cx.waker()
};
}
pub fn wake(cx: &mut std::task::Context) { pub fn wake(cx: &mut std::task::Context) {
cx.waker().wake_by_ref(); 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(); let w = cx.waker().clone();
w.wake(); w.wake();
cx.waker().clone().wake_by_ref(); cx.waker().clone().wake_by_ref();
} }
pub fn no_lint(c: &Custom) {
c.clone().wake()
}
fn main() {} fn main() {}

View File

@ -5,18 +5,25 @@ impl Custom {
pub fn wake(self) {} pub fn wake(self) {}
} }
macro_rules! mac {
($cx:ident) => {
$cx.waker()
};
}
pub fn wake(cx: &mut std::task::Context) { pub fn wake(cx: &mut std::task::Context) {
cx.waker().clone().wake(); 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(); let w = cx.waker().clone();
w.wake(); w.wake();
cx.waker().clone().wake_by_ref(); cx.waker().clone().wake_by_ref();
} }
pub fn no_lint(c: &Custom) {
c.clone().wake()
}
fn main() {} fn main() {}

View File

@ -1,5 +1,5 @@
error: cloning a `Waker` only to wake it 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(); LL | cx.waker().clone().wake();
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `cx.waker().wake_by_ref()` | ^^^^^^^^^^^^^^^^^^^^^^^^^ 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` = note: `-D clippy::waker-clone-wake` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::waker_clone_wake)]` = 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