Skip to content

Commit

Permalink
Apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
a1phyr committed Oct 25, 2023
1 parent f879096 commit ebf6667
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
12 changes: 5 additions & 7 deletions clippy_lints/src/methods/waker_clone_wake.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,21 +13,19 @@ 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,
WAKER_CLONE_WAKE,
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,
);
}
Expand Down
17 changes: 12 additions & 5 deletions tests/ui/waker_clone_wake.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
17 changes: 12 additions & 5 deletions tests/ui/waker_clone_wake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
10 changes: 8 additions & 2 deletions tests/ui/waker_clone_wake.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
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()`
|
= 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

0 comments on commit ebf6667

Please sign in to comment.