-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a lint to check needless
Waker
clones
- Loading branch information
Showing
8 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use clippy_utils::{match_def_path, paths}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::sym; | ||
|
||
use super::WAKER_CLONE_AND_WAKE; | ||
|
||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) { | ||
let ty = cx.typeck_results().expr_ty(recv).peel_refs(); | ||
|
||
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 mut applicability = Applicability::MachineApplicable; | ||
|
||
span_lint_and_sugg( | ||
cx, | ||
WAKER_CLONE_AND_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) | ||
), | ||
applicability, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub fn wake(cx: &mut std::task::Context) { | ||
cx.waker().wake_by_ref() | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pub fn wake(cx: &mut std::task::Context) { | ||
cx.waker().clone().wake() | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
error: cloning a `Waker` only to wake it | ||
--> $DIR/waker_clone_and_wake.rs:2:5 | ||
| | ||
LL | cx.waker().clone().wake() | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `cx.waker().wake_by_ref()` | ||
| | ||
= note: `-D clippy::waker-clone-and-wake` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::waker_clone_and_wake)]` | ||
|
||
error: aborting due to previous error | ||
|