forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#11698 - a1phyr:waker_clone_and_wake, r=y21
Add `waker_clone_and_wake` lint to check needless `Waker` clones Check for patterns of `waker.clone().wake()` and replace them with `waker.wake_by_ref()`. An alternative name could be `waker_clone_then_wake` changelog: [ `waker_clone_wake`]: new lint
- Loading branch information
Showing
8 changed files
with
137 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,32 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use clippy_utils::{is_trait_method, 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_WAKE; | ||
|
||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) { | ||
let ty = cx.typeck_results().expr_ty(recv); | ||
|
||
if let Some(did) = ty.ty_adt_def() | ||
&& match_def_path(cx, did.did(), &paths::WAKER) | ||
&& 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!("{snippet}.wake_by_ref()"), | ||
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,29 @@ | ||
#[derive(Clone)] | ||
pub struct Custom; | ||
|
||
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(); | ||
|
||
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(); | ||
} | ||
|
||
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,29 @@ | ||
#[derive(Clone)] | ||
pub struct Custom; | ||
|
||
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(); | ||
|
||
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(); | ||
} | ||
|
||
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,17 @@ | ||
error: cloning a `Waker` only to wake it | ||
--> $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: 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 | ||
|