Skip to content

Commit a675778

Browse files
committed
Auto merge of #6029 - Daniel-B-Smith:refcell_ref_await, r=flip1995
Add lint for holding RefCell Ref across an await Fixes #6008 This introduces the lint await_holding_refcell_ref. For async functions, we iterate over all types in generator_interior_types and look for `core::cell::Ref` or `core::cell::RefMut`. If we find one then we emit a lint. Heavily cribs from: #5439 changelog: introduce the await_holding_refcell_ref lint
2 parents b06856e + 4d33225 commit a675778

File tree

7 files changed

+257
-9
lines changed

7 files changed

+257
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,7 @@ Released 2018-09-13
16321632
[`assign_ops`]: https://rust-lang.github.io/rust-clippy/master/index.html#assign_ops
16331633
[`async_yields_async`]: https://rust-lang.github.io/rust-clippy/master/index.html#async_yields_async
16341634
[`await_holding_lock`]: https://rust-lang.github.io/rust-clippy/master/index.html#await_holding_lock
1635+
[`await_holding_refcell_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#await_holding_refcell_ref
16351636
[`bad_bit_mask`]: https://rust-lang.github.io/rust-clippy/master/index.html#bad_bit_mask
16361637
[`bind_instead_of_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#bind_instead_of_map
16371638
[`blacklisted_name`]: https://rust-lang.github.io/rust-clippy/master/index.html#blacklisted_name

clippy_lints/src/await_holding_lock.rs renamed to clippy_lints/src/await_holding_invalid.rs

+56-3
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,52 @@ declare_clippy_lint! {
4545
/// }
4646
/// ```
4747
pub AWAIT_HOLDING_LOCK,
48-
pedantic,
48+
correctness,
4949
"Inside an async function, holding a MutexGuard while calling await"
5050
}
5151

52-
declare_lint_pass!(AwaitHoldingLock => [AWAIT_HOLDING_LOCK]);
52+
declare_clippy_lint! {
53+
/// **What it does:** Checks for calls to await while holding a
54+
/// `RefCell` `Ref` or `RefMut`.
55+
///
56+
/// **Why is this bad?** `RefCell` refs only check for exclusive mutable access
57+
/// at runtime. Holding onto a `RefCell` ref across an `await` suspension point
58+
/// risks panics from a mutable ref shared while other refs are outstanding.
59+
///
60+
/// **Known problems:** None.
61+
///
62+
/// **Example:**
63+
///
64+
/// ```rust,ignore
65+
/// use std::cell::RefCell;
66+
///
67+
/// async fn foo(x: &RefCell<u32>) {
68+
/// let b = x.borrow_mut()();
69+
/// *ref += 1;
70+
/// bar.await;
71+
/// }
72+
/// ```
73+
///
74+
/// Use instead:
75+
/// ```rust,ignore
76+
/// use std::cell::RefCell;
77+
///
78+
/// async fn foo(x: &RefCell<u32>) {
79+
/// {
80+
/// let b = x.borrow_mut();
81+
/// *ref += 1;
82+
/// }
83+
/// bar.await;
84+
/// }
85+
/// ```
86+
pub AWAIT_HOLDING_REFCELL_REF,
87+
correctness,
88+
"Inside an async function, holding a RefCell ref while calling await"
89+
}
5390

54-
impl LateLintPass<'_> for AwaitHoldingLock {
91+
declare_lint_pass!(AwaitHolding => [AWAIT_HOLDING_LOCK, AWAIT_HOLDING_REFCELL_REF]);
92+
93+
impl LateLintPass<'_> for AwaitHolding {
5594
fn check_body(&mut self, cx: &LateContext<'_>, body: &'_ Body<'_>) {
5695
use AsyncGeneratorKind::{Block, Closure, Fn};
5796
if let Some(GeneratorKind::Async(Block | Closure | Fn)) = body.generator_kind {
@@ -78,6 +117,16 @@ fn check_interior_types(cx: &LateContext<'_>, ty_causes: &[GeneratorInteriorType
78117
"these are all the await points this lock is held through",
79118
);
80119
}
120+
if is_refcell_ref(cx, adt.did) {
121+
span_lint_and_note(
122+
cx,
123+
AWAIT_HOLDING_REFCELL_REF,
124+
ty_cause.span,
125+
"this RefCell Ref is held across an 'await' point. Consider ensuring the Ref is dropped before calling await.",
126+
ty_cause.scope_span.or(Some(span)),
127+
"these are all the await points this ref is held through",
128+
);
129+
}
81130
}
82131
}
83132
}
@@ -90,3 +139,7 @@ fn is_mutex_guard(cx: &LateContext<'_>, def_id: DefId) -> bool {
90139
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_READ_GUARD)
91140
|| match_def_path(cx, def_id, &paths::PARKING_LOT_RWLOCK_WRITE_GUARD)
92141
}
142+
143+
fn is_refcell_ref(cx: &LateContext<'_>, def_id: DefId) -> bool {
144+
match_def_path(cx, def_id, &paths::REFCELL_REF) || match_def_path(cx, def_id, &paths::REFCELL_REFMUT)
145+
}

clippy_lints/src/lib.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ mod assign_ops;
160160
mod async_yields_async;
161161
mod atomic_ordering;
162162
mod attrs;
163-
mod await_holding_lock;
163+
mod await_holding_invalid;
164164
mod bit_mask;
165165
mod blacklisted_name;
166166
mod blocks_in_if_conditions;
@@ -509,7 +509,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
509509
&attrs::MISMATCHED_TARGET_OS,
510510
&attrs::UNKNOWN_CLIPPY_LINTS,
511511
&attrs::USELESS_ATTRIBUTE,
512-
&await_holding_lock::AWAIT_HOLDING_LOCK,
512+
&await_holding_invalid::AWAIT_HOLDING_LOCK,
513+
&await_holding_invalid::AWAIT_HOLDING_REFCELL_REF,
513514
&bit_mask::BAD_BIT_MASK,
514515
&bit_mask::INEFFECTIVE_BIT_MASK,
515516
&bit_mask::VERBOSE_BIT_MASK,
@@ -906,7 +907,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
906907
]);
907908
// end register lints, do not remove this comment, it’s used in `update_lints`
908909

909-
store.register_late_pass(|| box await_holding_lock::AwaitHoldingLock);
910+
store.register_late_pass(|| box await_holding_invalid::AwaitHolding);
910911
store.register_late_pass(|| box serde_api::SerdeAPI);
911912
store.register_late_pass(|| box utils::internal_lints::CompilerLintFunctions::new());
912913
store.register_late_pass(|| box utils::internal_lints::LintWithoutLintPass::default());
@@ -1190,7 +1191,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11901191

11911192
store.register_group(true, "clippy::pedantic", Some("clippy_pedantic"), vec![
11921193
LintId::of(&attrs::INLINE_ALWAYS),
1193-
LintId::of(&await_holding_lock::AWAIT_HOLDING_LOCK),
11941194
LintId::of(&bit_mask::VERBOSE_BIT_MASK),
11951195
LintId::of(&checked_conversions::CHECKED_CONVERSIONS),
11961196
LintId::of(&copies::MATCH_SAME_ARMS),
@@ -1290,6 +1290,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
12901290
LintId::of(&attrs::MISMATCHED_TARGET_OS),
12911291
LintId::of(&attrs::UNKNOWN_CLIPPY_LINTS),
12921292
LintId::of(&attrs::USELESS_ATTRIBUTE),
1293+
LintId::of(&await_holding_invalid::AWAIT_HOLDING_LOCK),
1294+
LintId::of(&await_holding_invalid::AWAIT_HOLDING_REFCELL_REF),
12931295
LintId::of(&bit_mask::BAD_BIT_MASK),
12941296
LintId::of(&bit_mask::INEFFECTIVE_BIT_MASK),
12951297
LintId::of(&blacklisted_name::BLACKLISTED_NAME),
@@ -1736,6 +1738,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
17361738
LintId::of(&attrs::DEPRECATED_SEMVER),
17371739
LintId::of(&attrs::MISMATCHED_TARGET_OS),
17381740
LintId::of(&attrs::USELESS_ATTRIBUTE),
1741+
LintId::of(&await_holding_invalid::AWAIT_HOLDING_LOCK),
1742+
LintId::of(&await_holding_invalid::AWAIT_HOLDING_REFCELL_REF),
17391743
LintId::of(&bit_mask::BAD_BIT_MASK),
17401744
LintId::of(&bit_mask::INEFFECTIVE_BIT_MASK),
17411745
LintId::of(&booleans::LOGIC_BUG),

clippy_lints/src/utils/paths.rs

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ pub const RANGE_ARGUMENT_TRAIT: [&str; 3] = ["core", "ops", "RangeBounds"];
9393
pub const RC: [&str; 3] = ["alloc", "rc", "Rc"];
9494
pub const RC_PTR_EQ: [&str; 4] = ["alloc", "rc", "Rc", "ptr_eq"];
9595
pub const RECEIVER: [&str; 4] = ["std", "sync", "mpsc", "Receiver"];
96+
pub const REFCELL_REF: [&str; 3] = ["core", "cell", "Ref"];
97+
pub const REFCELL_REFMUT: [&str; 3] = ["core", "cell", "RefMut"];
9698
pub const REGEX_BUILDER_NEW: [&str; 5] = ["regex", "re_builder", "unicode", "RegexBuilder", "new"];
9799
pub const REGEX_BYTES_BUILDER_NEW: [&str; 5] = ["regex", "re_builder", "bytes", "RegexBuilder", "new"];
98100
pub const REGEX_BYTES_NEW: [&str; 4] = ["regex", "re_bytes", "Regex", "new"];

src/lintlist/mod.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,17 @@ vec![
6262
},
6363
Lint {
6464
name: "await_holding_lock",
65-
group: "pedantic",
65+
group: "correctness",
6666
desc: "Inside an async function, holding a MutexGuard while calling await",
6767
deprecation: None,
68-
module: "await_holding_lock",
68+
module: "await_holding_invalid",
69+
},
70+
Lint {
71+
name: "await_holding_refcell_ref",
72+
group: "correctness",
73+
desc: "Inside an async function, holding a RefCell ref while calling await",
74+
deprecation: None,
75+
module: "await_holding_invalid",
6976
},
7077
Lint {
7178
name: "bad_bit_mask",

tests/ui/await_holding_refcell_ref.rs

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// edition:2018
2+
#![warn(clippy::await_holding_refcell_ref)]
3+
4+
use std::cell::RefCell;
5+
6+
async fn bad(x: &RefCell<u32>) -> u32 {
7+
let b = x.borrow();
8+
baz().await
9+
}
10+
11+
async fn bad_mut(x: &RefCell<u32>) -> u32 {
12+
let b = x.borrow_mut();
13+
baz().await
14+
}
15+
16+
async fn good(x: &RefCell<u32>) -> u32 {
17+
{
18+
let b = x.borrow_mut();
19+
let y = *b + 1;
20+
}
21+
baz().await;
22+
let b = x.borrow_mut();
23+
47
24+
}
25+
26+
async fn baz() -> u32 {
27+
42
28+
}
29+
30+
async fn also_bad(x: &RefCell<u32>) -> u32 {
31+
let first = baz().await;
32+
33+
let b = x.borrow_mut();
34+
35+
let second = baz().await;
36+
37+
let third = baz().await;
38+
39+
first + second + third
40+
}
41+
42+
async fn less_bad(x: &RefCell<u32>) -> u32 {
43+
let first = baz().await;
44+
45+
let b = x.borrow_mut();
46+
47+
let second = baz().await;
48+
49+
drop(b);
50+
51+
let third = baz().await;
52+
53+
first + second + third
54+
}
55+
56+
async fn not_good(x: &RefCell<u32>) -> u32 {
57+
let first = baz().await;
58+
59+
let second = {
60+
let b = x.borrow_mut();
61+
baz().await
62+
};
63+
64+
let third = baz().await;
65+
66+
first + second + third
67+
}
68+
69+
#[allow(clippy::manual_async_fn)]
70+
fn block_bad(x: &RefCell<u32>) -> impl std::future::Future<Output = u32> + '_ {
71+
async move {
72+
let b = x.borrow_mut();
73+
baz().await
74+
}
75+
}
76+
77+
fn main() {
78+
let rc = RefCell::new(100);
79+
good(&rc);
80+
bad(&rc);
81+
bad_mut(&rc);
82+
also_bad(&rc);
83+
less_bad(&rc);
84+
not_good(&rc);
85+
block_bad(&rc);
86+
}
+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
error: this RefCell Ref is held across an 'await' point. Consider ensuring the Ref is dropped before calling await.
2+
--> $DIR/await_holding_refcell_ref.rs:7:9
3+
|
4+
LL | let b = x.borrow();
5+
| ^
6+
|
7+
= note: `-D clippy::await-holding-refcell-ref` implied by `-D warnings`
8+
note: these are all the await points this ref is held through
9+
--> $DIR/await_holding_refcell_ref.rs:7:5
10+
|
11+
LL | / let b = x.borrow();
12+
LL | | baz().await
13+
LL | | }
14+
| |_^
15+
16+
error: this RefCell Ref is held across an 'await' point. Consider ensuring the Ref is dropped before calling await.
17+
--> $DIR/await_holding_refcell_ref.rs:12:9
18+
|
19+
LL | let b = x.borrow_mut();
20+
| ^
21+
|
22+
note: these are all the await points this ref is held through
23+
--> $DIR/await_holding_refcell_ref.rs:12:5
24+
|
25+
LL | / let b = x.borrow_mut();
26+
LL | | baz().await
27+
LL | | }
28+
| |_^
29+
30+
error: this RefCell Ref is held across an 'await' point. Consider ensuring the Ref is dropped before calling await.
31+
--> $DIR/await_holding_refcell_ref.rs:33:9
32+
|
33+
LL | let b = x.borrow_mut();
34+
| ^
35+
|
36+
note: these are all the await points this ref is held through
37+
--> $DIR/await_holding_refcell_ref.rs:33:5
38+
|
39+
LL | / let b = x.borrow_mut();
40+
LL | |
41+
LL | | let second = baz().await;
42+
LL | |
43+
... |
44+
LL | | first + second + third
45+
LL | | }
46+
| |_^
47+
48+
error: this RefCell Ref is held across an 'await' point. Consider ensuring the Ref is dropped before calling await.
49+
--> $DIR/await_holding_refcell_ref.rs:45:9
50+
|
51+
LL | let b = x.borrow_mut();
52+
| ^
53+
|
54+
note: these are all the await points this ref is held through
55+
--> $DIR/await_holding_refcell_ref.rs:45:5
56+
|
57+
LL | / let b = x.borrow_mut();
58+
LL | |
59+
LL | | let second = baz().await;
60+
LL | |
61+
... |
62+
LL | | first + second + third
63+
LL | | }
64+
| |_^
65+
66+
error: this RefCell Ref is held across an 'await' point. Consider ensuring the Ref is dropped before calling await.
67+
--> $DIR/await_holding_refcell_ref.rs:60:13
68+
|
69+
LL | let b = x.borrow_mut();
70+
| ^
71+
|
72+
note: these are all the await points this ref is held through
73+
--> $DIR/await_holding_refcell_ref.rs:60:9
74+
|
75+
LL | / let b = x.borrow_mut();
76+
LL | | baz().await
77+
LL | | };
78+
| |_____^
79+
80+
error: this RefCell Ref is held across an 'await' point. Consider ensuring the Ref is dropped before calling await.
81+
--> $DIR/await_holding_refcell_ref.rs:72:13
82+
|
83+
LL | let b = x.borrow_mut();
84+
| ^
85+
|
86+
note: these are all the await points this ref is held through
87+
--> $DIR/await_holding_refcell_ref.rs:72:9
88+
|
89+
LL | / let b = x.borrow_mut();
90+
LL | | baz().await
91+
LL | | }
92+
| |_____^
93+
94+
error: aborting due to 6 previous errors
95+

0 commit comments

Comments
 (0)