Skip to content

Commit 86f2b29

Browse files
author
Daniel Smith
committed
Merge lints into one pass
1 parent d8c6bce commit 86f2b29

File tree

2 files changed

+24
-48
lines changed

2 files changed

+24
-48
lines changed

clippy_lints/src/await_holding_invalid.rs

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -49,48 +49,6 @@ declare_clippy_lint! {
4949
"Inside an async function, holding a MutexGuard while calling await"
5050
}
5151

52-
declare_lint_pass!(AwaitHoldingLock => [AWAIT_HOLDING_LOCK]);
53-
54-
impl LateLintPass<'_> for AwaitHoldingLock {
55-
fn check_body(&mut self, cx: &LateContext<'_>, body: &'_ Body<'_>) {
56-
use AsyncGeneratorKind::{Block, Closure, Fn};
57-
if let Some(GeneratorKind::Async(Block | Closure | Fn)) = body.generator_kind {
58-
let body_id = BodyId {
59-
hir_id: body.value.hir_id,
60-
};
61-
let def_id = cx.tcx.hir().body_owner_def_id(body_id);
62-
let typeck_results = cx.tcx.typeck(def_id);
63-
check_interior_types_lock(cx, &typeck_results.generator_interior_types, body.value.span);
64-
}
65-
}
66-
}
67-
68-
fn check_interior_types_lock(cx: &LateContext<'_>, ty_causes: &[GeneratorInteriorTypeCause<'_>], span: Span) {
69-
for ty_cause in ty_causes {
70-
if let rustc_middle::ty::Adt(adt, _) = ty_cause.ty.kind() {
71-
if is_mutex_guard(cx, adt.did) {
72-
span_lint_and_note(
73-
cx,
74-
AWAIT_HOLDING_LOCK,
75-
ty_cause.span,
76-
"this MutexGuard is held across an 'await' point. Consider using an async-aware Mutex type or ensuring the MutexGuard is dropped before calling await.",
77-
ty_cause.scope_span.or(Some(span)),
78-
"these are all the await points this lock is held through",
79-
);
80-
}
81-
}
82-
}
83-
}
84-
85-
fn is_mutex_guard(cx: &LateContext<'_>, def_id: DefId) -> bool {
86-
match_def_path(cx, def_id, &paths::MUTEX_GUARD)
87-
|| match_def_path(cx, def_id, &paths::RWLOCK_READ_GUARD)
88-
|| match_def_path(cx, def_id, &paths::RWLOCK_WRITE_GUARD)
89-
|| match_def_path(cx, def_id, &paths::PARKING_LOT_MUTEX_GUARD)
90-
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_READ_GUARD)
91-
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_WRITE_GUARD)
92-
}
93-
9452
declare_clippy_lint! {
9553
/// **What it does:** Checks for calls to await while holding a
9654
/// `RefCell` `Ref` or `RefMut`.
@@ -130,9 +88,9 @@ declare_clippy_lint! {
13088
"Inside an async function, holding a RefCell ref while calling await"
13189
}
13290

133-
declare_lint_pass!(AwaitHoldingRefCellRef => [AWAIT_HOLDING_REFCELL_REF]);
91+
declare_lint_pass!(AwaitHolding => [AWAIT_HOLDING_LOCK, AWAIT_HOLDING_REFCELL_REF]);
13492

135-
impl LateLintPass<'_> for AwaitHoldingRefCellRef {
93+
impl LateLintPass<'_> for AwaitHolding {
13694
fn check_body(&mut self, cx: &LateContext<'_>, body: &'_ Body<'_>) {
13795
use AsyncGeneratorKind::{Block, Closure, Fn};
13896
if let Some(GeneratorKind::Async(Block | Closure | Fn)) = body.generator_kind {
@@ -141,14 +99,24 @@ impl LateLintPass<'_> for AwaitHoldingRefCellRef {
14199
};
142100
let def_id = cx.tcx.hir().body_owner_def_id(body_id);
143101
let typeck_results = cx.tcx.typeck(def_id);
144-
check_interior_types_refcell(cx, &typeck_results.generator_interior_types, body.value.span);
102+
check_interior_types(cx, &typeck_results.generator_interior_types, body.value.span);
145103
}
146104
}
147105
}
148106

149-
fn check_interior_types_refcell(cx: &LateContext<'_>, ty_causes: &[GeneratorInteriorTypeCause<'_>], span: Span) {
107+
fn check_interior_types(cx: &LateContext<'_>, ty_causes: &[GeneratorInteriorTypeCause<'_>], span: Span) {
150108
for ty_cause in ty_causes {
151109
if let rustc_middle::ty::Adt(adt, _) = ty_cause.ty.kind() {
110+
if is_mutex_guard(cx, adt.did) {
111+
span_lint_and_note(
112+
cx,
113+
AWAIT_HOLDING_LOCK,
114+
ty_cause.span,
115+
"this MutexGuard is held across an 'await' point. Consider using an async-aware Mutex type or ensuring the MutexGuard is dropped before calling await.",
116+
ty_cause.scope_span.or(Some(span)),
117+
"these are all the await points this lock is held through",
118+
);
119+
}
152120
if is_refcell_ref(cx, adt.did) {
153121
span_lint_and_note(
154122
cx,
@@ -163,6 +131,15 @@ fn check_interior_types_refcell(cx: &LateContext<'_>, ty_causes: &[GeneratorInte
163131
}
164132
}
165133

134+
fn is_mutex_guard(cx: &LateContext<'_>, def_id: DefId) -> bool {
135+
match_def_path(cx, def_id, &paths::MUTEX_GUARD)
136+
|| match_def_path(cx, def_id, &paths::RWLOCK_READ_GUARD)
137+
|| match_def_path(cx, def_id, &paths::RWLOCK_WRITE_GUARD)
138+
|| match_def_path(cx, def_id, &paths::PARKING_LOT_MUTEX_GUARD)
139+
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_READ_GUARD)
140+
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_WRITE_GUARD)
141+
}
142+
166143
fn is_refcell_ref(cx: &LateContext<'_>, def_id: DefId) -> bool {
167144
match_def_path(cx, def_id, &paths::REFCELL_REF) || match_def_path(cx, def_id, &paths::REFCELL_REFMUT)
168145
}

clippy_lints/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -906,8 +906,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
906906
]);
907907
// end register lints, do not remove this comment, it’s used in `update_lints`
908908

909-
store.register_late_pass(|| box await_holding_invalid::AwaitHoldingLock);
910-
store.register_late_pass(|| box await_holding_invalid::AwaitHoldingRefCellRef);
909+
store.register_late_pass(|| box await_holding_invalid::AwaitHolding);
911910
store.register_late_pass(|| box serde_api::SerdeAPI);
912911
store.register_late_pass(|| box utils::internal_lints::CompilerLintFunctions::new());
913912
store.register_late_pass(|| box utils::internal_lints::LintWithoutLintPass::default());

0 commit comments

Comments
 (0)